Using existing forms for regression tables

If you already have correct outputs of the business process in forms that can be easily converted to HTML tables, you can use them as FitNesse tests almost straight away. Common examples are MS Word invoices and Excel calculations. You can copy HTML code directly into FitNesse pages (enclose it within !- and -! to prevent wiki formatting). Alternatively, you can use the Spreadsheet to FitNesse button to create test tables from old forms in tab-separated format.

Here is a simple example: we want to use an old invoice (Figure 13.1, “We can turn this invoice into a FitNesse test”) as a regression test for calculating tax. To connect the invoice to our business code, we can use TableFixture, a fixture class for testing free-form tables. Note that it is in the fitnesse.fixtures namespace, not in fit or fitlibrary like all the classes we have seen so far. To use a TableFixture, we need to implement the DoStaticTable(int rows) method and process our table there. We can get a string or integer in any cell using GetString(int row, int column) and GetInt(int row, int column). Tests that pass should be marked by calling the Right(int row, int column) method and tests that fail should be marked by calling Wrong(int row, int column, string actualValue).

Figure 13.1. We can turn this invoice into a FitNesse test

Item

Product code

Price

Pragmatic Programmer

B978-0201616224

34.03

Sony RDR-GX330

ERDR-GX330

94.80

Test Driven Development By Example

B978-0321146533

32.39

Net Total

 

161.22

Tax (10% on applicable items)

 

9.48

Total

 

170.70


We are interested only in the final tax calculation, so we just disregard the rest of the table. Let's check whether the value in the second row from the bottom matches what our tax calculator works out based on the rows in the middle.

extended/Invoice.cs


15    public class Invoice:fitnesse.fixtures.TableFixture
16    {
17      protected override void DoStaticTable(int rows)
18      {
19        TaxCalculator tc=new TaxCalculator();
20        decimal totaltax = 0;
21        for (int row = 1; row < rows - 3; row++)
22        {
23          totaltax += tc.GetTax(GetString(row, 1), 
24            Decimal.Parse(GetString(row, 2)));
25        }
26        decimal taxintable = Decimal.Parse(GetString(rows - 2, 2));
27        if (taxintable == totaltax)
28          Right(rows - 2, 2);
29        else
30          Wrong(rows - 2, 2, totaltax.ToString());
31      }
32    }

The cell that holds the calculated tax (third cell in the second row from below) is used as a test, while the others are ignored. We can use the invoice table to run the test (see Figure 13.2, “We can test free-form tables with TableFixture).

Figure 13.2. We can test free-form tables with TableFixture

We can test free-form tables with TableFixture