如何将 JVM 系统属性传递给我的测试?

发布于 2024-11-01 09:19:22 字数 271 浏览 5 评论 0原文

我有以下任务

task testGeb(type:Test) {
   jvmArgs '-Dgeb.driver=firefox'
   include "geb/**/*.class"
   testReportDir = new File(testReportDir, "gebtests")
}

系统属性似乎没有进入 Geb 测试,因为 Geb 不会生成 Firefox 来运行测试。当我在 Eclipse 中设置相同的系统属性并运行测试时,一切正常。

I have the following task

task testGeb(type:Test) {
   jvmArgs '-Dgeb.driver=firefox'
   include "geb/**/*.class"
   testReportDir = new File(testReportDir, "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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜`诱少女 2024-11-08 09:19:22

尝试使用系统属性:

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

Try using system properties:

test {
   systemProperties['geb.driver'] = 'firefox'
   include "geb/**/*.class"
   testReportDir = new File(testReportDir, "gebtests")
}
情话墙 2024-11-08 09:19:22

您还可以直接在任务中设置系统属性:(

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

上面的解决方案也适用于与 Test 不同的任务类型)

或者如果您希望能够从命令行传递不同的属性,您可以在任务定义中包含更灵活的解决方案:

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

然后您可以运行:
./gradlew testGeb -D[anyArg]=[anyValue],在您的情况下:./gradlew testGeb -Dgeb.driver=firefox

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

网白 2024-11-08 09:19:22
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'

}

注意:我使用的是 Ubuntu 操作系统,并且我在 /usr/bin/google_chrome/ 中指定了 chrome_driver 路径,并且您的路径根据您的路径而有所不同。

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.

拥抱影子 2024-11-08 09:19:22

在您的任务中添加 systemProperties System.getProperties()

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

以便在运行测试时可以对其进行配置。例如

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

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 
诗酒趁年少 2024-11-08 09:19:22

我建议

gradle myTask -DmyParameter=123

使用以下代码执行以下

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

操作输出应为

gradle myTask -DmyParameter=123
:我的任务
123

构建成功

总时间:2.115 秒

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文