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.
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.
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.