JUnit Integration (custom)

This custom report endpoint for JUnit exists as an alternative for posting test results to Zephyr Squad if you are using JUnit 4. If you’re using  JUnit 5 or later, we suggest you use the instructions 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 Squaddevelopment 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 Squad Library to Receive JUnit Test Results

  1. Download the SmartBear/zephyr-squad-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 Squad 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 Squad 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 Squad 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 Squad

Run the tests with this command:

mvn test

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

To send the test results to Jira, you'll need to upload the results file generated after running the automated tests using the API.

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

Publication date: