使用gradle在多个浏览器上运行特定的测试

时间:2018-10-05 18:23:05

标签: gradle automated-tests spock geb

我正在使用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上运行,但我不确定如何添加更多任务

2 个答案:

答案 0 :(得分:2)

我认为这2个任务是test个任务,因此看起来应该像这样:

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

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

答案 1 :(得分:1)

您可以使用Spock注释来控制测试或规范,请参见示例here

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