Skip to content

WA5. Unit tests design methods

statement

  • If you could select only three test case design methods to apply during unit testing:
    • What would they be and why?
    • Try to support your answer with apt examples/scenarios.

solution

  • According to Marsic, 2012: there are many methods to design your test cases to generate the maximum coverage of all possible cases that the program unit (class, function, etc.) may work on:

    1. Equivalence Testing.
    2. Boundary Testing.
    3. Control-Flow Testing.
    4. State-Based Testing.
  • The Equivalence testing includes dividing the unit domain into equivalent groups. The program behaves the same for each group.

  • For example, the square root function f(x) = sqrt(x) accepts all real numbers as an input; we can divide them into two groups: 1. negative numbers. 2. positive numbers (including zero). For the negative numbers, sqrt should throw an error. For positive numbers, it should find the square root.
  • The boundary testing includes defining the domain of the unit, then choosing values from the left, right, and middle boundaries of this domain.
  • For example, the function f(x) = x * x which accepts all real numbers as input. To test this function, we can choose values from the left boundaries (most high negative numbers), e.g. a number approaches -Infinity, then some values from the right boundaries, e.g. a number approaches +Infinity; then some values in the middle, e.g. around zero.
  • The control flow testing is useful for branched units, where this unit may follow different paths of executions according to the circumstances. In these units, the test cases must cover every branch of the unit.
  • For example, the absolute value function f(x) = |x|, which includes a different behavior between positive and negative numbers. The test cases for this unit must include negative and positive numbers to include both branches.

references