MxBlog

Unit testing Mendix applications

The UnitTesting module in the Mendix app store enables you to easily write and run Unit tests for your Mendix microflows. This blog post will show you how to install and use the module.

Include the Unit testing module

First you need add and configure the unit testing module:

  1. From your Mendix modeler, find the UnitTesting module in the appstore, download it into your project.
  2. Find the community commons module and download it into your project.
  3. Update the layouts of the pages in the UnitTesting module, to use the layouts of your application.
  4. Assign the Tester role your the appropriate application roles and users.
  5. Add the UnitTestOverview page to your application navigation.

Create tests

The UnitTesting module can be used to run Java junit test classes, but usually you'll write your testing as Microflows.

The following microflow is used to validate an entity before it's saved to the database. We'll create some tests to validate that the microflow is working properly.

Save product microflow

The project will look like this, after we have added the required modules and test microflows.

Project

In every Mendix module you can provide a setup and teardown microflow that will be executed before and after every test. In this example only setup microflow is used to delete old test data, the teardown microflow is empty.

Setup microflow

The following microflow tests the weight attribute is properly validated by the save product microflow. The microflow should check that the weight property is not empty.

Product needs weight unit test microflow

Some basic requirements for unit test micrflows:

  1. Name of the microflow should start with "Test".
  2. Microflow should not have any parameters.
  3. The microflow should either return a boolean or a string:

    1. In case of a boolean, true means success, false means that the test failed.
    2. In case of a string, any non empty string indicates a failed test.

Run tests

To run the tests, start your application and login with a user that has the tester role assigned. You should be able to open the Test Suite Overview page. Here you can add modules which contain tests. The UnitTesting module will find all your tests. If you add new tests you can use the Find tests button to rescan your module.

The following screenshot shows the result of running the three unit tests in the Products module.

Test results

For every test you can look at some detail information. For failed tests, the last step information can be very useful. You can provide this information in your microflow by calling the ReportStep sub-microflow.

Test detail results

Creating unit tests will help you maintain productivity on projects with many iterations and releases. Make sure you have unit tests for most of the important microflows and functionality. Usually it's not really useful to test everything though.