[英]How to pass gradle systemProperties JUnit5 tests?
I am using gradle 3.5
and Unit 5 (jupiter)
. 我正在使用gradle 3.5
和Unit 5 (jupiter)
。
I wish to pass System property to my tests, in order to configure the test 我希望将System属性传递给我的测试,以便配置测试
I am running the test using this command gradle test -Denv=envFile1
我正在使用此命令gradle test -Denv=envFile1
运行测试
Here is my gradle file : 这是我的gradle文件:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M4' //JUnit 5
}
}
repositories {
mavenCentral()
}
ext.versionJunitPlatform = '1.0.0-M4'
ext.versionJunitJupiter = '5.0.0-M4'
ext.versionLog4j = '1.2.17'
ext.versionRestAssured = '2.9.0'
ext.versionRestAssuredJsonValid = '3.0.2'
ext.versionHamcrest = '1.3'
ext.versionJacksonDatabind = '2.9.0.pr2'
ext.versionSeleniumJava = '3.3.1'
test {
systemProperties System.properties
systemProperty 'env1', System.properties['env']
systemProperty 'env2', 'i am a static env'
}
dependencies {
compile group: 'log4j' , name: 'log4j' , version: "$versionLog4j"
compile group: 'com.jayway.restassured' , name: 'rest-assured' , version: "$versionRestAssured"
compile group: 'io.rest-assured' , name: 'json-schema-validator' , version: "$versionRestAssuredJsonValid"
compile group: 'org.hamcrest' , name: 'hamcrest-all' , version: "$versionHamcrest"
compile group: 'com.fasterxml.jackson.core' , name: 'jackson-databind' , version: "$versionJacksonDatabind"
compile group: 'org.seleniumhq.selenium' , name: 'selenium-java' , version: "$versionSeleniumJava"
testCompile group: 'org.junit.jupiter' , name: 'junit-jupiter-api' , version: "$versionJunitJupiter"
testRuntime group: 'org.junit.jupiter' , name: 'junit-jupiter-engine' , version: "$versionJunitJupiter"
}
Here is my test: 这是我的测试:
package com.somecompany.someProject.tests;
import org.junit.jupiter.api.Test;
public class AaTest {
@Test
public void a() {
System.out.println("a env: " + System.getProperty("env"));
System.out.println("a env1: " + System.getProperty("env1"));
System.out.println("a env2: " + System.getProperty("env2"));
}
}
This is the output I get: 这是我得到的输出:
gradle test -Denv=envName
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:junitPlatformTest
a env: null
a env1: null
a env2: null
*i have updated the question *我已经更新了这个问题
This is a duplicate of Setting system properties when using junitPlatform , which I answered here: https://stackoverflow.com/a/41334739/388980 这是使用junitPlatform时设置系统属性的副本 ,我在这里回答: https : //stackoverflow.com/a/41334739/388980
You can set system properties like this: 您可以像这样设置系统属性:
afterEvaluate {
def junitPlatformTestTask = tasks.getByName('junitPlatformTest')
junitPlatformTestTask.systemProperty 'org.slf4j.simpleLogger.defaultLogLevel', 'warn'
}
With previous versions of junit it would work with the following piece of code: 使用以前版本的junit,它可以使用以下代码:
test {
systemProperties System.properties
}
or 要么
test {
systemProperty 'env', System.properties['env']
}
When you use -D
in command line it is passed to gradle build script not to the JVM. 在命令行中使用-D
,它将传递给gradle构建脚本而不是JVM。
However it seems that it doesn't work with the latest version, please see here : 但是它似乎不能与最新版本一起使用,请看这里 :
The junitPlatform extension/task does not support systemProperty at this time. junitPlatform扩展/任务此时不支持systemProperty。 This will change when the Gradle team adds support for JUnit 5 to core Gradle. 当Gradle团队将JUnit 5的支持添加到核心Gradle时,这将发生变化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.