Search code examples
androidkotlinjunitjunit5jira-xray

Custom TestExecutionListener in android Junit5


I'm trying to figure out how to customize the test case XML generated by Gradle. The goal is to add custom keys to the test cases so they can be identified in the modified testcase XML.

To do this, we need to:

  • Create a custom TestExecutionListener class
  • ./gradlew test is run, it should generate the custom XML in the build folder

However, the annotated tags under the tag are not being updated.

Has anyone successfully implemented this in an Android project? Thanks in advance!


Solution

  • Please have a look at this Gradle+Junit5 enhanced tutorial and corresponding code.

    You need to:

    • add the xray-junit-extensions as dependency in your build.gralde
    ...
    test {
        useJUnitPlatform()
        reports {
        // destination = file('build/test-results/folder')
            junitXml.required = true
            html.required = false
        }
        ignoreFailures = true
    }
     
    dependencies {
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
        testImplementation 'org.hamcrest:hamcrest-library:2.2'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
        testImplementation 'app.getxray:xray-junit-extensions:0.6.1'
    }
    ...
    
    • include
    • annotate your test methods with @Requirement and/or @XrayTest where appropriate (example)
        @Test
        @XrayTest(key = "CALC-739")
        @Requirement("CALC-740")
        public void CanAddNumbers()
        {
            assertThat(Calculator.add(1, 1), is(2));
            assertThat(Calculator.add(-1, 1), is(2));
        }
    
    • add the FQN of the class to a file named src/test/resources/META-INF /services/org.junit.platform.launcher.TestExecutionListener