# Create and Run a Testcase

## Introduction

This guide explains how to create testcases to cover the quality assurance of your rulesets.

**Testcases** are predefined tests that inspect if your ruleset works as expected. A testcase consists of input and the expected output to the given input. For example, if your ruleset calculates the total amount for a number of products:

* The input could be:
  * Number of products = 7
  * Price per product = 1.45
* The expected output would be:
  * Total amount = 10.15

If this testcase runs and the output is 10.15, the testcase succeeds. For any other output value, the testcase fails, indicating you need to debug the ruleset to fix one or more issues until the testcase succeeds.

### Testcases run automatically

Rulecube automatically runs each defined testcases after you make some changes to your ruleset. This way, Rulecube ensures that your ruleset stays correct when you make adjustments.&#x20;

### Create testcases afterward or upfront for TTD

You have the choice of creating testcases afterward when you finish your ruleset or upfront as the first components of your ruleset according to the principles of [**Test-Driven Development**](https://en.wikipedia.org/wiki/Test-driven_development) **(TTD)**. With TTD, you start with creating testcases and then develop your ruleset until all testcases run successfully. This is because the idea behind TTD is that testcases represent the requirements for a program or the ruleset, and by creating a ruleset that meets the testcases, you are guaranteed to implement the requirements.

To be clear, with TTD, all your testcase fail at first. But as you develop your ruleset, they gradually succeed until they all do, and you're done developing.

It's up to you which method you follow, but we strongly advise creating testcases for your rulesets because they assure the quality of your rulesets with minimal effort.&#x20;

### Testcase design

Testing and test case design are a profession in their own right and too extensive to explain here fully. You can find plenty of information about testcase design on the Web, but here we'll give a few helpful general tips:

1. **Create a testcase for every group.**\
   For example, if your ruleset has different processing for different age groups, create at least one testcase for each age group.
2. **Test for boundary values.**\
   For example, if the input for a ruleset can only have values in the range of 0 to 99, create testcases for input = 0 and input = 99. The same applies to the output: if you have an output with a minimum and maximum, create testcases that return the min and max boundaries.

## Procedure

With the following steps, you'll create TTD-based testcases upfront for a ruleset that calculates the total amount for an ordered product:

### Step 1: Create testcases

1. Create a ruleset named `TTD_demo`.
2. Create an input named `NumberOfProducts`.
3. In the **Type** list, select `number`.
4. Click **Advanced settings**.
5. Select the **Required** checkbox.
6. In the **Minimum value** field, enter `1`. And in the **Maximum value** field, enter `20`.&#x20;
7. Select the **Strict** checkbox and save the input.
8. Create a rule named `TotalAmount` and set its **Expression** to: `return -1`. This way, you'll ensure that testcases will fail at first.
9. Create a testcase named `MinProducts`.\
   ↳ The **Testcase details** page opens with an **Input** and an **Expected** output section:<br>

   <figure><img src="/files/JO9TMoJWCrCcXdxTlCG5" alt=""><figcaption></figcaption></figure>
10. In the **NumberOfProducts** field, enter `1`.
11. In the **TotalAmount** field, enter `1.45`.
12. Click **Save and run**.\
    ↳ The testcase fails because it returns `-1` for `TotalAmount`, while `1.45` was expected.
13. Now, create a testcase named `MaxProducts` and set **NumberOfProducts** to `20` and **TotalAmount** to `29`.\
    ↳ This testcase also fails. And Rulecube signals this with red icons for the **Testcases** section:<br>

    <div align="left"><figure><img src="/files/yud7ZinWIv57djRTBnqk" alt=""><figcaption></figcaption></figure></div>

### Step 2: Build the ruleset to match the testcases (and your requirements)

1. Create a constant named `ProductPrice` and set its `Value` to `1.45`.

2. Change the **Expression** for the `TotalAmount` rule to: <br>

   ```javascript
   NumberOfProducts * ProductPrice
   ```

3. Save the rule.\
   ↳ Immediately after this change, Rulecube automatically reruns the testcases, which now all pass and confirm that you implemented the requirements for your ruleset:<br>

   <div align="left"><figure><img src="/files/N6BMxd2m5MUg5OmT1VhT" alt=""><figcaption></figcaption></figure></div>


---

# Agent Instructions: 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/how-to-guides/ruleset-development/create-and-run-a-testcase.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.
