我需要:如何判断JUnit何时通过使用TestWatcher完成?
- 观看测试
- 收集信息
- 从
测试将通过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();
}
}
};
顺便说一句我才意识到,我们都使用我们的测试_Watcher_以旧的方式“守夜人” :) – 2012-03-30 13:49:48
感谢您的回答命名,这绝对是在步骤正确的方向。如果我有多个班,该怎么办?我计划拥有多个测试类,并且希望根据所有运行的测试类的结果构建报告。 – 2012-03-30 13:53:19
要在一分钟内编辑答案以回答您的评论。 – 2012-03-30 13:57:43