[https://softwareengineering.stackexchange.com/questions/750/what-should-you-test-with-unit-tests](https://softwareengineering.stackexchange.com/questions/750/what-should-you-test-with-unit-tests]

Among the plethora of answers thusfar no one has touched upon equivalence partitioning and boundary value analysis, vital considerations in the answer to the question at hand. All of the other answers, while useful, are qualitative but it is possible–and preferable–to be quantitative here. @fishtoaster provides some concrete guidelines, just peeking under the covers of test quantification, but equivalence partitioning and boundary value analysis allow us to do better.

In equivalence partitioning, you divide the set of all possible inputs into groups based on expected outcomes. Any input from one group will yield equivalent results, thus such groups are called equivalence classes. (Note that equivalent results does not mean identical results.)

As a simple example consider a program that should transform lowercase ASCII characters to uppercase characters. Other characters should undergo an identity transformation, i.e. remain unchanged. Here is one possible breakdown into equivalence classes:
等价类
| # | Equivalence class | Input | Output | # test cases |
+————————————————————————+
| 1 | Lowercase letter | a – z | A – Z | 26 |
| 2 | Uppercase letter | A – Z | A – Z | 26 |
| 3 | Non-alphabetic chars | 0-9!@#,/”… | 0-9!@#,/”… | 42 |
| 4 | Non-printable chars | ^C,^S,TAB… | ^C,^S,TAB… | 34 |
The last column reports the number of test cases if you enumerate all of them. Technically, by @fishtoaster’s rule 1 you would include 52 test cases–all of those for the first two rows given above fall under the “common case”. @fishtoaster’s rule 2 would add some or all from rows 3 and 4 above as well. But with equivalence partitioning testing any one test case in each equivalence class is sufficient. If you pick “a” or “g” or “w” you are testing the same code path. Thus, you have a total of 4 test cases instead of 52+.

Boundary value analysis recommends a slight refinement: essentially it suggests that not every member of an equivalence class is, well, equivalent. That is, values at boundaries should also be considered worthy of a test case in their own right. (One easy justification for this is the infamous off-by-one error!) Thus, for each equivalence class you could have 3 test inputs. Looking at the input domain above–and with some knowledge of ASCII values–I might come up with these test case inputs:
边界值
| # | Input | # test cases |
| 1 | a, w, z | 3 |
| 2 | A, E, Z | 3 |
| 3 | 0, 5, 9, !, @, *, ~ | 7 |
| 4 | nul, esc, space, del | 4 |
(As soon as you get more than 3 boundary values that suggests you might want to rethink your original equivalence class delineations, but this was simple enough that I did not go back to revise them.) Thus, boundary value analysis brings us up to just 17 test cases — with a high confidence of complete coverage — compared to 128 test cases to do exhaustive testing. (Not to mention that combinatorics dictate that exhaustive testing is simply infeasible for any real-world application!)

发表评论

电子邮件地址不会被公开。

18 + 17 =