views:

66

answers:

1

A search yields a couple of questions on catching fatal exceptions, but this one is specific to using SimpleTest. (I know that SimpleTest is out of date, but getting PHP-Unit to work on my configuration is another question).

I am trying to get the tearDown() method to work even when there is a fatal exception, as I create some test rows in the database during setup, and remove them during tear-down. But when SimpleTest comes to a fatal exception, teardown() is never ran.

Is there a way to get tearDown() to run despite a fatal exeception?

+1  A: 

When a Fatal Error occurs, the PHP process is terminated -- which means there is no way to have that same PHP process executed any kind of addionnal code, as it's no longer there.

This will also mean that :

  • you'll probably not get much reporting
  • the other tests after the one that ends up in a Fatal Error will not run
  • You have a test that is failing -- badly.


You should fix the problem : a Fatal Error in your application is bad ; it's great that you detected it with your automated tests, of course -- but the next step is to make it go away ;-)


As you cannot run anymore PHP code in the same process that has died, the only solution I see would be to launch another process, to run your clean-up code.

The basic idea would be to :

  • Launch your tests
  • Use another, totally distinct, script, to run the clean-up operations

Of course, this means the clean-up will only be done once, after all tests have been run ; but I suppose it's better than nothing.


Now, how to do that in an automated way ?

The simplest solution would probably be to use a shell-script, that runs both commands ; something like this, I'd say :

#!/bin/sh

php /.../launch-tests.php

php /.../cleanup.php

And run your tests by launching that shell-script.

Pascal MARTIN