Testing CS Thinking Example 4

Follow the full solution, then compare it with the other examples linked below.

Example 4

hard
Explain the difference between white-box and black-box testing. Which approach would you use to test a sorting function, and why?

Solution

  1. 1
    Step 1: Black-box testing tests the function without knowing its internal code โ€” only inputs and expected outputs. White-box testing examines the code structure and tests specific paths, branches, and edge cases.
  2. 2
    Step 2: For a sorting function, use both: black-box to verify it sorts correctly (test with sorted, reverse, random, empty, single-element lists), and white-box to ensure all code paths execute (e.g., both branches of comparison).
  3. 3
    Step 3: Black-box tests what it should do; white-box tests how thoroughly the code is exercised. Both are needed for thorough testing.

Answer

Black-box: test inputs/outputs without seeing code. White-box: test internal code paths. Use both for a sorting function for thorough coverage.
Professional software testing combines both approaches. Black-box testing validates requirements, while white-box testing ensures code coverage and catches implementation-specific bugs.

About Testing

Systematically running a program with known inputs to verify that its outputs are correct. Testing involves designing test cases that cover normal inputs, boundary values, and error conditions, then comparing actual results against expected results.

Learn more about Testing โ†’

More Testing Examples