【发布时间】:2012-04-14 03:06:55
【问题描述】:
我需要:
- 观看测试
- 收集信息
- 根据测试生成报告
测试将通过 TeamCity 启动。我创建了一个 TestWatcher 对象来监听测试结果,这个 TestWatcher 包含在每个包含测试的 JUnit 类中。我有一个监听器,它会在整个套件完成时收听,但我必须以编程方式添加它。由于我现在使用 TeamCity 运行测试并生成结果,我相信我已经失去了这种能力。我还被要求制作一份包含 TeamCity 结果的 PDF 报告。我只需要知道测试什么时候完成,这样我就可以知道什么时候开始构建我的报告。有没有办法仅通过使用 TestWatcher 来完成此任务?
下面是我的 TestWatcher 目前的样子。 BaseTestResult 只是一个包含测试结果的类,并将它们组织起来以便更容易地打印出来。我也在用 Selenium,驱动变量是 WebDriver 类型的:
@Rule
public TestWatcher watchman = new TestWatcher() {
private BaseTestResult currentTest;
private long startTime;
private long endTime;
@Override
protected void starting(Description d) {
startTime = System.currentTimeMillis();
currentTest = new BaseTestResult(d);
currentTest.setBrowser(type);
if (d.getAnnotation(TestDescription.class) != null) {
currentTest.setDescription(d.getAnnotation(
TestDescription.class).description());
}
currentTest.setSuite(d.getTestClass().getName());
}
@Override
protected void succeeded(Description d) {
currentTest.setSucceeded(true);
}
@Override
protected void failed(Throwable e, Description d) {
currentTest.setThrowable(e);
}
@Override
protected void finished(Description d) {
endTime = System.currentTimeMillis();
currentTest.setRuntime(endTime - startTime);
String fileName = d.getMethodName() + type + ".png";
File srcFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
String filePath = "./screens/" + fileName;
try {
FileUtils.copyFile(srcFile, new File(filePath));
currentTest.setScreenshotPath(filePath);
} catch (IOException e) {
log.severe(e.toString());
}
if (currentTest.getSucceeded()) {
BaseListener.getSuiteResult().addPassed(currentTest);
} else {
BaseListener.getSuiteResult().addFailed(currentTest);
}
// Quit, the web driver
if (driver != null) {
driver.quit();
}
}
};
【问题讨论】: