Test cases can be designed in two ways. One looks only at what the software should do, and the other looks at how the code is built.

Black-Box Testing

Black-box testing, or functional testing, treats the software as a box whose inside cannot be seen. Test cases are made from the requirements, using the inputs and the expected outputs. It finds missing functions, interface errors and errors in the behaviour. Equivalence partitioning divides the input into classes that the program should treat alike, and one test is taken from each class. Boundary value analysis tests the values at the edges of each class, because errors tend to occur there. For a field that accepts ages 18 to 60, the boundary tests are 17, 18, 19, 59, 60 and 61.

Definition: Black-box testing. A testing method in which test cases are designed from the specified behaviour of the software, without reference to its internal structure.

White-Box Testing

White-box testing, or glass-box testing, uses the internal structure of the code to design the tests. The aim is to exercise all independent paths, all decisions on both their true and false sides, all loops at their boundaries, and all internal data structures. Statement coverage requires that every statement is run at least once, and branch coverage requires that every decision takes both outcomes. Basis path testing finds a set of independent paths using the cyclomatic complexity of the control flow graph.

Cyclomatic Complexity

The control flow graph has nodes for statements and edges for the flow of control. The cyclomatic complexity V(G) = E - N + 2, where E is the number of edges and N the number of nodes. It also equals the number of decision points plus one, and the number of regions of the graph. V(G) is the number of independent paths, and so the least number of tests that are needed to cover every statement.

Table 3.6: Black-box and white-box testing

Aspect Black box White box
Based on Requirements Code structure
Needs code knowledge No Yes
Techniques Equivalence classes, boundary values Statement, branch, basis path
Done mainly by Testers Developers

Example 3.6: Cyclomatic complexity

Problem: The control flow graph in the figure has 6 nodes and 7 edges. Find the cyclomatic complexity, list the independent paths and check the answer using the decision points.

Solution:

  1. V(G) = E - N + 2 = 7 - 6 + 2 = 3.
  2. The independent paths are 1-2-3-5-6, 1-2-4-5-6 and 1-2-3-5-2-4-5-6.
  3. The graph has two decision points, nodes 2 and 5, so decisions + 1 = 3, which agrees.

Result: V(G) = 3, so there are 3 independent paths and at least 3 test cases are needed.

E, N = 7, 6
print("V(G) =", E - N + 2)
lo, hi = 18, 60
tests = [lo - 1, lo, lo + 1, hi - 1, hi, hi + 1]
print(tests, [lo <= a <= hi for a in tests])

Output

V(G) = 3
[17, 18, 19, 59, 60, 61] [False, True, True, True, True, False]

Common mistakes

  • Testing only valid inputs and skipping the boundaries.
  • Thinking that 100 percent statement coverage means the code is error free.
  • Counting nodes and edges wrongly when finding cyclomatic complexity.

Key takeaways

  • Black-box tests use the specification, and white-box tests use the code structure.
  • Boundary value analysis tests the edges of the input classes.
  • V(G) = E - N + 2.
  • V(G) is the number of independent paths.

Practice questions

  1. What is boundary value analysis?
  2. Write the formula of cyclomatic complexity.
  3. Which testing needs knowledge of the code?

Answers to the practice questions

  1. Testing the values at and just beside the edges of each input class.
  2. V(G) = E - N + 2.
  3. White-box testing.

Sources

  • Official BCA syllabus Software Engineering, course code 26BCA403DS03, Unit III