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.2.0 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 Unit Testing > _USE ME > Microflows > Startup, and then click Select.
-
Click 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 parameters:
- Name – Level
- Enumeration values – Junior, Medior, and Senior.
-
Open the domain model of MyFirstModule.
-
Create a new entity with the following parameters:
- Name – Employee
- Attributes – Name (of the String type) and Level ( of the Enumeration > Level type).
-
Create a microflow called Promote that changes the Level parameter for the Employee entity based on the enumeration.
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, 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_PromoteEmployeeToJunior.
-
In the Test_PromoteEmployeeToJunior microflow, add Employee as an input parameter.
-
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 parameters:
- Name – John
- Member type – String
-
In the Test_PromoteEmployeeToJunior microflow, call the Promote microflow with the Employee object by adding an activity with the following parameters:
- Type – Microflow call
- Microflow – MyFirstModule.Promote
- Parameter – Employee
- Argument –$NewEmployee
-
Promote the employee to the right level by using an activity with the following parameters:
- Type – Microflow call
- Microflow – UnitTesting.AssertTrue1
- Parameter – ValueToAssert
- Argument –$NewEmployee/Level = MyFirstModule.Level.Junior
-
Right-click the AssertTrue1 activity that you created.
-
Select Edit caption, and then enter Promoted to Junior? for the new caption.
-
To provide more information about test results for failed tests, call the ReportStep sub-microflow by doing the following steps:
- Add a new activity of the Microflow call type between Create Employee and Promote
- Select UnitTesting.ReportStep as the microflow.
- Set the argument of the Message parameter to Employee created.
- Add a new activity of the Microflow call type between Promote and Promoted to Junior?.
- Select UnitTesting.ReportStep as the microflow.
- Set the argument of the Message parameter to Employee promoted.
-
Double-click the End event, and then specify the following properties:
- Type – Boolean
- Return value – true
-
Connect the activities, as shown in the following figure:
-
In the left-side navigation pane, in the UnitTests folder, duplicate the Test_PromoteEmployeeToJunior three times, and then rename the new microflows in the following way:
- Test_PromoteEmployeeToMedior
- Test_PromoteEmployeeToSenior
- Test_EmployeeStillSenior
-
In each of the new microflows, edit the Promoted to Junior? activity to reflect the employee level for that microflow. For example, in the Test_PromoteEmployeeToMedior microflow, set the following values for the activity:
- Caption - Promoted to Medior?
- Argument –$NewEmployee/Level = MyFirstModule.Level.Medior
-
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.