Testing Microflows with the Unit Testing Module
Introduction
Verify that your microflow works as expected by creating unit tests with the Unit Testing module.
The Unit Testing module provides a user-friendly interface to manage and run unit tests that are created by using microflows, as well as unit tests that are by created using JUnit.
This how-to teaches you how to do the following:
- Set up the Unit Testing module
- Unit-test a microflow
Prerequisites
Before starting this how-to, make sure you have completed the following prerequisites:
-
Download Mendix Studio Pro
-
Review the Marketplace components used in this how-to:
Component Version Used in This How-to Unit Testing 9.5.1 All the images, names, and steps in this how-to are based on the Marketplace component versions listed above. When using later versions of this content, images or names on your screen may be different than shown in this how-to.
Setting up the Unit Testing Module
To set up the unit testing module and run the example tests, follow these steps:
-
Create a new app.
-
Download and install the Unit Testing module.
For more information, see How to Use Marketplace Content.
-
In the App Explorer, expand the App {App name} node, and then click Settings.
-
On the Runtime tab of the App settings dialog box, select the After startup microflow.
-
Click Marketplace modules > UnitTesting > _USE ME > Microflows > Startup.
-
Click Select and then OK.
-
In the App Explorer, under the App {App name} node, click Navigation.
-
In the Menu section, click New item.
-
In the Caption field, enter UnitTestOverview.
-
In the On click field, select Call a microflow, and then select the UnitTestOverview microflow.
-
Click OK.
-
Run the app locally.
-
Go to
http://localhost:8080/index.html
. -
In the navigation pane, click UnitTestOverview.
The app shows the UnitTesting page, as in the following image:
You can use this page to execute unit tests, or to reset the test status back to not executed. Use the left-side navigation pane to select a module that contains unit tests. In this scenario, UnitTesting is the only module that contains unit tests.
-
Ensure that the Rollback microflow tests after execution checkbox is selected.
If the checkbox is cleared, all changes made by the microflows that you test are saved to the database. This can result in populating the database with unwanted test data. As a best practice, do not clear the checkbox unless it is required by your specific use case. -
Validate that the Unit Testing module is correctly set up by running the UnitTesting.Test_ValidUnitTest test.
The color of the test case changes to red if the test fails, and to green if it passes.
-
To view detailed test results, in the UnitTesting.Test_ValidUnitTest row, click Details.
Unit-testing a Microflow
In this section, you will learn how to create and run a microflow unit test.
Creating a Sample Microflow for Testing
For the purpose of this how-to, create a sample microflow that you can then test with unit testing. In a real-life scenario, the steps below may be different, depending on the microflow that you want to create.
To create a sample microflow for testing, follow these steps:
-
Create a new enumeration with the following properties:
- Name – Level
- Enumeration values – Junior, Medior, and Senior.
-
Open the domain model of MyFirstModule.
-
Create a new entity with the following properties:
- Name – Employee
- Attributes – Name (of the String type) and Level ( of the Enumeration > Level type).
-
Create a microflow called Promote that changes the Level attribute of the Employee entity based on the enumeration value:
- For the Change Employee’s level to Medior activity, set the Level value to MyFirstModule.Level.Medior
- For the Change Employee’s level to Senior activity, set the Level value to MyFirstModule.Level.Senior
Creating a Unit Test Microflow
This section describes how to create a microflow test for the sample microflow described in the Creating a Sample Microflow for Testing section above. In a real-life scenario, the steps below may be different, depending on the microflow that you want to test. If you need to adapt the microflow test to your requirements, bear in mind the following considerations:
- The test microflow name must start with Test_ or UT_ (case-insensitive), for example Test_RegisterTrainee.
- The microflow should have no input arguments.
- The result type must be set to one of the following types:
- Boolean – For this result type, a true result means that the test succeeded, while a false result means that the test failed.
- String – For this result type, any non-empty string indicates a failed test.
- As a best practice, do not test every microflow in your application. Instead, test the most used or most complex microflows.
- You can create a Setup and TearDown microflow in each module. The Setup microflow is invoked once before each test run, and the TearDown microflow is invoked once after each test run, regardless of whether the test run consists of one or multiple unit tests.
To create a sample test microflow, follow these steps:
-
In the left-side navigation pane, right-click on MyFirstModule, and then click Add folder.
-
In the Name field, enter UnitTests.
-
Right-click the UnitTests folder, and then click Add microflow.
-
In the Name field, enter Test_PromoteEmployeeToMedior.
-
In the Test_PromoteEmployeeToMedior microflow, add a new Create object activity for the Employee entity.
-
In the Commit section of the activity, select Yes.
-
In the Member section of the activity, add a new member with the following properties:
- Name – ‘John’
- Member type – String (200)
-
In the Member section of the activity, add another new member with the following properties:
- Level – MyFirstModule.Level.Junior
- Member type – Enumeration ‘Level’
-
In the Test_PromoteEmployeeToMedior microflow, call the Promote microflow by adding a Microflow Call activity with the following properties:
- Microflow – MyFirstModule.Promote
- Parameter – Employee
- Argument –$NewEmployee
-
To test if the employee is promoted to the right level, add a Microflow Call activity with the following properties:
- Microflow – UnitTesting.AssertTrue1
- Parameter – ValueToAssert
- Argument –$NewEmployee/Level = MyFirstModule.Level.Medior
-
Right-click the AssertTrue1 activity that you created.
-
Select Edit caption, and then enter Promoted to Medior? for the new caption.
-
To provide more information about test results for failed tests, call the ReportStep microflow by doing the following steps:
- Add a new Microflow call activity between Create Employee and Promote
- Select UnitTesting.ReportStep as the microflow.
- Set the argument of the Message parameter to string ‘Employee created’.
- Add a new Microflow call activity between Promote and Promoted to Medior?.
- Select UnitTesting.ReportStep as the microflow.
- Set the argument of the Message parameter to string ‘Employee promoted’.
-
Connect the activities, as shown in the following figure:
-
In the left-side navigation pane, in the UnitTests folder, duplicate the Test_PromoteEmployeeToMedior twice, and then rename the new microflows in the following way:
- Test_PromoteEmployeeToSenior
- Test_EmployeeStillSenior
-
In each of the new microflows, make edits to reflect the employee level for that microflow. In the Test_PromoteEmployeeToSenior microflow:
- Set the value of the Level member to MyFirstModule.Level.Medior for the Create Employee activity.
- Set the following values for the Promoted to Medior? activity:
- Caption – Promoted to Senior?
- Argument – $NewEmployee/Level = MyFirstModule.Level.Senior
-
In the Test_EmployeeStillSenior microflow:
- Set the value of the Level member to MyFirstModule.Level.Senior for the Create Employee activity.
- Set the following values for the Promoted to Medior? activity:
- Caption – Employee still Senior?
- Argument – $NewEmployee/Level = MyFirstModule.Level.Senior
-
Run the app locally and view it.
-
In the left-side navigation pane, click UnitTestOverview > MyFirstModule.
-
Verify that the Rollback microflow tests after execution checkbox is selected.
-
Click Run all module tests and verify that all the test cases pass.