Книга: Practical Programming, Fourth Edition
Назад: Case Study: Testing running_sum
Дальше: Hunting Bugs

Choosing Test Cases

Having a set of tests that pass is good; it shows that your code performs as expected in the situations you’ve considered. However, for any large project, there will be situations that don’t occur to you. Tests can reveal the absence of many bugs, but they cannot confirm that a program is entirely correct.

It’s essential to ensure you have good test coverage: your test cases should thoroughly cover critical situations. In this section, we provide some heuristics to help you develop a comprehensive set of test cases.

Now that you’ve seen two example sets of tests, we’ll give you an overview of things to think about while you’re developing tests for other functions. Some of them overlap, and not all will apply in every situation, but they are all worth thinking about while you are figuring out what to test.

If you carefully plan your test cases according to these ideas and your code passes the tests, there’s a very good chance that it will work for all other cases as well. Over time, you’ll commit fewer and fewer errors. Whenever you find an error, figure out why it happened; as you mentally catalog them, you’ll subsequently become more conscious of them. And that’s the whole point of focusing on quality. The more you do it, the less likely it is for problems to arise.

A Software Tester Walks Into A Bar...
by Random Guy on Reddit
Random Guy on Reddit

A software tester walks into a bar. Runs into a bar. Crawls into a bar. Dances into a bar. Flies into a bar. Jumps into a bar. And orders a beer. 2 beers. 0 beers. 99999999 beers. A lizard in a beer glass. -1 beer. “Qwertyuiop” beers. Testing complete.

A real customer walks into the bar and asks where the bathroom is. The bar goes up in flames.

Назад: Case Study: Testing running_sum
Дальше: Hunting Bugs