> For the complete documentation index, see [llms.txt](https://docs.rulecube.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rulecube.com/v2.5/tutorial/adding-testcases.md).

# 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

1. In the left menu, click on the **+** Next to the Testcases menu section\
   ![](/files/4EsD9BWRLbj2SEojPM7k)
2. You should be presented with a new Testcase like this:<br>

   <figure><img src="/files/bIVb2BuLi1BLVHEeRUqK" alt=""><figcaption></figcaption></figure>

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:

1. Change the value of StartDate to: `2023-07-20`
2. Change the value of EndDate to: `2023-07-27`&#x20;
3. Change the value of NumberOfItems to: `2`
4. Change 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: ![](/files/WQqGewiqcOEeDQEwFNur) (next to the Reset button under Expected output fields).

1. Go ahead and press the create baseline button

This should result in the following:

<figure><img src="/files/C5TPvjfR5mQmMk6YhAww" alt=""><figcaption></figcaption></figure>

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:

1. Change the `InsuranceCosts` in Expected output to `4`&#x20;
2. Save the ruleset

Note how the Testcase is run automatically when you save. It should now show a failure:

<figure><img src="/files/xr0kG5PDyJ0TxN1RsrHE" alt=""><figcaption></figcaption></figure>

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

1. Open the `InsuranceCosts` rule

<figure><img src="/files/2pb8qhlQhlbjde8TLPdd" alt=""><figcaption></figcaption></figure>

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!

1. Change the expression to the following:<br>

   ```javascript
   1 + ROUND(RiskCostsPerDay.get(RiskCategory) * NumberOfDays + AgeSurcharge(Age, NumberOfDays))
   ```
2. 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

The 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.

1. Open the InsuranceCosts rule again
2. Change the `1` at the start of the expression to `10`
3. Save the ruleset

As you might have expected, this will break our Testcase once again. Let's open our `Testcase 1`.

<figure><img src="/files/jtx5drMV6kj0rxYP0URq" alt=""><figcaption></figcaption></figure>

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.&#x20;

1. Click CONSOLIDATE&#x20;
   1. Alternatively, manually enter `13` into 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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.rulecube.com/v2.5/tutorial/adding-testcases.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
