【问题标题】:Passing system property to cucumber test via gradle通过gradle将系统属性传递给黄瓜测试
【发布时间】:2019-01-11 13:19:57
【问题描述】:

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

我查看了this question 并尝试了那里推荐的所有不同解决方案,但无济于事。我还注意到,虽然在 2014 年提出了问题并接受了答案,但 2018 年有许多 cmets 表示该方法不起作用。 this question 中详细介绍了类似的方法,cmets 建议它适用于黄瓜。

我的 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 版。

【问题讨论】:

  • 它是否适用于像 systemProperty "localBrowser", 'false' 这样的硬编码版本?
  • 很奇怪,没有:-/。更奇怪的是,我从 GitHub 下载了一个干净的 cucumber-java-skeleton-master 版本,并且可以毫无问题地传递属性。这里发生了一些非常奇怪的事情。
  • 我看到你指定了'io.cucumber:cucumber-java:4.+',而骨架项目指定了cucumberVersion = '4.0.0';不太熟悉 Gradle,我不确定这是否重要。另外我注意到您指定了 JUnit 5,Cucumber-jvm 尚不支持 afaik,除非您使用 'junit-vintage-engine' 来规避它?
  • @Marit 是的,老式引擎也在使用中。不过我会尝试做一些修改,谢谢。

标签: java gradle cucumber


【解决方案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) 可能会做得更好,但我还没有尝试过。

【讨论】:

    猜你喜欢
    • 2022-10-23
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 2016-11-02
    • 2013-03-24
    • 1970-01-01
    • 2016-06-25
    • 2016-11-08
    相关资源
    最近更新 更多