Reputation: 823
I've got a set of cucumber (java) tests which I would like to run via the commandline, setting system properties to define things such as the browser and whether to run locally or on BrowsersStack. The system is built with Gradle.
I have looked at 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, and the comments suggest it works for cucumber.
My gradle.build file looks like this:
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'
}
The code that checks the system property and should load it is held in a @Before statement:
@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"));
...
When I run the script using gradle test -DlocalBrowser=false --rerun-tasks
the output looks like this:
> Task :junitPlatformTest
cucumber.runner.Scenario@2e377400
Value of localBrowser is null
Value of localBrowser is true
Tests running locally on Chrome
So, localBrowser
is not picked up or not passed to the module, and my script then sets it to true
as it finds a null value, and runs all tests on a local browser in the code that follows.
Am I missing something obvious, or is there a problem with a newer version of Gradle? I'm using version 5.0.
Upvotes: 0
Views: 3188
Reputation: 823
After much digging, it works out that the junit gradle plugin was the thing blocking the passing of system variables. I have decided to post it as an answer because I'm sure this is not documented behaviour!
My new build file is as follows:
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')
}
As far as I can tell, the junit gradle plugin was only used to enable the logging of test steps to the console. This has been resolved with including testlogging.showStandardStreams=true
in the test task. There's a really neat logging plugin (here) that might do an even better job of this but I haven't tried it.
Upvotes: 1