TestNG rerun failed tests

I want to rerun TestNG failed tests, my build.gradle looks like follows:

apply plugin: 'eclipse'
apply plugin: 'java'
  dependencies {
  compile fileTree(dir: 'lib', include: '*.jar')
}
  test {
        useTestNG()
        ignoreFailures = true
 }
test.doLast {
    if (new File('build/reports/tests/testng-failed.xml').exists()) {
         useTestNG(){ suites "build/reports/tests/testng-failed.xml"}
    }
}
task wrapper(type: Wrapper) {
 gradleVersion = '1.0-milestone-8'
}

The problem is that the line useTestNG(){ suites “build/reports/tests/testng-failed.xml”} doesn’t do anything.

Hello Federico, the snippet

test {
        useTestNG()
        ignoreFailures = true
 }

configures your test task of type Test (http://gradle.org/docs/current/dsl/org.gradle.api.tasks.testing.Test.html)

useTestNG just configures the test task to use testng instead of junit. The action defined in your doLast closure is executed after your tests had run. useTestNG() has no effect here and does run your defined tests. I think you have to split the execution of the failed tests out of the original test task to a second task of type Test. Within this second test task. you can use the onlyIf to execute the task only if failed testng tests are reported. BTW. you shouldn’t reference files within your gradle build using “new File(
)” instead use the file() method. Otherwise you will run into problems when executing your build from another directory than the project root dir, which is a very common case when using a ci server.

regards, René

I tried to execute two test tasks

test {
        useTestNG()
        ignoreFailures = true
 }
  test2 {
    if (new File('build/reports/tests/testng-failed.xml').exists()) {
         useTestNG(){
      suites "build/reports/tests/testng-failed.xml"
        }
    }
}

I’m calling it this way, gradle -Dtest.single=Mytest clean test test2

But gives me this error: Could not find method test2() for arguments [build_gradle_d6125bea128226210db44ff8a79ab0b$_run_closure3@1d47f59]

The Java plugin creates and configures a ‘test’ task, but not a ‘test2’ task. If you want another test task (unrelated to a particular source set), you’ll have to create and configure it yourself.

ok thanks for your responses. But I’m new to gradle, can you give me an example of how can revolve/configure this?

Change it to "task test2(type: Test) << {
}

hello federico, the configuration for rerunning failing tests in a second test task can look like this:

test{
 useTestNG()
 ignoreFailures = true
 }
  task secondTry(type:Test){
  onlyIf{
   file("build/reports/tests/testng-failed.xml").exists()
   }
    dependsOn test
    testClassesDir = sourceSets.test.output.classesDir
  classpath = sourceSets.test.runtimeClasspath
  testSrcDirs = sourceSets.test.java.srcDirs as List
     testReportDir = file("$buildDir/reports/secondTry")
        useTestNG(){
  suites("build/reports/tests/testng-failed.xml")
  }
 }
   task allTests(dependsOn: tasks.withType(Test))

just run “gradle alltests” and if all tests pass on the first try, the secondTry task is skipped. If the test task has failed tests, the secondTry test task retries them.

Have a look at the DSL reference of the Test task for details (http://gradle.org/docs/current/dsl/org.gradle.api.tasks.testing.Test.html)

regards, René

1 Like

It was really helpful the code, thanks Rene!