How to re-run only failed JUnit test classes using Gradle?

后端 未结 1 1373
后悔当初
后悔当初 2020-12-29 13:10

Inspired by this neat TestNG task, and this SO question I thought I\'d whip up something quick for re-running of only failed JUnit tests from Gradle.

But after searc

相关标签:
1条回答
  • 2020-12-29 13:31

    Here is one way to do it. The full file will be listed at the end, and is available here.

    Part one is to write a small file (called failures) for every failed test:

    test {
        // `failures` is defined elsewhere, see below
        afterTest { desc, result -> 
            if ("FAILURE" == result.resultType as String) {
                failures.withWriterAppend { 
                    it.write("${desc.className},${desc.name}\n")
                }
            }
        }
    }
    

    In part two, we use a test filter (doc here) to restrict the tests to any that are present in the failures file:

    def failures = new File("${projectDir}/failures.log")
    def failedTests = [] 
    if (failures.exists()) {
        failures.eachLine { line ->
            def tokens = line.split(",")
            failedTests << tokens[0] 
        }
    }
    failures.delete()
    
    test {
        filter {
            failedTests.each { 
                includeTestsMatching "${it}"
            }
        }
        // ...
    }
    

    The full file is:

    apply plugin: 'java'
    
    repositories {
        jcenter()
    }
    
    dependencies {
        testCompile('junit:junit:4.12')
    }   
    
    def failures = new File("${projectDir}/failures.log")
    def failedTests = [] 
    if (failures.exists()) {
        failures.eachLine { line ->
            def tokens = line.split(",")
            failedTests << tokens[0] 
        }
    }
    failures.delete()
    
    test {
        filter {
            failedTests.each { 
                includeTestsMatching "${it}"
            }
        }
    
        afterTest { desc, result -> 
            if ("FAILURE" == result.resultType as String) {
                failures.withWriterAppend { 
                    it.write("${desc.className},${desc.name}\n")
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题