Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to tell whether there are failures in my PHPUnit test case from tearDown()?

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 [reviewed for 9.5] and Facebook's php-webdriver, so as far as I know, I don't have the variable $captureScreenshotOnFailure.

Thoughts?

like image 927
Dan Chan Avatar asked Dec 06 '11 22:12

Dan Chan


1 Answers

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.

like image 56
David Harkness Avatar answered Mar 26 '23 16:03

David Harkness