Adding Testcases
Introduction
In the previous tutorials we've been developing our ruleset and trying it out in the Sandbox to test it. Now that we've got a ruleset we're happy with, we want to make sure that it will continue to work as intended. We'd like to be notified when a change to our ruleset unknowingly changes its output.
Rulecube has a mechanism for this: Testcases.
Task: Create a Testcase
In the left menu, click on the + Next to the Testcases menu section

You should be presented with a new Testcase like this:

A Testcase consists of two parts, similar to the Sandbox: Input and Expected output. What we'll be defining here is "when this goes in, this should come out".
Let's first set the input:
Change the value of StartDate to:
2023-07-20Change the value of EndDate to:
2023-07-27Change the value of NumberOfItems to:
2Change the value of DateOfBirth:
1990-01-01
Now we need to define what we expect ElectronicDeviceCosts and InsuranceCosts to be. We could manually fill this in, but since this our first time with this Testcase, we can create a so-called baseline by clicking this button:
(next to the Reset button under Expected output fields).
Go ahead and press the create baseline button
This should result in the following:

Rulecube has determined that for the given Input, this is the Output.
Now what happens when the Expected output doesn't match the actual output? Let's say that an travel insurance expert looks at the Input of this Testcase and determines that the InsuranceCosts in the Output should actually be 4 in this case!
This means that somewhere while making our ruleset, we've made an error or we forgot something in our calculation!
Let's trust the travel insurance export for now:
Change the
InsuranceCostsin Expected output to4Save the ruleset
Note how the Testcase is run automatically when you save. It should now show a failure:

Rulecube has detected that the expected output no longer matches the actual output. Now let's see how we can fix the InsuranceCosts rule so that it will match the expected output.
Task: Fix the InsuranceCosts rule
InsuranceCosts ruleOpen the
InsuranceCostsrule

It seems we've missed something from our expert's specification! We need to always add 1 to the result to offset the insurance costs. Let's fix that!
Change the expression to the following:
1 + ROUND(RiskCostsPerDay.get(RiskCategory) * NumberOfDays + AgeSurcharge(Age, NumberOfDays))Save the ruleset
When the ruleset has been saved, the Testcase should succeed and the failure banners should disappear.
Task: Fix the InsuranceCosts rule again
InsuranceCosts rule againThe travel insurance expert just informed you that the insurance costs offset should actually be 10, not 1!
Let's implement this change to our code.
Open the InsuranceCosts rule again
Change the
1at the start of the expression to10Save the ruleset
As you might have expected, this will break our Testcase once again. Let's open our Testcase 1.

We could manually fix the value for InsuranceCosts here to match the actual value 13. An easier way is to press the CONSOLIDATE button below it. This will automatically change the expected output to match the actual output.
Click CONSOLIDATE
Alternatively, manually enter
13into InsuranceCosts output and Save
The ruleset should automatically save and re-run our Testcase. If all goes well, the Testcase should once again succeed!
Recap
We've learned how we can create Testcases that will allow us to make sure our rulesets work as expected. It will also notify you when a ruleset is broken by a change in the ruleset or its dependencies.
Once your ruleset reaches a certain level of complexity, it is recommended to add testcases to it and to not rely on manual testing in the Sandbox anymore.