JUnit Integration (custom)

Applies to Zephyr Scale Cloud, last modified on September 24, 2021.

This custom report endpoint for JUnit exists as an alternative for posting test results to Zephyr Scale if you are using JUnit 4. If you’re using  JUnit 5 or later, we suggest you use the endpoint documented here.

The JUnit custom endpoint can come in handy if you'd like to use JUnit with annotations or write your own test reporter for any language and use this format for the output file.

Annotating JUnit tests in a Zephyr Scale development project enables results to be sent from JUnit directly to Jira. All it takes is adding a Java dependency to your project.

Configure the Zephyr Scale Library to Receive JUnit Test Results

  1. Download the SmartBear/zephyr-scale-junit-integration repository from our GitHub.

  2. Add the automation library to your project by following the instructions in the README section.
    This enables you to annotate your JUnit tests with a Zephyr Scale key or name and generate a custom report when you run tests.

To send the test results to Jira, you'll need to include a pre-made script (see the task below).

Annotating JUnit Tests

  1. Add the dependency to your .pom file for the Zephyr Scale JUnit library:

    <dependencies>
        <dependency>
            <groupId>com.smartbear</groupId>
            <artifactId>zephyrscale-junit-integration</artifactId>
            <version>2.0.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
  2. Register the Zephyr Scale JUnit listener: this will generate the results file with test case keys.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
                <configuration>
                    <properties>
                        <property>
                            <name>listener</name>
                            <value>com.smartbear.zephyrscale.junit.ExecutionListener</value>
                        </property>
                    </properties>
                </configuration>
            </plugin>
        </plugins>
    </build>
  3. Add annotations to your JUnit 4 tests for the test case key or name:

    public class CalculatorSumTest {
    
        @Test
        @TestCase(key = "JQA-T1")
        public void sumTwoNumbersAndPass() {
            Calculator calculator = new Calculator();
            assertEquals(1, calculator.sum(1, 2));
        }
    
        @Test
        @TestCase(key = "JQA-T2")
        public void sumTwoNumbersAndFail() {
            Calculator calculator = new Calculator();
            assertNotEquals(2, calculator.sum(1, 2));
        }
    
        @Test
        public void notMappedToTestCaseAndPass() {
            Calculator calculator = new Calculator();
            assertEquals(1, calculator.sum(1, 2));
        }
    
        @Test
        @TestCase(name = "Mapped to Test Case Name and Pass")
        public void mappedToTestCaseNameAndPass() {
            Calculator calculator = new Calculator();
            assertEquals(1, calculator.sum(1, 2));
        }
    
    }

The rules for matching a test method to a test case are:

  • Try to match by using the test case key or name from the test method annotation @TestCase.

  • If no test case is matched, then try to match a test case by using the method full qualified name.

  • If no test case is matched, then:

    • If the parameter autoCreateTestCases is true, create the test case and create a new test execution for it.

    • If the parameter autoCreateTestCases is false or not present, return an error - no test cases have been matched.

Uploading results to Zephyr Scale

Run the tests with this command:

mvn test

The Zephyr Scale test-execution-result file generates in the same execution folder.

Finally, if you want to see an example of the Zephyr Scale integration with JUnit 4 in action, we've got you covered.

Highlight search results