An Example FitNesse TestIf you were testing the division function of a calculator application, you might like to see some examples working. You might want to see what you get back if you ask it to divide 10 by 2. (You might be hoping for a 5!)
In FitNesse, tests are expressed as tables of input data and expected output data. Here is one way to specify a few division tests in FitNesse:
This style of FitNesse test table is called a Decision Table, each row represents a complete scenario of example inputs and outputs. Here, the "numerator" and "denominator" columns are for inputs, and the question mark in the "quotient?" column tells FitNesse that this is our column of expected outputs. Notice our "10/2 = 5.0" scenario. Try reading it as a question: "If I give you a numerator of 10 and denominator of 2, do I get back a 5?"
Running our test table: Click the Test buttonIf you are reading this page on a local copy of FitNesse, run this test table. See the Test button in the upper-right corner? Click it and see what happens.
Ah, color! In the green cells, we got back the expected values from our code. When we divided 10 by 2, we expected and got back 5. When we divided 12.6 by 3, we expected and got back 4.2.
What about red? A cell turns red when we get back a different value than what we expected. We also see two values: the expected value and the actual value. Above we expected 33 back when we divided 100 by 4, but we got back 25. Ah, a flaw in our test table. That happens!
Notice also the comparisons. 22/7 is approximately equal (~=) to 3.14. 9/3 is less than 5. 11/2 is between 4 and 6.
Creating the TableHow did we put that test table on this page in the first place? Well, we used FitNesse itself to do it!
FitNesse is a wiki, which is a style of web server that allows any visitor to make any edits, including changing existing pages and creating new pages. A simple markup language lets you easily create headings, make text bold, underline, and italic, create bulleted lists, and do other kinds of simple formatting. You can read more about it on EditingFitNessePages.
Once you download FitNesse to your own computer, you can edit any page simply by clicking on the Edit button on the left (just below the Test button). If you are reading this page on your own copy of FitNesse on your own machine, you can click the Edit button and see for yourself.
The wiki markup for our table above (with some omissions) looks like this:
|eg.Division| |numerator|denominator|quotient?| |10 |2 |5 | |12.6 |3 |4.2 | |100 |4 |33 |The vertical bars delimit table cells. For this table, there is not much more too it than that. You don't have to line up the vertical bars this way; we just do it to make it easier to read.
Note that if you don't like mucking about with a markup language at all, you can create your test tables in a spreadsheet such as Excel, copy them to the clipboard, and use the Spreadsheet to Fitnesse button on the Edit page to have FitNesse format your table properly for you.
Note also that if you change "eg.Division" to any other name, you may need to use a ! before the fixture to avoid confusing FitNesse with WikiWords (see MarkupTable)