You will often want to test that calculations are being carried out correctly, according to some business rule. Tables of concrete examples can help you to understand business needs and communicate what's required. This chapter will focus on how to read such Fit tests, and why it is important to do so.

This chapter is from the book

We often want to test that calculations are being carried out correctly, according to some business rule. Tables of concrete examples help us to understand business needs and communicate what's required. Here, we will focus on how to read such Fit tests; later, we will show how to write them.

We begin with two simple examples that test calculations by using tables of type ColumnFixture, which is designed to do this.

These tests are rather abstract in that they say nothing about how someone using the system under test will see the consequences of this business rule. In Chapter 4, we'll see tests that are more aligned to the step-by-step use of the system under test.

3.1 Calculating Discount

The business rule for our first example follows.

A 5 percent discount is provided whenever the total purchase is greater than $1,000.

We use examples in a table to help define the relationship between the amount and the discount for several cases. For example, when the amount is $1,100, the discount is $55. Figure 3.1 shows a simple set of tests for the part of a system that calculates discounts.

Table 3.1. Fit Table for Testing Discount (with an Error)

CalculateDiscount

amount

discount()

0.00

0.00

100.00

0.00

999.00

0.00

1000.00

0.00

1010.00

50.50

1100.00

55.00

1200.00

60.00

2000.00

100.00

The fixture, CalculateDiscount, is named in the first row of the table. The fixture determines how the examples in the table are to be tested automatically against the system under test.

As this is a ColumnFixture table, the second row is a header row, which labels the given and calculated value columns. Here, the amount column holds the given values, and the discount() column holds the calculated results that are expected.
[1]

Column labels in the header row serve two purposes.

They help us to understand the examples in the table.

They are used when the examples are automatically tested by Fit.

The remaining eight test rows of the table are our test cases, which are checked one at a time. For the first test case, the given value under amount is 0, and so a calculated value of 0 under discount() is expected. The second test case is independent of the first; the givenamount is 100, and so the calculateddiscount() is expected to be 0. And so on.

Fit runs the tests, one row at a time, against the system under test. Fit begins with the third row of the table, providing the given value of 0.00 to the application. Fit checks that the result calculated by the application, based on that amount, is 0.00, as expected.