Why doesn't my test run tearDownAfterTestClass when it fails

Posted by Memor-X on Stack Overflow See other posts from Stack Overflow or by Memor-X
Published on 2013-10-17T22:08:19Z Indexed on 2013/10/20 21:54 UTC
Read the original article Hit count: 269

Filed under:
|

in a test i am writing the setUpBeforeTests creates a new customer in the database who is then used to perform the tests with so naturally when i finish the test i should get rid of this test customer in tearDownAfterTestClass so that i can create then anew when i rerun the test and not get any false positives

how when the tests all run fine i have no problem but if a test fails and i go to rerun it my setUpBeforeTests fails because i check for mysql errors in it like this

try
{
    if(!mysqli_query($connection,$query))
    {
        $this->assertTrue(false);
    }
}
catch (Exception $exc)
{
    $msg = '[tearDownAfterTestClass] Exception Error' . PHP_EOL . PHP_EOL;
    $msg .= 'Could not run query - '.mysqli_error($connection). PHP_EOL;;
    $this->fail($msg);
}

the error i get is that there is a primary key violation which is expected cause i'm trying to create a new customer using the same data (primary key is on email which is also used to log in) however that means when the test failed it didn't run tearDownAfterTestClass

now i could just move everything in tearDownAfterTestClass to the start of setUpBeforeTests however to me that seems like bad programming since it defeates the purpose of even have tearDownAfterTestClass

so i am wondering, why isn't my tearDownAfterTestClass running when a test fails

NOTE: the database is a fundamental part of the system i'm testing and the database and system are on a separate development environment not the live one, the backup files for the database are almost 2 GBs and takes almost 1/2 an hour to restore, the purpose of the tear down is to remove any data we have added because of the test so that we don't have to restore the database every time we run the tests

© Stack Overflow or respective owner

Related posts about selenium

Related posts about phpunit