Execute gradle test task if another test task fails?

Viewed 374

I am using TestNG and Gradle, what I am trying to achieve is, if the task that runs the tests fails, run another task which is also of type test which actually sends the test report. If all tests pass, don't do anything, I know about finalizedBy, but that sends the test report either way.

I tried something like this, but no luck.

task uiTest(type: Test) {
    useTestNG() {
        environment "DOMAIN", "${DOMAIN}"
        useDefaultListeners = true
        suites "src/test/resources/ui-tests/ThemeA/chrome67.xml"
    }
    reports {
        html {
            enabled true
        }
        reports.html.destination = "build/reports/TestReports/uiTestThemeA"
    }
    testLogging {
        showStandardStreams = true
        exceptionFormat "full"
        events = ["started", "passed", "skipped", "failed"] //, "standard_out", "standard_error"]
        showExceptions = true
        showStackTraces = true
    }
}

task testing(dependsOn: uiTest, type: Test) {
    boolean state = uiTestThemeA.state.executed
    if(!state) {
        println name
        useTestNG() {
            suites "src/test/resources/ui-tests/sendReport.xml"
        }
    }
}
1 Answers

If I understand you correctly, then the following should do what you need. Replace your testing task with the following one and configure your uiTest task to be finalized by the testing task:

task testing(type: Test) {
    onlyIf { uiTest.state.failure }
    useTestNG() {
        suites "src/test/resources/ui-tests/sendReport.xml"
    }
}

uiTest {
    finalizedBy testing
}

Some notes:

  • the onlyIf statement does the magic you are looking for
  • this assumes you execute uiTest
Related