通过gradle将系统属性传递给黄瓜测试

时间:2019-01-11 13:19:58

标签: java gradle cucumber

我有一组黄瓜(java)测试,我想通过命令行运行,设置系统属性以定义诸如浏览器以及是在本地还是在BrowsersStack上运行。该系统使用Gradle构建。

我查看了this question,并尝试了那里推荐的所有其他解决方案,但无济于事。我还注意到,虽然在2014年提出了问题并接受了答案,但2018年有很多评论说这种方法行不通。 this question中详细介绍了一种类似的方法,注释表明该方法适用于黄瓜。

我的gradle.build文件如下:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-RC2'
    }
}

plugins {
    id 'com.github.spacialcircumstances.gradle-cucumber-reporting' version '0.0.12'
}

repositories {
    mavenCentral()
}

apply plugin: 'org.junit.platform.gradle.plugin'

group 'org.myorg'
version '0.1'

junitPlatform {
    platformVersion '1.0.0-RC2'
    reportsDir file('build/test-results/junit-platform')
}

apply plugin: 'java'

sourceCompatibility = 1.8

cucumberReports {
    outputDir = file('target/cucumber-report')
    buildName = '0'
    reports = files('target/cucumber-report/json/cucumber-report.json')
    parallelTesting = false
}

test {
    ignoreFailures = true
    filter 
    {
        // Include all tests.
        includeTestsMatching "*.*"
    }
    systemProperty "localBrowser", System.getProperty("localBrowser")
}

dependencies {
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.5.1'

    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile group: 'org.testng', name: 'testng', version: '6.9.10'
    compile group: 'com.browserstack', name: 'browserstack-local-java', version:'1.0.1'

    // The cucumber version is 2.+ as 3+ cannot run tests from within IntelliJ ~_~
    // Fixed for v 4! :-)
    testCompile 'io.cucumber:cucumber-java:4.+'
    testCompile 'io.cucumber:cucumber-junit:4.+'
    testCompile group: 'io.cucumber', name: 'cucumber-picocontainer', version: '4.+'
    testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'

    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-RC2'
    testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.0.0-RC2'
    testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-RC2'
    testRuntime group: 'org.junit.vintage', name: 'junit-vintage-engine', version: '4.12.0-RC2'

}

检查系统属性并应加载它的代码保存在@Before语句中:

@Before
    public void setUp(Scenario scenario) throws Exception {
        System.out.println(scenario);

        System.out.println("Value of localBrowser is " + System.getProperty("localBrowser"));

        // Local browser or Browserstack? Default is local.
        if (System.getProperty("localBrowser") == null) {
            System.setProperty("localBrowser", "true");
        }

         System.out.println("Value of localBrowser is " + System.getProperty("localBrowser"));
...

当我使用gradle test -DlocalBrowser=false --rerun-tasks运行脚本时,输出如下所示:

> Task :junitPlatformTest
cucumber.runner.Scenario@2e377400
Value of localBrowser is null
Value of localBrowser is true
Tests running locally on Chrome

因此,localBrowser未被拾取或未传递给模块,然后我的脚本在找到空值时将其设置为true,并在本地浏览器中运行所有测试。随后的代码。

我是否缺少明显的东西,或者较新版本的Gradle是否有问题?我正在使用5.0版。

1 个答案:

答案 0 :(得分:1)

经过大量挖掘,结果发现junit gradle插件是阻止系统变量传递的东西。我决定将其发布为答案,因为我确定这没有记录的行为!

我的新构建文件如下:

buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id 'java'
    id "com.github.spacialcircumstances.gradle-cucumber-reporting" version "0.1.2"
}


group 'org.myorg'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

project.ext {
    cucumberVersion = '4.0.0'
}


dependencies {
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.5.1'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile 'io.cucumber:cucumber-java:' + cucumberVersion
    testCompile 'io.cucumber:cucumber-junit:' + cucumberVersion
    testCompile group: 'com.browserstack', name: 'browserstack-local-java', version:'1.0.1'
    testCompile group: 'io.cucumber', name: 'cucumber-picocontainer', version: '2.1.0'
    testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
    testCompile group: 'org.testng', name: 'testng', version: '6.9.10'

}

test {
    systemProperties System.getProperties()
    ignoreFailures = true
    filter
            {
                // Include all tests.
                includeTestsMatching "*.*"
            }

    systemProperty "localBrowser", System.getProperty("localBrowser")
    testLogging.showStandardStreams = true
}

cucumberReports {
    outputDir = file('target/cucumber-report')
    buildId = '0'
    reports = files('target/cucumber-report/json/cucumber-report.json')
}

据我所知,junit gradle插件仅用于将测试步骤记录到控制台。通过在测试任务中包含testlogging.showStandardStreams=true可以解决此问题。有一个非常整洁的日志记录插件(here)可能会做得更好,但是我还没有尝试过。