//imports removed...
public class BatchDepXMLReportValidationTest {
/**
* @testng.before-class = "true"
*/
protected void configure() throws Exception {
XMLUnit.setControlParser(
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
XMLUnit.setTestParser(
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
XMLUnit.setSAXParserFactory(
"org.apache.xerces.jaxp.SAXParserFactoryImpl");
XMLUnit.setIgnoreWhitespace(true);
}
private Filter[] getFilters(){
return new Filter[] {
new RegexPackageFilter("java|org"),
new SimplePackageFilter("junit.")
};
}
private Dependency[] getDependencies(){
return new Dependency[] {
new Dependency("com.vanward.resource.XMLizable"),
new Dependency("com.vanward.xml.Element")
};
}
/**
* @testng.test
*/
public void assertToXML() throws Exception{
BatchDependencyXMLReport report =
new BatchDependencyXMLReport(
new Date(9000000), this.getFilters());
report.addTargetAndDependencies(
"com.vanward.test.MyTest", this.getDependencies());
report.addTargetAndDependencies(
"com.xom.xml.Test", this.getDependencies());
Diff diff = new Diff(new FileReader(
new File("./test/conf/report-control.xml")),
new StringReader(report.toXML()));
Assert.assertTrue(
diff.identical(),"XML was not identical");
}
}
虽然不应在每次运行构建时执行组件测试,但在将代码提交到储存库之前运行组件测试却是一个好主意。在持续集成环境中,时常运行它很可能是个好主意,比如每小时一次。
系统测试
系统测试处理一个完整的软件应用程序,验证外部接口,如Web页面、Web 服务端点、GUI、按照设计端到端地完成工作。因为系统测试处理的是整个系统,所以往往在开发的后期创建。除增加了设置和配置时间之外,系统测试还倾向于具有更长的执行时间。
例如,以下测试使用 jWebUnit 测试一个Web站点的登陆功能。请注意,这个测试中做了许多假设,如可用URL、实际上拥有有效账户的“tst”用户、未提及交易历史记录等。这些隐含的依赖项通常需要在测试运行前完成一个配置步骤。
public class LoginTest {
private WebTester tester;
/**
* @testng.before-class = "true"
*/
protected void init() throws Exception {
this.tester = new WebTester();
this.tester.getTestContext().
setBaseUrl("http://stocktrader.com");
}
/**
* @testng.test
*/
public void verifyLogIn() {
this.tester.beginAt("/");
this.tester.setFormElement("user", "tst");
this.tester.setFormElement("psswd", "t@t");
this.tester.submit();
this.tester.assertTextPresent("Logged in as tst");
}
/**
* @testng.test dependsOnMethods = "verifyLogIn"
*/
public void verifyAccountInfo() {
this.tester.clickLinkWithText("History", 0);
this.tester.assertTextPresent(".00, sold");
}
}
开发人员应当以需求为基础,在一个持续集成环境中本地运行这些测试。每晚执行测试是个不错的策略(如果测试能够实现自动运行)。更频繁地运行测试可能消耗大量系统资源,特别是在较大的环境中。但有了合理的硬件规划和更加完善的自动化,团队就能更频繁地运行这些测试。