Using Unit Test Framework for Rules

Last Updated: 07/15/2016 Introduced in Verision: 2.0
The rule engine has a unit test framework that allows you to write a suite of tests and execute them against your rule. This allows you to quickly see if your rule is returning the expected results. This functionality is very useful for verifying that changes made to existing rules have not broken the expected results of a rule.
 
Example
We will create a simple rule that tests if two numbers are equal. Then we will build a suite of unit tests to evaluate the functionality of our rule.
 
Write a simple rule with two Int32 inputs (Name your inputs Number to Evaluate 1 and Number to Evaluate 2). This rule will evaluate if those two numbers are equal.
 
 ruleSet
 
Click Debug Rule. Enter Eval 9 and 9 for Test Name. Enter 9 for Number to Evaluate 1 and Number to Evaluate 2. Click Save and then close this window.
 
 debugTest9and9
Click Debug again. This time the debugger will open with your newly added Unit Test. We will add a few more tests and then run it to see how it works. Click Create New and enter the follow Test Name: Eval 6 and 6, Test Result: Pass, Number to Evaluate 1: 6, Number to Evaluate 2: 6. Click Save.
 
 eval6and6
Click Create New once more. This time enter Test Name: Eval 1 and 3, Test Restult: Fail, Number to Evaluate 1: 1, Number to Evaluate 2: 3. Click Save.
 
 eval1and3
To run your new test suite, click Run All. This will run all three tests against your rule and evaluate the outcome of the rule compared to the expected outcome you specified in the test definition.
 
 runAll
If you ever modify this rule in a way that breaks it (e.g. changing the Equals verb to the Greater Than verb), your unit tests will fail, alerting you that something is wrong. The screen shot below shows the results of the test run after changing the Equals verb to the Greater Than verb. As you can see, one of my tests still happened to pass because the outcome of that test was the same using the Equals or the Greater Than verbs. This illustrates the need to build many different unit tests that test for both positive and negative conditions.
changeRuleDebugTest
 

Additional Resources