gradle beforeSuite {}& afterSuite {}多次执行

时间:2018-02-01 01:34:37

标签: testing gradle build.gradle

我今天遇到了一个与Gradle中的beforeSuite{}afterSuite{}有关的问题。我在gradle 4.1中为我的测试任务添加了beforeSuite{}afterSuite{},但每次执行测试时,它都会多次运行闭包。我想我已经使用-debug缩小了为单个任务生成的多个任务操作。

的build.gradle:

task unzipfirefoxDriver(type: Copy) {
    //https://github.com/mozilla/geckodriver/releases
    def outputDir = file("$buildDir/webdriver/geckodriver")
    outputs.dir(outputDir)
    if (OperatingSystem.current().isWindows()) {
        from(zipTree("drivers/geckodriver-${gechoVersion}-win64.zip"))
    } else {
        from(tarTree("drivers/geckodriver-${gechoVersion}-macos.tar.gz"))
    }
    into(outputDir)
    def geckodriverFilename = OperatingSystem.current().isWindows() ? "geckodriver.exe" : "geckodriver"
    map.put("firefox", new File(outputs.files.singleFile, geckodriverFilename))

}

task firefoxTest(type: Test) {
    testLogging {
        events 'started', 'passed'
    }
    reports {
        html.destination = reporting.file("$name/tests")
        junitXml.destination = file("$buildDir/test-results/$name")
    }
    beforeSuite { desc ->
        println "Automation has Started"
    }
    afterSuite { desc, result ->
        println "Automation has Finished - Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, 
${result.failedTestCount} failures, ${result.skippedTestCount} skipped)"
    }
    outputs.upToDateWhen { false }
    systemProperty("webdriver.gecko.driver", map."firefox".absolutePath)
    systemProperty("geb.env", "firefox")
}
firefoxTest.dependsOn unzipfirefoxDriver

调试信息:

7:26:44.236 [INFO] [org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter] Executing task ':firefoxTest' (up-to-date check took 0.138 secs) due to:
Task.upToDateWhen is false.
17:26:44.237 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter] Executing actions for task ':firefoxTest'.
17:26:44.237 [DEBUG] [org.gradle.internal.progress.DefaultBuildOperationExecutor] Build operation 'Execute task action 1/5 for :firefoxTest' started
17:26:44.237 [DEBUG] [org.gradle.internal.progress.DefaultBuildOperationExecutor] Completing Build operation 'Execute task action 1/5 for :firefoxTest'
17:26:44.237 [DEBUG] [org.gradle.internal.progress.DefaultBuildOperationExecutor] Build operation 'Execute task action 2/5 for :firefoxTest' started
17:26:44.237 [DEBUG] [org.gradle.internal.progress.DefaultBuildOperationExecutor] Completing Build operation 'Execute task action 2/5 for :firefoxTest'
17:26:44.237 [DEBUG] [org.gradle.internal.progress.DefaultBuildOperationExecutor] Build operation 'Execute task action 3/5 for :firefoxTest' started
17:26:44.238 [DEBUG] [org.gradle.internal.progress.DefaultBuildOperationExecutor] Completing Build operation 'Execute task action 3/5 for :firefoxTest'
17:26:44.238 [DEBUG] [org.gradle.internal.progress.DefaultBuildOperationExecutor] Build operation 'Execute task action 4/5 for :firefoxTest' started
17:26:44.238 [DEBUG] [org.gradle.internal.progress.DefaultBuildOperationExecutor] Completing Build operation 'Execute task action 4/5 for :firefoxTest'
17:26:44.238 [DEBUG] [org.gradle.internal.progress.DefaultBuildOperationExecutor] Build operation 'Execute task action 5/5 for :firefoxTest' started
17:26:44.239 [DEBUG] [org.gradle.api.internal.file.delete.Deleter] Deleting ..../test-results/firefoxTest/binary
17:26:44.242 [DEBUG] [org.gradle.internal.progress.DefaultBuildOperationExecutor] Build operation 'Resolve files of :testRuntimeClasspath' started
17:26:44.242 [DEBUG] [org.gradle.internal.progress.DefaultBuildOperationExecutor] Completing Build operation 'Resolve files of :testRuntimeClasspath'
17:26:44.242 [QUIET] [system.out] Automation has Started
17:26:44.243 [DEBUG] [TestEventLogger] 
17:26:44.243 [DEBUG] [TestEventLogger] Gradle Test Run :firefoxTest STARTED

终端输出:

> Task :firefoxTest
Automation has Started
Automation has Started
Automation has Started

com.example.ReviewWidgetSpec > test 1 STARTED

com.example.ReviewWidgetSpec > test 1 PASSED

com.example.ReviewWidgetSpec > test 2 STARTED

com.example.ReviewWidgetSpec > test 2 PASSED

com.example.ReviewWidgetSpec > test 3 STARTED

com.example.ReviewWidgetSpec > test 3 PASSED
Automation has Finished - Results: SUCCESS (3 tests, 3 successes, 0 failures, 0 skipped)
Automation has Started
Automation has Finished - Results: SUCCESS (0 tests, 0 successes, 0 failures, 0 skipped)
Automation has Started
Automation has Finished - Results: SUCCESS (0 tests, 0 successes, 0 failures, 0 skipped)
Automation has Finished - Results: SUCCESS (3 tests, 3 successes, 0 failures, 0 skipped)
Automation has Finished - Results: SUCCESS (3 tests, 3 successes, 0 failures, 0 skipped)


BUILD SUCCESSFUL in 21s
4 actionable tasks: 1 executed, 3 up-to-date

这是我的第一篇文章,所以我希望我得到一些可能需要的东西来帮助我弄清楚这个问题。

1 个答案:

答案 0 :(得分:0)

在gradle中为beforeSuite和afterSuite调用多个套件。修改您的闭包以检查父级为null以查找根套件:

beforeSuite { descriptor, result ->
    if(descriptor.parent == null){
        //your logic
    }
}

https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html#org.gradle.api.tasks.testing.Test:beforeSuite(groovy.lang.Closure)