-->

Is there a way to tell whether there are failures

2020-08-16 08:51发布

问题:

What's the best way for me to check in PHPUnit whether my test execution succeeded or failed? 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.

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.

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

Thoughts?

回答1:

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...
    }
}

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.



标签: phpunit