Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rigor in capturing test cases for unit testing

Let's say we have a simple function defined in a pseudo language.

List<Numbers> SortNumbers(List<Numbers> unsorted, bool ascending);

We pass in an unsorted list of numbers and a boolean specifying ascending or descending sort order. In return, we get a sorted list of numbers.

In my experience, some people are better at capturing boundary conditions than others. The question is, "How do you know when you are 'done' capturing test cases"?

We can start listing cases now and some clever person will undoubtedly think of 'one more' case that isn't covered by any of the previous.

like image 885
Brad Barker Avatar asked Aug 15 '08 18:08

Brad Barker


People also ask

What should be considered for unit testing?

A unit test typically comprises of three stages: plan, cases and scripting and the unit test itself. In the first step, the unit test is prepared and reviewed. The next step is for the test cases and scripts to be made, then the code is tested.


2 Answers

Don't waste too much time trying to think of every boundry condition. Your tests won't be able to catch every bug first time around. The idea is to have tests that are pretty good, and then each time a bug does surface, write a new test specifically for that bug so that you never hear from it again.

Another note I want to make about code coverage tools. In a language like C# or Java where your have many get/set and similar methods, you should not be shooting for 100% coverage. That means you are wasting too much time writing tests for trivial code. You only want 100% coverage on your complex business logic. If your full codebase is closer to 70-80% coverage, you are doing a good job. If your code coverage tool allows multiple coverage metrics, the best one is 'block coverage' which measures coverage of 'basic blocks'. Other types are class and method coverage (which don't give you as much information) and line coverage (which is too fine grain).

like image 151
Justin Standard Avatar answered Nov 15 '22 09:11

Justin Standard


How do you know when you are 'done' capturing test cases?

You don't.You can't get to 100% except for the most trivial cases. Also 100% coverage (of lines, paths, conditions...) doesn't tell you you've hit all boundary conditions.

Most importantly, the test cases are not write-and-forget. Each time you find a bug, write an additional test. Check it fails with the original program, check it passes with the corrected program and add it to your test set.

An excerpt from The Art of Software Testing by Glenford J. Myers:

  1. If an input condition specifies a range of values, write test cases for the ends of the range, and invalid-input test cases for situations just beyond the ends.
  2. If an input condition specifies a number of values, write test cases for the minimum and maximum number of values and one beneath and beyond these values.
  3. Use guideline 1 for each output condition.
  4. Use guideline 2 for each output condition.
  5. If the input or output of a program is an ordered set focus attention on the first and last elements of the set.
  6. In addition, use your ingenuity to search for other boundary conditions

(I've only pasted the bare minimum for copyright reasons.)

Points 3. and 4. above are very important. People tend to forget boundary conditions for the outputs. 5. is OK. 6. really doesn't help :-)

Short exam

This is more difficult than it looks. Myers offers this test:

The program reads three integer values from an input dialog. The three values represent the lengths of the sides of a triangle. The program displays a message that states whether the triangle is scalene, isosceles, or equilateral.

Remember that a scalene triangle is one where no two sides are equal, whereas an isosceles triangle has two equal sides, and an equilateral triangle has three sides of equal length. Moreover, the angles opposite the equal sides in an isosceles triangle also are equal (it also follows that the sides opposite equal angles in a triangle are equal), and all angles in an equilateral triangle are equal.

Write your test cases. How many do you have? Myers asks 14 questions about your test set and reports that highly qualified professional programmes average 7.8 out of a possible 14.

like image 29
Christian Lescuyer Avatar answered Nov 15 '22 09:11

Christian Lescuyer