通过 gradle 将系统 属性 传递给黄瓜测试
Passing system property to cucumber test via gradle
我有一组黄瓜 (java) 测试,我想通过命令行 运行,设置系统属性来定义诸如浏览器之类的东西以及是否 运行 在本地或在 BrowsersStack 上。该系统是用 Gradle 构建的。
我看过 this question and tried every different solution recommended there, to no avail. I also noticed that while the question was posed and the answer accepted in 2014, there are a number of 2018 comments that say the approach isn't working. A similar approach is detailed in 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 版。
经过大量挖掘,发现是 junit gradle 插件阻止了系统变量的传递。我决定 post 它作为答案,因为我确定这不是记录在案的行为!
我的新构建文件如下:
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) 可能会做得更好,但我还没有尝试过。
我有一组黄瓜 (java) 测试,我想通过命令行 运行,设置系统属性来定义诸如浏览器之类的东西以及是否 运行 在本地或在 BrowsersStack 上。该系统是用 Gradle 构建的。
我看过 this question and tried every different solution recommended there, to no avail. I also noticed that while the question was posed and the answer accepted in 2014, there are a number of 2018 comments that say the approach isn't working. A similar approach is detailed in 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 版。
经过大量挖掘,发现是 junit gradle 插件阻止了系统变量的传递。我决定 post 它作为答案,因为我确定这不是记录在案的行为!
我的新构建文件如下:
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) 可能会做得更好,但我还没有尝试过。