开发者

How can I pass JVM system properties on to my tests?

开发者 https://www.devze.com 2023-02-24 09:12 出处:网络
I have the following task task testGeb(type:Test) { jvmArgs \'-Dgeb.driver=firefox\' include \"geb/**/*.class\"

I have the following task

task testGeb(type:Test) {
   jvmArgs '-Dgeb.driver=firefox'
   include "geb/**/*.class"
   testReportDir = new File(t开发者_高级运维estReportDir, "gebtests")
}

The system property doesn't appear to make it to the Geb tests, as Geb does not spawn Firefox to run the tests. When I set the same system property in Eclipse and run the tests, everything works fine.


Try using system properties:

test {
   systemProperties['geb.driver'] = 'firefox'
   include "geb/**/*.class"
   testReportDir = new File(testReportDir, "gebtests")
}


You can also directly set the system property in the task:

task testGeb(type:Test) {
    System.setProperty('geb.driver', 'firefox')}

(the solution above will also work for task type different from Test)

or if you would like to be able to pass different properties from the command line, you can include a more flexible solution in the task definition:

task testGeb(type:Test) {
    jvmArgs project.gradle.startParameter.systemPropertiesArgs.entrySet().collect{"-D${it.key}=${it.value}"}
}

and then you can run: ./gradlew testGeb -D[anyArg]=[anyValue], in your case: ./gradlew testGeb -Dgeb.driver=firefox


Below code works fine for me using Gradle and my cucumber scenarios are passing perfectly. Add below code in your build.gradle file:

//noinspection GroovyAssignabilityCheck

test{

    systemProperties['webdriver.chrome.driver'] = '/usr/bin/google_chrome/chromedriver'

}

Note: I used Ubuntu OS and the chrome_driver path I specified in /usr/bin/google_chrome/ and your path varies according to your path.


Add systemProperties System.getProperties() in your task

test {
  ignoreFailures = false
  include "geb/**/*.class"
  testReportDir = new File(testReportDir, "gebtests")
  // set a system property for the test JVM(s)
  systemProperties System.getProperties()
}

So that it will be configurable while running the test. For example

gradle -Dgeb.driver=firefox test
gradle -Dgeb.driver=chrome test 


I would recommend doing the following

gradle myTask -DmyParameter=123

with the following code

task myTask {
    doLast {
        println System.properties['myParameter']
    }
 }

The output should be

gradle myTask -DmyParameter=123 :myTask 123

BUILD SUCCESSFUL

Total time: 2.115 secs

0

精彩评论

暂无评论...
验证码 换一张
取 消