Rigor in capturing test cases for unit testing

后端 未结 5 1163
没有蜡笔的小新
没有蜡笔的小新 2021-02-09 14:02

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

List SortNumbers(List unsorted, bool ascending);
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-09 14:31

    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.

提交回复
热议问题