我有一个使用 TestNg 用 Java 编写并使用 gradle 构建的测试框架。我想在命令行传递一个“环境”参数,它将在环境类中用于为特定环境定义域和服务器值。使用 Gradle 文档和关于此问题的几个论坛讨论,我仍然无法将参数作为系统属性传递给框架。我试图通过“测试”任务和“调试”任务来完成此任务,但无济于事。任务中的“println”语句显示 gradle 正在获取 -Denv 参数,但我尝试将其添加到测试框架的系统属性失败。
执行结果为空引用,environmentName 被设置为空。
有人可以找出我的错误吗?
命令行
.\gradlew debug -Denv=production
构建.gradle
test {
def environment = System.properties["env"]
println environment
systemProperties = System.getProperties()
systemProperties['env'] = environment
useTestNG()
}
task debug(type: Test) {
// def environment = System.properties["env"]
// println environment
// systemProperties = System.getProperties()
// systemProperties['env'] = environment
def groupsToInclude = []
def groupsToExclude = []
groupsToInclude.add('under_development')
useTestNG() {
groupsToInclude.each { String group -> includeGroups group }
groupsToExclude.each { String group -> excludeGroups group }
}
}
Java 类
import com.sun.javafx.runtime.SystemProperties;
public class Environment {
private static EnvironmentDefinition environment;
private Environment() {
}
public static EnvironmentDefinition getInstance() {
if (environment == null) {
String environmentName = SystemProperties.getProperty("env");
// environmentName = environmentName == null ? "production" : environmentName;
switch (environmentName) {
default:
Environment.environment = ProductionEnvironment.getInstance();
}
}
return environment;
}
}
最佳答案
关于java - GRADLE:TestNg - 无法将 -D 参数传递给 Java 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32815090/