How to setup test data for Pegaunit tests
If we try to see "Test case" from a broader perspective, there are few checkpoints it passes during its runtime lifecycle. The first checkpoint is “Setup” of test data (Before rule execution option in setup). Setup is not always a required task, But, if actual rule which we are unit-testing, has some Pre-requisites or dependencies, or in other words, if some environment needs to be set before running it, can do this using the Setup checkpoints.
Examples: Let us take an example to demonstrate the scenario.
Say, during the “New Hire” case life cycle, we are using an activity to fetch “Best fit Role” for a particular applicant from Database.
Our Rule Under Test is FetchAvailableRole( Available_Position step is internally using this activity to fetch the Role). This activity calls a Decision table to find a suitable role. These Roles are fetched depending on the “Work experience” provided by the user in the Collect_Information step. So, during case execution, work experience will be present in “Work Object” itself.
But, when we are trying to test the activity FetchAvailableRole independently, we need to “Setup” the required “work experience” data for the activity, as this particular activity will run on “RunRecordPrimaryPage” and Work experience will not be present there. So, we will set Workexp property value in setup. We are using a data transform(setWorkExperience) to do our job. Both setup section and details of the data transform is shown below.
When we run the test case, it first Stops before setup checkpoint, and runs setWorkExperience and sets Workexp property to 7. After that, it runs the Rule Under Test activity FetchAvailableRole, which runs the decision table with Workexp = 7 and the decision table returns "Principal S.E". So our property assertion also passes and test case also runs successfully
In this way, we can use “Apply data transform “ options provided in the setup section of the test case rule form, to prepare the required environment where the actual Tested rule can run properly.
Other available options to create setup :
- Execute activity: we can run activities using this option.
- Load data page: mentioned data page will be loaded
- Load object: using this option we can load any object by passing the Class name and object keys as parameters.
- Create data object: we can create a data object (classes inherited from Data- classes) and load in on the desired page
- Create work object: we can even create work objects of any Work- class and load it on any desired page.
NB: During clean up, all the created Data/Work objects will be auto cleaned if we select “Clean up the test data at the end of run” in the Cleanup section.
This is not all. Say, to run our Rule, we need to supply a clipboard page to it ( This is quite possible as Rules like activities, data transform can refer to any page in memory ). Now, what if this required page is a very complex page with lots of embedded child data. To create this page using data transform/activities will be a hectic and erroneous task for the test writer. But, during actual rule execution(for ex. from our scenario example, during new hire case execution) this page gets created in memory. So, won't it be easy to just copy the entire page and put it in test case rule form, instead of creating it from scratch??
Yes. Definitely, this will be easy. And we have provided support to that as well. The test writer can copy any page from any thread and save it in the test case. During test case execution, when setup happens, we load these pages in memory and they became available for actual Rule execution. We need to use “+Add data” Option in setup section and a model with entire available clipboard data appears. We can add pages that we want. Using this model we can also create pages. If a simple page we need to create with very few properties, using this model we can do that as well.
This ApplicationDetails page is copied from the “New-Hire” thread to test case ruleform. And will be available during Rule execution/assertion/post rule execution.
So, using these setup options, we can provide the required test data and any complex rule can be independently tested.
Keep up to date on this post and subscribe to comments
- PegaUnit- Activity Data Setup
- Issue creating a PegaUnit test for Data Transform with String as input
- Different results when running PegaUnit test for Data Page
- Cannot view the Test Cases when switching between PegaUnit testing and Automated Unit Testing
- Running all PegaUnit test cases with the Execute Tests service in PEGA 7.1.x