Overview
Gradle is an open-source build tool that provides flexibility, and performance through task parallelization. Gradle is known in Java projects, as an alternative to Maven, but also supports other languages. Find out more about its features on Gradle's site.
Integration with Xray is possible using Xray APIs, namely the REST API for importing test results.
You can use this integration to upload test results to Xray, in one of the supported test reports/formats, so that you have visibility of your test results from your pipeline in Jira.
The examples detailed on this page are available in a GitHub repo. They are not exhaustive; please consider them just as a reference and feel free to adapt them to your needs.
How to use
The following examples were made using Gradle 7.4.2 and JDK 11.
In order to invoke the REST API, we'll use the curl
utility that is available on Unix, Linux, macOS, and other systems.
We don't need to install any specific Gradle plugin for this.
To import the results we use a custom Gradle task. We can then either invoke it by using gradle
or gradlew
(i.e., Gradle Wrapper).
Configuration
Our task will make use of several variables.
In Gradle, we can define some variables within the tasks themselves, and they will be instantiated during the configuration phase.
Another option is to use a gradle.properties
file, such as the following example.
Gradle will pick it and these variables will be available for the task(s) that we will use/implement.
Examples
JUnit4
In this example, the tests are implemented using Java + JUnit 4.13.x.
We use a configuration file to define the REST API specifics (the Xray API key, i.e., the client id and client secret pair) and some parameters to identify, for example, the target project and version/release of the SUT.
In this case, we're using the standard JUnit endpoint (more info on the endpoint and the supported parameters is available in Import Execution Results - REST v2).
We then generate a "standard" JUnit XML report and use a custom task to perform the REST API request that submits the results to Xray.
The following example shows a build.gradle
file with a custom task named importJUnitResultsToXrayCloud where we implement the logic to push the results to Xray.
The test
task uses the JUnit4 runner by declaring useJUnit().
To run the tests and import them to Xray we can run Gradle as usual and add the name of the task we created earlier.
gradle clean compileJava test importJUnitResultsToXrayCloud
In Xray, a Test Execution will be created accordingly.
JUnit5
In this example, the tests are implemented using Java + JUnit 5.8.x.
We use a configuration file to define the REST API specifics (the Xray API key, i.e., the client id and client secret pair) and some parameters to identify, for example, the target project and version/release of the SUT.
In this case, we're using the standard JUnit endpoint (more info on the endpoint and the supported parameters are available in Import Execution Results - REST v2).
We then generate a "standard" JUnit XML report and use a custom task to perform the REST API request that submits the results to Xray.
The following example shows a build.gradle
file with a custom task named importJUnitResultsToXrayCloud where we implement the logic to push the results to Xray.
The test
task uses the JUnit5 platform runner by declaring useJUnitPlatform().
To run the tests and import them to Xray we can run Gradle as usual and add the name of the task we created earlier.
gradle clean compileJava test importJUnitResultsToXrayCloud
In Xray, a Test Execution will be created accordingly.
JUnit5 with additional information
In this example, the tests are implemented using Java + JUnit 5.8.x.
In this case, we'll use the enhanced capabilities that Xray provides for JUnit allowing you to provide additional information during the execution of the tests, including screenshots and others; to do so, we'll use the xray-junit-extensions package.
We use a configuration file to define the REST API specifics (the Xray API key, i.e., the client id and client secret pair) and some parameters to identify, for example, the target project and version/release of the SUT.
In this case, we're using the standard JUnit endpoint (more info on the endpoint and the supported parameters is available in Import Execution Results - REST v2).
We then generate a "standard" JUnit XML report and use a custom task to perform the REST API request that submits the results to Xray.
The following example shows a build.gradle
file with a custom task named importJUnitResultsToXrayCloud where we implement the logic to push the results to Xray.
The test
task uses the JUnit5 platform runner by declaring useJUnitPlatform().
To run the tests and import them to Xray we can run Gradle as usual and add the name of the task we created earlier.
gradle clean compileJava test importJUnitResultsToXrayCloud
In Xray, a Test Execution will be created accordingly.
TestNG
In this example, the tests are implemented using Java + TestNG 7.6.x.
We use a configuration file to define the REST API specifics (the Xray API key, i.e., the client id and client secret pair) and some parameters to identify, for example, the target project and version/release of the SUT.
In this case, we're using the standard TestNG endpoint (more info on the endpoint and the supported parameters is available in Import Execution Results - REST v2).
We then generate a "standard" TestNG XML report and use a custom task to perform the REST API request that submits the results to Xray.
The following example shows a build.gradle
file with a custom task named importTestNGResultsToXrayCloud where we implement the logic to push the results to Xray.
The test
task uses the TestNG runner by declaring useTestNG().
To run the tests and import them to Xray we can run Gradle as usual and add the name of the task we created earlier.
gradle clean compileJava test importTestNGResultsToXrayCloud
In Xray, a Test Execution will be created accordingly.
TestNG with additional information
In this example the tests are implemented using Java + TestNG 7.6.x.
In this case, we'll use the enhanced capabilities that Xray provides for TestNG allowing you to provide additional information during the execution of the tests, including screenshots and others; to do so, we'll use the xray-testng-extensions package.
We use a configuration file to define the REST API specifics (the Xray API key, i.e., the client id and client secret pair) and some parameters to identify, for example, the target project and version/release of the SUT.
In this case, we're using the standard TestNG endpoint (more info on the endpoint and the supported parameters is available in Import Execution Results - REST v2).
We then generate a "standard" TestNG XML report and use a custom task to perform the REST API request that submits the results to Xray.
The following example shows a build.gradle
file with a custom task named importTestNGResultsToXrayCloud where we implement the logic to push the results to Xray.
In this case, we won't be using the built-in test
task; instead, we implement a custom one named testngTest
as we need to provide additional parameters to the XMLReporter class provided by TestNG that otherwise is not yet possible; this is what enables the feature of embedding additional attributes on the TestNG XML report that Xray can take advantage of.
To run the tests and import them to Xray we can run Gradle as usual and add the name of the task we created earlier.
gradle clean compileJava testngTest importTestNGResultsToXrayCloud
In Xray, a Test Execution will be created accordingly.
Tips
- To debug existing tasks, run your Gradle command with "–stacktrace"
- On the custom tasks that execute a command using exec, set
ignoreExitValue = false