A Testing Example (T03) - Gnorion/BizVR GitHub Wiki
Suppose you have this business decision:
with the following business rules
How should we go about testing it?
If you run the validator it will suggest these test cases (you can add more if you want)
"test cases": [
{"testid": 1,"decision": "T03","age": 19,"income": 74999},
{"testid": 2,"decision": "T03","age": 19,"income": 75000},
{"testid": 3,"decision": "T03","age": 17,"income": 74999},
{"testid": 4,"decision": "T03","age": 17,"income": 75000},
{"testid": 5,"decision": "T03","age": 66,"income": 74999},
{"testid": 6,"decision": "T03","age": 66,"income": 75000}
]
You can then run these though the decision and check the result.
In order to do that you will have to copy each test into the evaluator and run each of the six tests separately and then check that you got the correct result.
This is not a problem with just six test cases, but suppose you had hundreds.
And don't forget you will need to rerun all of these tests every time you make a change to the rules.
So is there a way we can automate this?
Yes, of course
Step 1 (option 1) Create an expression that builds the test cases
You can copy the test cases from VV directly into the expression like this. Just add the expected results if you want to automate the checking.
Just remember that inside an expression you do not put the double quotes around the property names:
then create an expression to extract a single test case from the list of test cases based on the id:
Then (because the return values is a list):
Step 1 (option 2) Create a table to set the test values
This only requires a single table (compare with two expressions and a table for the other method) but you will have to enter the value into the table. With the other method you can just copy the json test cases directly into the expression The test id is the condition and the actions set the various values of the variables used in the business rules
Now, using either of these methods you can feed a selected test case to the business rules:
In order to execute every test case you just need something that can invoke the decision in a loop passing it the various test ids one at a time. The calling program doesn't need to know what the test cases are - it just needs to know how many.
And it can find that out by initially invoking the decision and executing the expression named "get number of test cases"
which will return the number of test cases that are available to be run: e.g.
Step 2 Add Table to Compare Actual and Expected
Add a table like this to compare the actual results with the expected results. In this example we are only checking the value of "eligible" but you can check as many as necessary
Step 3 Run the Test Cases
You can either manually run the test cases one at a time in the execution environment or use an external program to invoke the decision passing in the test case number or use the generic DMN Test Case Handler (Generic Test Case Handler Modeled in DMN)
In this example we provide the test case number ("atest") that will drive the expression that selects a test case
To select test cases from the decision table you just specify the value of N
And then another option is to ignore the built in test cases entirely and specify your own test case:
NOTE: this input must be in json format - so the property names are in double quotes. Also you don't need to supply the testid and decision since they are not used
When you do this you should disconnect the expression and table that supply internal test cases so only the values you supply as input are used
Step 4 Review Results
If you run test cases manually then you will see results like this
When you invoke this decision from the generic test case handler then these results will be passed back to the handler where you can create some summary statistics.