本文介绍了如何通过gradle systemProperties JUnit5测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用gradle 3.5
和Unit 5 (jupiter)
.
我希望将System属性传递给我的测试,以便配置测试
I wish to pass System property to my tests, in order to configure the test
我正在使用此命令gradle test -Denv=envFile1
这是我的gradle文件:
Here is my gradle file :
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"
}
这是我的考试:
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"));
}
}
这是我得到的输出:
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
*我已经更新了问题
推荐答案
在junit的早期版本中,它将与以下代码段一起工作:
With previous versions of junit it would work with the following piece of code:
test {
systemProperties System.properties
}
或
test {
systemProperty 'env', System.properties['env']
}
在命令行中使用-D
时,它会传递给gradle构建脚本而不是JVM.
When you use -D
in command line it is passed to gradle build script not to the JVM.
但是,似乎它不适用于最新版本,请参见此处:
However it seems that it doesn't work with the latest version, please see here:
这篇关于如何通过gradle systemProperties JUnit5测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!