Appendix F. Exercises

“Arithmetic is where the answer is right and everything is nice and you can look out of the window and see the blue sky—or the answer is wrong and you have to start over and try again and see how it comes out this time.”

Carl Sandburg

“You can’t become a snowboarder by just reading about it.”

Anonymous

To round out the exercises that are suggested throughout this book, here are some additional exercises for you. The calculator test exercise gives you an opportunity to create acceptance tests for an existing system and then use these tests as the basis for creating a new implementation of that system. The other exercises are to create detailed requirements and acceptance tests.


Calculator

Almost everyone has used a calculator. In some elementary schools, students are required to demonstrate proficiency with one. The type of calculator for which the tests are being written is a simple one with a single memory for a number, such as the one available on the Windows operating system (see Figure F.1).

Figure F.1. Calculator

image

The table could specify a single input key, with the conditions of the previous step. Note that MS is “memory save” and MR is “memory recall.”

image

Alternatively, the table could state the sequence of steps.

image

For the memory save and recall, you could create even more tests.

image

Now you apply a number of inputs to see what the results might be, such as the following.

image

Create Some Tests

Because you have an existing application (a calculator), check the results of these inputs on that application. The 2 − − 3 test could produce –1 or 5, depending on how your existing calculator works. Change the results in the examples to match that calculator. Now give the examples to a developer, and have him create a program that passes these acceptance tests. Do you need a user interface? How much effort would it be to run the tests with a user interface than without one?

If the developer feels that the program is too large to complete easily, you can break up the acceptance tests. One way to do that is to separate tests involving the memory from the other tests. The memory is a separable feature. Another way is to have tests that specify only series of input that are “normal.” That is, you would leave out sequences that had two symbols immediately following each other, such as “/–”.

After the developer creates a program that passes the test as originally specified, change the result so that the other answer is expected. The acceptance test should fail. The developer now needs to alter the program to create the expected result. When the developer does so, do any other tests fail? Do you have redundant tests? Are all tests consistent?


More Exercises

Here are some other exercises. Create detailed requirements and the corresponding acceptance tests for each. Answers will be posted on atdd.biz.

Sam’s CD Rental

As the inventory manager, I want to get a list of all rentals for a CD so I can see whether it is popular.

As the owner, I want to see a record of all activities, such as when CDs are checked-out and checked-in. This helps me determine whether I need to hire more staff.

As an auditor, I need to be able to keep track of all money transfers for Sam’s CD Rental.

As a customer, I want to search for CDs by title, artist, or song.

As a customer, I also want to search for songs whose lengths fall between a minimum and maximum length expressed in seconds.

Triangle

A program inputs three values representing the lengths of the sides of a triangle. The program is to determine whether the triangle is scalene, isosceles, or equilateral [Myers01].

File Copying Exercise

A module is being written for an operating system to copy a file from one directory to another. The desired user interface follows.

For example:

copy some_directory another_directory a_file

The happy path includes the conditions where some_directory and another_directory exist and a_file exists in some_directory. There are many other conditions, such as another_directory does not exist.

List these conditions and create other cases.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.235.144