我有一个包含 JUnit 测试的 Java 项目,需要通过 Jenkins 在不同的测试环境(Dev、Staging 等)上运行。
我目前必须在不同的环境中构建项目并将 url、用户名和密码传递给测试运行器的解决方案是在 POM 文件中为每个环境加载特定的属性文件。将通过 Maven 构建命令为每个环境设置属性文件:
mvn clean install -DappConfig=/src/test/resouces/integration.environment.properties
在 pom.xml 中:
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<appConfig>${app.config}</appConfig>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
在 JUnit 测试运行器类中:
public class BoGeneralTest extends TestCase {
protected WebDriver driver;
protected BoHomePage boHomePage;
protected static Properties systemProps;
String url = systemProps.getProperty("Url");
String username = systemProps.getProperty("Username");
String password = systemProps.getProperty("Password");
int defaultWaitTime = Integer.parseInt(systemProps.getProperty("waitTimeForElements"));
String regUsername = RandomStringUtils.randomAlphabetic(5);
final static String appConfigPath = System.getProperty("appConfig");
static {
systemProps = new Properties();
try {
systemProps.load(new FileReader(new File(appConfigPath)));
} catch (Exception e) {
e.printStackTrace();
}
}
此配置的问题在于,现在无法通过 Eclipse 单独运行各个测试,因为它们期望从 maven 接收 appConfig
而我收到 NullPointerException。
非常感谢任何建议。
最佳答案
运行单个测试的前提是每个测试用例都有一个运行配置,它将指定默认的执行环境。请注意,此设置必须在每个测试用例本地完成。
在 Eclipse Arguments 选项卡/VM Arguments 中,必须指定 VM 参数:
-DappConfig=src/test/resources/pp1.environment.properties
它包含具有环境登录详细信息的相应属性文件的路径。
在项目的src/test/resources源码文件夹下定义了五个属性文件:
environment1.properties
environment2.properties
environment3.properties
environment4.properties
environment5.properties
关于java - 在多个环境中执行 JUnit 测试的配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15509578/