我有一个包含JUnit测试的Java项目,需要通过Jenkins在不同的测试环境(Dev,Staging等)上运行。在多个环境下执行JUnit测试的配置
我目前必须在不同的环境下构建项目并将url,用户名和密码传递给测试运行器的解决方案是为POM文件中的每个环境加载特定的属性文件。属性文件将为每个环境经由Maven构建命令来设置:
MVN清洁安装-DappConfig =/SRC /测试/ 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。
任何建议,非常感谢。
你真的需要为单元测试ENV设置。单元测试应该模拟所有的依赖关系,而不是依赖于环境。如果你正在尝试执行集成测试,那么你应该照顾环境。 – emd 2013-03-19 20:28:49
我有一套针对网站运行的测试用例,因此它们需要作为参数url,登录名和密码 – 2013-03-20 04:24:11
您可以使用Eclipse运行测试,只需设置java系统属性,如-DappConfig =“某个位置” – emd 2013-03-20 09:51:29