The decision table is the default table style used by FitNesse. That is, when not specifying a prefix, FitNesse decides it is a Decision Table. As an alternative, you can prefix the Fixture name definition in the Test page with ‘decision:’ or ‘dt:’, so the example below would look like: ‘decision:cr edits for payment’. An example of a decision table is shown below:
!3 Payment test
The amount you pay determines how many credits get added to your balance.
!|credits for payment|
|payment |credits?|
|.25 |1 |
|1.0 |5 |
|5.0 |25 |
|10 |60 |
The English text above outside the table is ignor ed by
FitNesse and only serves a documenting purpose. The (pipe separated) table however is executed by FitNesse. There are 3 items in the table: the fixture name (credits for payment), the header (payment & credits), and the data (the rest of the table).
Please note the exclamation mark (!) in the first row. While not explicitly needed in this example, it prevents FitNesse from interpreting camel case words as page links. Putting a exclamation mark in the first row will leave any camel case words as-is and will not turn them into page links.
The header row consists of two header names, payment and credits. Normally, each header corresponds to a set function. The ‘credits’ header contains a ? Decision Tables consider this to be an output and so calls it as a function. The return value of that function is compared to the cell contents and the cell is colored green if it matches the cell, or red if it doesn’t.
When executing this Test, FitNesse will look in the classpath for a fixture named CreditsForPayment. For each line in the table, FitNesse will call a setter for payment on the Fixture (setPayment(double payment)). After all ‘set’ functions have been called, the ‘execute’ method will be called by FitNesse. In the example below, the ‘execute’ method does the actual work. After that, the ‘credits’ function will be called (public int credits()), and the result of that call will be evaluated by FitNesse and colored accordingly. The code required to make this work is shown below.
package jukebox.fixtures;
public class CreditsForPayment {
private double payment;
private int credits;
public void setPayment(double payment) {
this.payment = payment;
}
public void execute() {
this.credits = JukeBox.calaculateCredits(payment)
}
public int credits() {
return credits;
}
}
Optional Functions
Decision tables have the option to implement optional functions, which will be called if they are defined in the Fixture. In the example above, the execute function is used. Decision tables know 3 optional functions:
FunctionDescriptionresetCalled once per row before any set or output functions care called.executeCalled once per row after all set functions have been called, but just before the first output (if any) is called.tableIs called after the constructor and before any rows are processed. It is passed the contents of the complete table in the form of a list of lists that contain all the cells except for the very first row.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}