Rigor in capturing test cases for unit testing

后端 未结 5 1177
没有蜡笔的小新
没有蜡笔的小新 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.

    0 讨论(0)
  • 2021-02-09 14:31

    A good code coverage tool really helps.

    100% coverage doesn't mean that it definitely is adequately tested, but it's a good indicator.

    For .Net NCover's quite good, but is no longer open source.


    @Mike Stone - Yeah, perhaps that should have been "high coverage" - we aim for 80% minimum, past about 95% it's usually diminishing returns, especially if you have belt 'n' braces code.

    0 讨论(0)
  • 2021-02-09 14:33

    From a practical standpoint, I create a list of tests that I believe must pass prior to acceptance. I test these and automate where possible. Based on how much time I've estimated for the task or how much time I've been given, I extend my test coverage to include items that should pass prior to acceptance. Of course, the line between must and should is subjective. After that, I update automated tests as bugs are discovered.

    0 讨论(0)
  • 2021-02-09 14:36

    @Keith

    I think you nailed it, code coverage is important to look at if you want to see how "done" you are, but I think 100% is a bit unrealistic a goal. Striving for 75-90% will give you pretty good coverage without going overboard... don't test for the pure sake of hitting 100%, because at that point you are just wasting your time.

    0 讨论(0)
  • 2021-02-09 14:46

    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).

    0 讨论(0)
提交回复
热议问题