简体   繁体   English

有谁知道如何将gradle属性传递到单元测试并在命令行上覆盖它们?

[英]Does anyone know how to pass gradle properties to unit test and override them on command line?

For example, I would like to run smoke tests in Jenkins and the tests will use a different port/address depending on the environment. 例如,我想在詹金斯(Jenkins)中运行冒烟测试,并且测试将根据环境使用不同的端口/地址。 So I want to set a default value somewhere (gradle.build?) and then be able to override it on the gradle command line. 所以我想在某个地方设置默认值(gradle.build?),然后能够在gradle命令行上覆盖它。 But I need to use the default or overridden value in my junit test. 但是我需要在我的junit测试中使用默认值或覆盖值。 Right now I'm just reading a properties file in my junit test to get the url but I want to use it somehow from gradle.build value. 现在,我只是在junit测试中读取属性文件以获取url,但我想以某种方式从gradle.build值中使用它。

Right now I just use property file. 现在我只使用属性文件。

@Before
public void setup() throws FileNotFoundException, IOException{
    try {
        props.load(new FileInputStream("gradle.properties"));

    } catch (IOException e) {
        logger.error(e.getMessage());
        logger.debug("Error loading gradle.properties",e);
    }
}

@Test 
public void testResponseCode () {   
    get(props.getProperty("url") + HISTORIC).then().assertThat().statusCode(equalTo(200));
}

You will probably need to pass it through Gradle as a system property, ie -Dkey=value . 您可能需要将其作为系统属性通过 Gradle传递,即-Dkey=value Alternatively, your Gradle script could set a system property, eg System.properties.'my.property' = ? 另外,您的Gradle脚本可以设置系统属性,例如System.properties.'my.property' = ? . A JUnit test will have no access to Gradle properties. JUnit测试将无法访问Gradle属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从gradle项目中的命令行覆盖集成测试中的属性? - How can I override a property in an integration test from the command line in a gradle project? 如何使用 gradle bootRun 添加命令行属性? - How to add command line properties with gradle bootRun? 将 gradle 命令行 arguments 传递给 application.properties - Pass gradle command line arguments to application.properties 有谁知道如何将这一行 Java 代码翻译成 Python? - Does anyone know how to translate this single line of Java code into Python? 如何通过命令行参数覆盖属性文件的值? - How to override the properties file value through command line arguments? 如何将命令行参数传递给Junit Test - How to pass command line arguments to Junit Test 如何将命令行参数作为密码传递给Gradle项目? - How to pass command line argument as a password to gradle project? 如何在gradle中将命令行参数传递给主类? - How to pass command-line arguments to main class in gradle? 有谁知道如何使用使用tomcat插件的子项目构建gradle父项目 - Does anyone know how to build a gradle parent project with subprojects that uses the tomcat plugin 运行gradle任务时如何在命令行中传递多个参数? - How to pass multiple parameters in command line when running gradle task?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM