如何通过使用TestWatcher来判断JUnit何时完成?

Rei*_*Mac 3 java pdf junit selenium

我需要:

  • 观看测试
  • 收集信息
  • 从测试中构建报告

测试将通过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();
        }
    }

};
Run Code Online (Sandbox Code Playgroud)

Pet*_*ček 6

你可以这样做:

@ClassRule // the magic is done here
public static TestRule classWatchman = new TestWatcher() {
    @Override
    protected void starting(Description desc) {
        System.out.println(desc.testCount()); // insert actual logic here
    }
};
Run Code Online (Sandbox Code Playgroud)

这样可以观看全班而不是每次测试.这意味着它可以在套件开始时为您提供套件中的测试数量.然后,每次调用BaseListener.getSuiteResult().addPassed(currentTest);或者BaseListener.getSuiteResult().addFailed(currentTest);您可以检查是否已经在套件中添加了测试数量(意味着套件已完成).

或者,甚至更好,

@ClassRule
public static TestRule classWatchman = new TestWatcher() {
    @Override
    protected void finished(Description desc) {
        System.out.println("Suite completed!"); // insert actual logic here
    }
};
Run Code Online (Sandbox Code Playgroud)

如果你有多个包含测试的类,你可以创建一个包含所有这些的AllMyTests类!然后可以通过JUnit运行此AllMyTests类.在这种情况下,@ ClassRule将表现为@SuiteRule(不存在).

@RunWith(Suite.class)
@Suite.SuiteClasses({ First.class, Second.class, Third.class })
public class AllMyTests {
    // nothing to do
}
Run Code Online (Sandbox Code Playgroud)