Skip to main content

Unit tests

An incorrect Rule configuration in an application can cause delays in Case processing. When an error occurs, end users might need to reassign work, or a Case might require repair by an administrator. For example, consider a Case that needs to be routed to the Fulfillment department. If the Case is routed instead to the Accounting department, an accountant must reroute the Case to Fulfillment. The accountant wastes time rerouting the Assignment while the Fulfillment department is idle. The result is a delay to the customer during Case reassignment.

error_customer_delay

To avoid configuration errors such as incorrectly routed Assignments, developers test their applications. The most basic form of application testing is unit testing individual Rules. Unit testing supports the continuous delivery of applications by enabling quality testing of the smallest units of functionality. In a Pega application, the smallest unit is an individual Rule.

The purpose of unit testing is to verify that each element of the application, for example, a Decision Table or a report definition, works as expected. Unit testing reduces the risk of a configuration error in one Rule being propagated to other Rules in the application, which causes significant delays in Case processing. 

Use unit testing to reduce configuration errors. By unit testing the individual Rules, you know that each Rule works as expected when you configure them. For example, consider a Decision Tree that evaluates a property. As shown in the following image, the application reads the property from a Data Page that is sourced from a Report Definition. If the Decision Tree returns an incorrect result but the Data Page contains the correct data, you can isolate the error to the Decision Tree.

isolate_cause_of_error

Check your knowledge with the following interaction:

Unit testing individual Rules

Unit testing in Pega focuses on validating that individual Rules behave as expected when provided with defined inputs. By testing Rules in isolation, teams can confirm that logic is correct and reflects real runtime behavior, since Rule Resolution ensures the appropriate version is applied during testing.
To support repeatability, individual test runs can be captured as reusable test cases. These test cases define expected outcomes and allow teams to continuously verify Rule behavior as the application evolves. This approach supports continuous delivery by ensuring that changes are validated early and frequently, reducing the risk of regression.
Test cases rely on clearly defined expectations, often expressed as assertions that verify outputs, decisions, performance, or data conditions.

The following table provides some examples of assertions and their uses.

Assertion type Usage Example
Property Tests the value of the specified property. Requires the page on which the property is defined, a comparison operation, and a comparison value. pxUrgencyWork is equal to 10
Decision result Tests the value returned by a Decision Rule. Requires values for each input property needed by the Decision Rule to return the expected result. When Referred by employee is false, return RecruitingWB
Expected run time Tests whether a Rule runs within an allowed amount of time. Requires a comparison operation and an allowed time in seconds. Expected run time is less than or equal to three seconds
Page Tests for the presence of a page in memory. Requires the name of the page and a comparison operation. Page D_CoursesList has no errors.

By organizing test cases and running them regularly, teams can quickly identify failures and isolate issues. Grouping test cases further supports comprehensive validation across multiple scenarios, improving overall application quality while maintaining a consistent development pace.

Best practices to configure unit tests

Automated unit tests deliver actionable results and the time spent on running and maintaining automated tests is less than the time spent on manual testing. Ensure that test development occurs simultaneously with the development of Rules for your Pega Platform™ application. You may decide to reuse test cases during ongoing development and other teams might leverage your test suites. 

When to create automated unit tests

When a Rule returns an expected result, consider configuring an automated unit test as the following priority table suggests.

High-priority Low-priority
Tests that have predictable results Tests that undergo frequent changes that require maintenance of test cases
Tests that are required to run frequently Tests that are easy to test manually 
Tests that help to reduce manual effort when testing complex logic Tests that are too complex to automate
Tests that contain Rules with wide usage across the application Tests that contain persistence of data from a database
Tip: It is recommended that you run tests on every merge and check-in at a minimum, but ideally on a more frequent, regular basis. 

Build for coverage 

Because your unit tests cover a wide range of scenarios, ensure that sufficient validations are created to cover all positive and negative scenarios. Aim to include as much Rule coverage as possible, which includes different paths of Rule execution. Add tests that cover all input and output combinations, but keep the test case logic short and visible to optimize reusability. Smaller unit tests can help quickly pinpoint when Rule functionality does not work and promote ease in design and maintenance. 

Build for maintenance

Each test case must be easy to read and understand by any person. For example, test case names and descriptions are relevant and explain the purpose of the test case. Add comments for every step for better readability and to ensure ease of maintenance.

Make test data as modular as possible to make any updates or future changes easier and quicker. For example, you do not need to create test data for the application as a whole or larger, complex data structures. More modular test data means that small changes do not require reconfiguring all test data and potentially introducing issues with other tests. 

 

Check your knowledge with the following interaction:


This Topic is available in the following Module:

If you are having problems with your training, please review the Pega Academy Support FAQs.

Did you find this content helpful?

Want to help us improve this content?

We'd prefer it if you saw us at our best.

Pega Academy has detected you are using a browser which may prevent you from experiencing the site as intended. To improve your experience, please update your browser.

Close Deprecation Notice