You define unit test scenarios in JSON files. You create one JSON file per
scenario.
About this task
A JSON file consists of three parts: a name, input data, and a list of assertions.
You can retrieve the input data content from Decision Designer by copying a JSON
payload from a data set in the Run tab. For more information about the
Run feature, see Testing models in Decision Designer. Alternatively,
you can find your data sets in the <model>/resources/validate_dataset folder
of your decision library.
You can define one or more assertions in a test. Each assertion consists of five items:
label
- Defines the name of the assertion.
pointer
- Represents the path to the tested values in the output.
operator
- Defines the type of comparison that you want to perform. The list of supported operators is
available in Table 1.
ignoringOrder (optional)
- Makes an order agnostic comparison of collections if set to
true. It is set to
false by default.
expected
- Contains all expected values.
Table 1. List of supported operators in assertions
| Operator |
Definition |
Example |
Details |
⊃ |
Contains |
{
"label": "Assertion 1",
"pointer": "/customer/address",
"operator": "⊃",
"expected": "Paris"
}
|
These operators can be used on any type of data.
They support the ignoringOrder option for collections.
|
⊅ |
Does not contain |
{
"label": "Assertion 1",
"pointer": "/customer/address",
"operator": "⊅",
"expected": "London"
}
|
= |
Equals |
{
"label": "Assertion 1",
"pointer": "/approval/approved",
"operator": "=",
"expected": false
}
|
These operators can be used on any type of data.
They support the ignoringOrder option for collections.
|
!= |
Does not equal |
{
"label": "Assertion 1",
"pointer": "/approval/approved",
"operator": "!=",
"expected": false
}
|
> |
Is greater than |
{
"label": "Assertion 1",
"pointer": "/customer/age",
"operator": ">",
"expected": 18
}
|
These operators can be used on numbers. |
≥ |
Is greater than or equal to |
{
"label": "Assertion 1",
"pointer": "/customer/age",
"operator": "≥",
"expected": 18
}
|
< |
Is less than |
{
"label": "Assertion 1",
"pointer": "/customer/age",
"operator": "<",
"expected": 18
}
|
≤ |
Is less than or equal to |
{
"label": "Assertion 1",
"pointer": "/customer/age",
"operator": "≤",
"expected": 18
}
|
∈ |
Is within a range |
{
"label": "Assertion 1",
"pointer": "/customer/age",
"operator": "∈",
"expected": {
"min": 10,
"max":15,
"minExclusive": true,
"maxExclusive": true
}
}
|
These operators can be used on numbers. |
∉ |
Is not within a range |
{
"label": "Assertion 1",
"pointer": "/customer/age",
"operator": "∉",
"expected": {
"min": 10,
"max":15,
}
}
|
Procedure
-
Create a
resources/<packagename>/<operationID>
folder under the src/test/ folder that you created in Creating a Java test file. The <packagename> must be the same as the one you used when creating the Java test file.
- Create a
.json file in the directory you just created, for example
lowcreditscore.json.
- Define your scenario using the details provided in the About this task
section.
The following example shows a complete test file named
too big debt income
ratio that contains two assertions:
{
"name": "too big debt income ratio",
"input": {
"loan": {
"numberOfMonthlyPayments": 72,
"startDate": "2020-06-01T00:00:00Z",
"amount": 185000,
"loanToValue": 0.7
},
"borrower": {
"firstName": "John",
"lastName": "Doe",
"SSN": {
"areaNumber": "123",
"groupCode": "45",
"serialNumber": "6789"
},
"latestBankruptcy": {
"date": "2019-03-10T00:00:00Z",
"chapter": 7,
"reason": "Unemployment"
},
"yearlyIncome": 100000,
"zipCode": "91320",
"creditScore": 500,
"birthDate": "1968-05-12T00:00:00Z"
},
"currentTime": "2020-02-04T08:41:21.242Z"
},
"assertions": [
{
"label": "Approved",
"pointer": "/approval/approved",
"operator": "=",
"expected": false
},
{
"label": "Message, sorry",
"pointer": "/approval/message",
"operator": "⊃",
"expected": "sorry"
}
]
}
What to do next
You are now ready to run your unit tests. You can either run tests locally by using Maven.
To run unit tests locally, follow the instructions that are described in Building and deploying from a CI/CD stack.
To run unit tests in Decision Designer, you
must first push all the content of the src/test folder to your Git repository and
load it to Decision Designer. When you are ready
to run your unit tests, follow the instructions that are described in Building and deploying from Decision Designer. Click the View
report icon
next to the test status to see the test results.
Important: Unit tests are executed every time you deploy your decision service. You must
update unit tests whenever you update the model you are testing to avoid deployment failures.