微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我需要:

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

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

};

解决方法:

你可以这样做:

@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
    }
};

这样可以观看全班而不是每次测试.这意味着它可以在套件开始时为您提供套件中的测试数量.然后,每次调用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
    }
};

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐