有没有办法从 tearDown() 判断我的 PHPUnit 测试用例中是否有故障? [英] Is there a way to tell whether there are failures in my PHPUnit test case from tearDown()?

查看:21
本文介绍了有没有办法从 tearDown() 判断我的 PHPUnit 测试用例中是否有故障?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PHPUnit 中检查我的测试执行是成功还是失败的最佳方法是什么?

What's the best way for me to check in PHPUnit whether my test execution succeeded or failed?

我正在尝试为我的 Selenium 窗口截屏,但前提是我的测试失败了.我试过在 onNotSuccessfulTest() ,但是如果我总是在 tearDown() 中关闭我的窗口(我应该这样做),那么在我的 onNotSuccessfulTest()函数.

I am trying to take a screenshot for my Selenium window, but only when my test has failed. I've tried taking a screenshot in onNotSuccessfulTest() , but if I am always closing my window in tearDown() (which I should be doing), then there is no session to take the screenshot with in my onNotSuccessfulTest() function.

我正在考虑的解决方案是在tearDown()中检查测试是成功还是失败,确定是否截屏.

The solution I'm thinking of involves checking for whether the test succeeded or failed in the tearDown(), determining whether or not to take a screenshot.

我正在使用 PHPUnit 3.6 [审查为 9.5] 和 Facebook 的 php-webdriver,据我所知,我没有变量 $captureScreenshotOnFailure.

I am using PHPUnit 3.6 [reviewed for 9.5] and Facebook's php-webdriver, so as far as I know, I don't have the variable $captureScreenshotOnFailure.

想法?

推荐答案

你可以查看getStatus()的返回值,并在你想要的条件下截图.

You can check the return value from getStatus() and take a screenshot under your desired conditions.

protected function tearDown() {
    $status = $this->getStatus();
    if ($status == PHPUnit_Runner_BaseTestRunner::STATUS_ERROR 
            || $status == PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE) {
        // take a screenshot...
    }
}

请参阅 runBare(),了解根据测试方法抛出的异常设置状态的位置.您可能还想为跳过的测试截取屏幕截图.

See runBare() for where the status is set based on the exception thrown from the test method. You might want to take a screenshot for skipped tests too.

这篇关于有没有办法从 tearDown() 判断我的 PHPUnit 测试用例中是否有故障?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆