testing - gradle beforeSuite {}和afterSuite {}执行多次

标签 testing gradle build.gradle

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

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

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

关于testing - gradle beforeSuite {}和afterSuite {}执行多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48554060/

相关文章:

android - 使用 Gradle 运行命令行并保存输出结果

javascript - 在单元测试中,如何测试具有对象参数的函数?

java - 使用 gradle 从 java 项目中删除未使用的导入

android - Gluon Mobile:使用ExoPlayer

macos - Android Studio OS X : Gradle seems to have no connection to jcenter (?)

android-gradle-plugin - 安卓 : Didn't find class "Picasso" on path: DexPathList (Failed resolution of: Picasso)

android - 使用 Proguard 时出错

testing - Camel 核磁共振测试 : where is AbstractComponentTest?

testing - 移动和桌面应用程序测试的差异

unit-testing - 在 IntelliJ 中运行单元和集成测试