【问题标题】:Running specific tests using gradle over multiple browsers在多个浏览器上使用 gradle 运行特定测试
【发布时间】:2018-10-05 18:23:05
【问题描述】:

我正在使用 Geb/Spock 进行自动化测试。我使用 Gradle 作为我的构建工具。

我想调用不同的 gradle 任务来构建和运行特定规范(测试)或一套规范。

我对 gradle 构建生命周期的了解不够,无法完全理解这里发生了什么:https://github.com/geb/geb-example-gradle/blob/master/build.gradle

plugins {
    id "idea"
    id "groovy"
    id "com.energizedwork.webdriver-binaries" version "1.4"
    id "com.energizedwork.idea-base" version "1.4"
}

ext {
    // The drivers we want to use
    drivers = ["firefox", "chrome", "chromeHeadless"]

    ext {
        groovyVersion = '2.4.12'
        gebVersion = '2.2'
        seleniumVersion = '3.6.0'
        chromeDriverVersion = '2.32'
        geckoDriverVersion = '0.18.0'
    }
}

repositories {
    mavenCentral()
}

dependencies {
    // If using Spock, need to depend on geb-spock
    testCompile "org.gebish:geb-spock:$gebVersion"
    testCompile("org.spockframework:spock-core:1.1-groovy-2.4") {
        exclude group: "org.codehaus.groovy"
    }
    testCompile "org.codehaus.groovy:groovy-all:$groovyVersion"

    // If using JUnit, need to depend on geb-junit (3 or 4)
    testCompile "org.gebish:geb-junit4:$gebVersion"

    // Drivers
    testCompile "org.seleniumhq.selenium:selenium-chrome-driver:$seleniumVersion"
    testCompile "org.seleniumhq.selenium:selenium-firefox-driver:$seleniumVersion"
}

webdriverBinaries {
    chromedriver chromeDriverVersion
    geckodriver geckoDriverVersion
}

drivers.each { driver ->
    task "${driver}Test"(type: Test) {
        group JavaBasePlugin.VERIFICATION_GROUP

        outputs.upToDateWhen { false }  // Always run tests

        systemProperty "geb.build.reportsDir", reporting.file("geb/$name")
        systemProperty "geb.env", driver
    }
}

test {
    dependsOn drivers.collect { tasks["${it}Test"] }
    enabled = false
}

tasks.withType(Test) {
    maxHeapSize = "1g"
    jvmArgs '-XX:MaxMetaspaceSize=128m'
    testLogging {
        exceptionFormat = 'full'
    }
}

tasks.withType(GroovyCompile) {
    groovyOptions.forkOptions.memoryMaximumSize = '256m'
}

我尝试将以下内容插入 build.gradle:

task dataGen {
    include '**com.company.project.spec.util/DataGenerationUtilSpec.groovy'
}

task sanity {
    include '**com.company.project.spec.sanity.*'
}

但调用这些任务 (gradle sanity) 会导致构建失败:

Could not find method include() for arguments [**com.company.project.spec.util/DataGenerationUtilSpec.groovy] on task ':dataGen' of type org.gradle.api.DefaultTask

显然有现有的构建说明,因为我可以调用 gradle build 并且所有规范都在 Chrome 上运行,我只是不确定如何添加更多任务

【问题讨论】:

  • 你能更好地解释一下这个问题的标题吗?你的意思是运行不同的情况(理智、回归等)还是在不同的浏览器上运行(chrome、firefox)
  • @Royg 我想我可以传入哪个浏览器 ID 喜欢用作运行时参数,但我需要测试来运行一组特定的案例
  • 对于这种用法,您可以使用下面的答案。
  • 目前尚不清楚您要实现什么 Doug Clark - 您的问题标题提到在多个浏览器中运行测试,但您的问题文本似乎是关于运行测试子集作为不同测试任务的一部分。还不清楚您的设置是什么,因为根据 ToYonos 的回答,您的一个 cmets 似乎没有任何测试 - 您得到的是 Skipping task ':patientGen' as it has no source files。如果您希望我们能够为您提供帮助,您需要更好地向我们展示全貌。

标签: gradle automated-tests spock geb


【解决方案1】:

我认为这 2 个任务是 test 任务,所以应该是这样的:

task dataGen (type: Test) {
    include '**com.company.project.spec.util/DataGenerationUtilSpec.groovy'
}

task sanity (type: Test) {
    include '**com.company.project.spec.sanity.*' 
}

【讨论】:

  • 谢谢,又近了一步。现在,当我调用“gradle sanity -i”时,我得到以下信息: > 任务:patientGen NO-SOURCE 跳过任务“:patientGen”,因为它没有源文件,也没有以前的输出文件。 :patientGen (Thread[Task worker for ':',5,main]) 完成。耗时 0.001 秒。
  • 控制台显示所有可操作的任务都已运行,但 webdriver 从未启动
  • 这是另一个问题...patientGen你的问题中没有提到任务
  • 消毒后不小心,dataGen和patientGen是一样的
【解决方案2】:

您可以使用 Spock 注解来控制测试或 Spec,参见示例 here。

您必须定义注释类并定义 Spock 配置文件以使用该注释。然后,您注释特定的规范(或测试)。
现在您必须在任务中或从参数中定义 Spock 配置文件。

【讨论】:

    猜你喜欢
    • 2018-12-09
    • 1970-01-01
    • 2017-11-02
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    相关资源
    最近更新 更多