Testing CS Thinking Example 4
Follow the full solution, then compare it with the other examples linked below.
Example 4
hardExplain the difference between white-box and black-box testing. Which approach would you use to test a sorting function, and why?
Solution
- 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 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 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
Example 1 easy
A function isEven(n) should return TRUE if n is even and FALSE otherwise. Suggest three test cases,
Example 2 mediumA program grades students: 90-100 = A, 80-89 = B, 70-79 = C, 60-69 = D, below 60 = F. Design a compl
Example 3 mediumA password validation function requires: at least 8 characters, at least one uppercase letter, and a