Testing Microflows with the Unit Testing Module

Last modified: November 5, 2024

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

Setting up the Unit Testing Module

To set up the unit testing module and run the example tests, follow these steps:

  1. Create a new app.

  2. Download and install the Unit Testing module.

    For more information, see How to Use Marketplace Content.

  3. In the App Explorer, expand the App {App name} node, and then click Settings.

  4. On the Runtime tab of the App settings dialog box, select the After startup microflow.

    Selecting the After startup microflow
  5. Click Marketplace modules > UnitTesting > _USE ME > Microflows > Startup.

  6. Click Select and then OK.

  7. In the App Explorer, under the App {App name} node, click Navigation.

  8. In the Menu section, click New item.

  9. In the Caption field, enter UnitTestOverview.

  10. In the On click field, select Call a microflow, and then select the UnitTestOverview microflow.

    Selecting the UnitTestOverview microflow
  11. Click OK.

  12. Run the app locally.

  13. Go to http://localhost:8080/index.html.

  14. In the navigation pane, click UnitTestOverview.

    The app shows the UnitTesting page, as in the following image:

    A view of the UnitTesting page and default tests

    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.

  15. Ensure that the Rollback microflow tests after execution checkbox is selected.

  16. 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.

    An example of a passed test case
  17. 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:

  1. Create a new enumeration with the following properties:

    • NameLevel
    • Enumeration valuesJunior, Medior, and Senior.
    Enumeration with three values
  2. Open the domain model of MyFirstModule.

  3. Create a new entity with the following properties:

    • NameEmployee
    • AttributesName (of the String type) and Level ( of the Enumeration > Level type).
    A view of the Employee entity used by the sample microflow
  4. 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
    A view of the sample Promote microflow

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:

  1. In the left-side navigation pane, right-click on MyFirstModule, and then click Add folder.

  2. In the Name field, enter UnitTests.

  3. Right-click the UnitTests folder, and then click Add microflow.

  4. In the Name field, enter Test_PromoteEmployeeToMedior.

  5. In the Test_PromoteEmployeeToMedior microflow, add a new Create object activity for the Employee entity.

  6. In the Commit section of the activity, select Yes.

  7. In the Member section of the activity, add a new member with the following properties:

    • Name‘John’
    • Member typeString (200)
  8. In the Member section of the activity, add another new member with the following properties:

    • LevelMyFirstModule.Level.Junior
    • Member typeEnumeration ‘Level’
  9. In the Test_PromoteEmployeeToMedior microflow, call the Promote microflow by adding a Microflow Call activity with the following properties:

    • MicroflowMyFirstModule.Promote
    • ParameterEmployee
    • Argument$NewEmployee
    A microflow call activity with Employee as the parameter
  10. To test if the employee is promoted to the right level, add a Microflow Call activity with the following properties:

    • MicroflowUnitTesting.AssertTrue1
    • ParameterValueToAssert
    • Argument$NewEmployee/Level = MyFirstModule.Level.Medior
  11. Right-click the AssertTrue1 activity that you created.

  12. Select Edit caption, and then enter Promoted to Medior? for the new caption.

  13. To provide more information about test results for failed tests, call the ReportStep microflow by doing the following steps:

    1. Add a new Microflow call activity between Create Employee and Promote
    2. Select UnitTesting.ReportStep as the microflow.
    3. Set the argument of the Message parameter to string ‘Employee created’.
    4. Add a new Microflow call activity between Promote and Promoted to Medior?.
    5. Select UnitTesting.ReportStep as the microflow.
    6. Set the argument of the Message parameter to string ‘Employee promoted’.
  14. Connect the activities, as shown in the following figure:

    A view of the sample unit test microflow
  15. 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
  16. 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:
      • CaptionPromoted to Senior?
      • Argument$NewEmployee/Level = MyFirstModule.Level.Senior
  17. 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:
      • CaptionEmployee still Senior?
      • Argument$NewEmployee/Level = MyFirstModule.Level.Senior
  18. Run the app locally and view it.

  19. In the left-side navigation pane, click UnitTestOverview > MyFirstModule.

  20. Verify that the Rollback microflow tests after execution checkbox is selected.

  21. Click Run all module tests and verify that all the test cases pass.

Read More