如何传递gradle systemProperties JUnit5测试?

Sha*_*y_t 5 java testing gradle junit5 selenium-jupiter

我正在使用gradle 3.5Unit 5 (jupiter).

我希望将System属性传递给我的测试,以便配置测试

我正在使用此命令运行测试 gradle test -Denv=envFile1

这是我的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"
}
Run Code Online (Sandbox Code Playgroud)

这是我的测试:

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"));
    }
} 
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出:

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
Run Code Online (Sandbox Code Playgroud)

*我已经更新了这个问题

Opa*_*pal 5

对于以前版本的 junit,它可以使用以下代码:

test {
   systemProperties System.properties
}
Run Code Online (Sandbox Code Playgroud)

或者

test {
   systemProperty 'env', System.properties['env']
}
Run Code Online (Sandbox Code Playgroud)

当您-D在命令行中使用时,它会传递给 gradle 构建脚本,而不是传递给 JVM。

但是它似乎不适用于最新版本,请参阅此处

junitPlatform 扩展/任务目前不支持 systemProperty。当 Gradle 团队向核心 Gradle 添加对 JUnit 5 的支持时,这种情况将会改变。


Sam*_*nen 5

这是使用junitPlatform时设置系统属性的副本,我在这里回答:https://stackoverflow.com/a/41334739/388980

您可以像这样设置系统属性:

afterEvaluate {
    def junitPlatformTestTask = tasks.getByName('junitPlatformTest')

    junitPlatformTestTask.systemProperty 'org.slf4j.simpleLogger.defaultLogLevel', 'warn'
}
Run Code Online (Sandbox Code Playgroud)