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

Testing gradle beforeSuite{}&;afterSuite{}执行多次,testing,gradle,build.gradle,Testing,Gradle,Build.gradle,我今天遇到了一个问题,与Gradle中的beforeSuite{}和afterSuite{}有关。我在Gradle4.1中的测试任务中添加了一个beforeSuite{}和一个beforeSuite{},但是每次执行测试时,它都会多次运行闭包。我想我已经使用-debug将它缩小到为单个任务生成多个任务操作 build.gradle: task unzipfirefoxDriver(type: Copy) { //https://github.com/mozilla/geckodriver

我今天遇到了一个问题,与Gradle中的
beforeSuite{}
afterSuite{}
有关。我在Gradle4.1中的测试任务中添加了一个
beforeSuite{}
和一个
beforeSuite{}
,但是每次执行测试时,它都会多次运行闭包。我想我已经使用
-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

这是我的第一篇文章,所以我希望我得到了人们可能需要的一切来帮助我解决这个问题

对于gradle中的多个套件,调用beforeSuite和afterSuite。修改闭包以检查父项是否为空,以查找根套件:

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

非常感谢!这是我需要的缺失的环节。我还缩短了if以使用
!描述父项