JUnit5 is one of the most popular unit-testing frameworks in the Java ecosystem and, with the right libraries, it can be used for other test levels such as integration or even system tests with UI interactions. In this tutorial, we will explore how to integrate the test results generated by a JUnit5 automated test run within TestRail using the TestRail CLI. This will allow you to centralize your automated test results and take advantage of all the analytical and reporting capabilities TestRail provides.
Overview
Throughout this tutorial, we will be using a sample project to guide you through the process of setting up a JUnit5 automated tests project that is compatible with the TestRail CLI and uploading the generated test results.
After reading through this tutorial, you will be able to:
- Execute tests from a simple JUnit5 project
- Install the TestRail CLI
- Configure your TestRail instance
- Run the CLI
- See your test cases and test results on TestRail
Prerequisites
To be able to install and run a JUnit5 Java tests project, all you’ll need is to install the Java SE Development Kit. In the context of our sample project, you will also have to install Maven, which will be used for dependency management and running the project stages.
You will also need Python to install and run the TestRail CLI that will be used to import the test results to TestRail.
Prerequisite | Description |
---|---|
Java SE Development Kit 11 | Download the version for your operating system and follow the install wizard instructions. To make sure the install was successful, try executing the command java --version and javac --version from your command line and they should output the installed version. |
Maven3 |
Download the version for your operating system and unzip it on your preferred location. Add the location of the bin folder to your PATH environment variable. To make sure the install was successful, try executing |
Python 3.10.4 |
Download the version for your operating system and follow the install wizard instructions. To make sure the install was successful, try executing the commands |
Installing the sample project
Let’s start by fetching the sample project code and installing the required dependencies.
- Clone or download the sample project
- Open your command prompt on the project root folder and execute the command below
$ mvn clean compile
Requirement | Description |
---|---|
(dependency) junit-jupiter-engine | Test automation framework which provides a TestEngine for running Jupiter based tests on the platform |
(dependency) junit-jupiter-params |
Extension to enable writing parameterized tests |
(dependency) testrail-junit-extensions | Extension to enable setting custom report properties (i.e.: to add attachments or any other metadata) |
(plugin) maven-surefire-plugin | Plugin which runs tests and generates JUnit style reports |
(plugin) maven-site-plugin |
Plugin to run the maven site goal |
Exploring the sample project
Use your favorite IDE to open the sample project and start digging into the test files. We’ve kept the automated tests code simple so that the focus of this tutorial remains on how to import the execution results. These tests consist of simple mathematical validations as you can see in the example below.
package com.idera.testrail.tests;
import com.testrail.junit.customjunitxml.TestRailTestReporter;
import com.testrail.junit.customjunitxml.TestRailTestReporterParameterResolver;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SumTests {
@Test
@DisplayName("Add Two Numbers")
void AddTwoNumbers() {
assertEquals(3, 1+2, "1 + 2 should equal 3");
}
@Test
@DisplayName("Add Two Numbers With Decimals")
void AddTwoNumbersWithDecimals(TestRailTestReporter customReporter) {
// Sets the "testrail_attachment" property on the report with a path to a file to be uploaded with the test result
customReporter.setProperty("testrail_attachment", "sample_reports/testrail.jpg");
assertEquals(3, 1.5+1.4, "1.5+1.4 should equal 3");
}
@Nested
class AddMoreNumbersTests {
@Test
@DisplayName("Add Three Numbers")
void AddThreeNumbers() {
assertEquals(3, 1+1+1, "1+1+1 should equal 3");
}
}
}
Attaching files to the report
Using the testrail-junit-extensions, you can add properties to the JUnit report in your code. This allows you to set, i.e., testrail_attachment properties with paths to files you want to be attached to your test result in TestRail when uploading the report through the TestRail CLI.
To be able to use the capabilities provided by the testrail-junit-extensions test execution listener, you first need to register it. This is done in the file org.junit.platform.launcher.TestExecutionListener
, by setting the namespace of the testrail-junit-extensions listener in the code snippet below. This file is already set up in the project, so you do not have to concern yourself with it.
com.testrail.junit.customjunitxml.EnhancedLegacyXmlReportGeneratingListener
Then in your test classes, you simply extend them with the annotation on the code snippet below and add the TestRailTestReporter
argument to your test methods. Setting properties is as simple as using the setProperty()
method with the desired key and value. In the snippet below you can see an example of setting an attachment to be sent to TestRail along with the test result.
@ExtendWith(TestRailTestReporterParameterResolver.class)
class SumTests {
@Test
@DisplayName("Add Two Numbers With Decimals")
void AddTwoNumbersWithDecimals(TestRailTestReporter customReporter) {
// Sets the "testrail_attachment" property on the report with a path to a file to be uploaded with the test result
customReporter.setProperty("testrail_attachment", "sample_reports/testrail.jpg");
assertEquals(3, 1.5+1.4, "1.5+1.4 should equal 3");
}
}
Executing the sample project
On the same command prompt, execute the following command to run the JUnit5 tests in the project and save the results in JUnit XML format.
$ mvn clean compile test
If the maven command was correctly executed, you should be able to see your test results on the target folder. It should contain an XML file named TEST-junit-jupiter.xml. This file will be parsed by the TestRail CLI to create the test run and upload your test results to TestRail on the next step.
<?xml version="1.0" encoding="UTF-8"?>
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report-3.0.xsd" version="3.0" name="com.idera.testrail.tests.SumTests$AddMoreNumbersTests" time="0.002" tests="3" errors="0" skipped="0" failures="1">
<properties>
<property name="sun.desktop" value="windows"/>
<property name="awt.toolkit" value="sun.awt.windows.WToolkit"/>
<property name="java.specification.version" value="11"/>
</properties>
<testcase name="AddTwoNumbersWithDecimals" classname="com.idera.testrail.tests.SumTests" time="0.003">
<properties>
<property name="test_summary" value="Add Two Numbers With Decimals"/>
<property name="testrail_attachment" value="sample_reports/testrail.jpg"/>
</properties>
<failure message="1.5+1.4 should equal 3 ==> expected: <3.0> but was: <2.9>" type="org.opentest4j.AssertionFailedError">
<![CDATA[org.opentest4j.AssertionFailedError: 1.5+1.4 should equal 3 ==> expected: <3.0> but was: <2.9>
at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)
at org.junit.jupiter.api.AssertionUtils.failNotEqual(AssertionUtils.java:62)
at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:70)
at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:925)
at com.idera.testrail.tests.SumTests.AddTwoNumbersWithDecimals(SumTests.java:20)
(...)
]]>
</failure>
</testcase>
<testcase name="AddTwoNumbers" classname="com.idera.testrail.tests.SumTests" time="0.002"/>
<testcase name="AddThreeNumbers" classname="com.idera.testrail.tests.SumTests$AddMoreNumbersTests" time="0.001"/>
</testsuite>
Importing results to TestRail
After the tests have been executed and the JUnit report files are generated, you can easily import your test results (and test cases!) to TestRail. This will bring visibility to your automated test runs and will enable you to look at the big picture of how you’re actually testing your app all within TestRail.
Installing the TestRail CLI
Given you already have Python installed on your machine, installing the TestRail CLI is as simple as executing the following command on your command line.
$ pip install trcli
Configuring TestRail
Secondly, you need to configure your TestRail instance according to the instructions below.
- Enable the TestRail API by going to Administration > Site Settings, click on the API tab, and checking the Enable API option.
- Create a Custom Field in order to map your automated test cases code to the actual TestRail cases. You can do so by going to Administration > Customizations and clicking Add Field. After you’ve reached the field creation screen, there are two requirements for this custom field:
- The System Name must be automation_id
- The Type must be Text
Sending results to TestRail
After you’re done installing the TestRail CLI and finished configuring your TestRail instance, you can upload your test results by simply using a one-liner such as the one below.
$ trcli -y \
> -h https://INSERT-INSTANCE-NAME.testrail.io \
> --project "My Project" \
> --username INSERT-EMAIL \
> --password INSERT-PASSWORD \
> parse_junit \
> --title "JUnit5 Automated Test Run" \
> -f "./target/TEST-junit-jupiter.xml"
Note that the file name after the -f option should match the path to your report file in case you change its default location. All others options should be according to your TestRail instance and project. You can check other command line options by checking the TestRail CLI README.md file on the project repository, the TRCLI documentation article, or the embedded CLI help through the commands below.
$ trcli --help
$ trcli parse_junit --help
Visualizing the results on TestRail
Now, if you go to the Test Cases page in your TestRail project, you’ll notice that the TestRail CLI automatically created the test cases that were on your test results report. Notice that it added a unique Automation ID by combining the classname
and name
attributes on each test on the JUnit report. This Automation ID is used to map the tests on your automation code base to the test cases on TestRail. This means that each time you run the TestRail CLI, it first attempts to match an existing test case on TestRail and only creates a new one in case there is no test case with that Automation ID.
If you change the name of your tests, the name of the file or its location, the Automation ID for those tests will change and they won’t be mapped to the existing test cases on TestRail.
On the Test Runs & Results page, we can see that a test run with the name JUnit5 Automated Test Run was created. By opening it we can dig further into the details of each automated test result and perform a high level analysis of why any test is failing since the error message provided by the test automation framework is also registered on the test result, as you can see on the image below.
What next?
Now that you have centralized your test results on TestRail, not only can you check the results of your automated test runs, along with the error messages for failed tests, but you can also aggregate both your manual and automated testing efforts on reports that show you the full test coverage surrounding your app and even track test automation progress. You can also report a bug directly from the automated test result to an issue tracker of your preference as you would do for your manual test results!
You can look into the TestRail’s Reports and Test Metrics video to learn about how you can leverage TestRail’s reporting capabilities.