Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run a function after every PHPUnit test failure?

I'm using PHPUnit and Selenium 2 to do some integration tests on my web app. I would like a screenshot to be saved every time a test fails. Here is what I have so far:

<?php
include 'c:/wamp/www/testarea/selenium/phpunit-selenium/vendor/autoload.php';

class WebTest extends PHPUnit_Extensions_Selenium2TestCase
{       
    protected function setUp()
    {
        $this->setBrowser('firefox');
        $this->setBrowserUrl('http://www.google.com/');
    }

    public function testTitle()
    {
        $this->url('http://www.google.com/');
        file_put_contents("c:/wamp/www/testarea/selenium/phpunit-selenium/screenshots/screenshot1.png",$this->currentScreenshot());
        $this->assertEquals('NOT GOOGLE', $this->title());
    }

}

This works fine, and saves a screenshot when the test is run - however, I would like to be able to save a screenshot only after a test failure, and this should happen with every test. Is there a way of telling PHPUnit to automatically run a function after every test failure?

Thanks

like image 966
user1578653 Avatar asked Oct 03 '13 11:10

user1578653


1 Answers

Try to use the method tearDown() or onNotSuccessfulTest()

http://phpunit.de/manual/current/en/fixtures.html

like image 158
Ricardo Simas Avatar answered Sep 27 '22 18:09

Ricardo Simas