tags:

views:

34

answers:

2

I'm testing that a function properly adds data to a db, but I want the test data removed after the test is finished. If the test fails, it quits at the failure and never gets the chance to delete the test rows.

It's the only test that hits the db, so I don't really want to do anything in the tearDown() method.

I'm testing an $obj->save() type method that saves data parsed from a flat file.

+3  A: 

You should use separate databases for development/production and testing. Which are identical in structure but every time you perform testing you drop the testing db and restore it from some data fixtures. The point is that this way you can be absolutely sure that your db contains the same set of data every time you run your tests. So deleting test data is no big deal.

Wallgate
I agree this is how it should be done, I can't imagine convincing my office to start doing this though.
PMV
+2  A: 

Are you using the suggested approach for database testing via the Database Testcase Extension?

Basically, if the test fails (read if there is not an error that makes PHPUnit exit), there should be no issues because the database is seeded on startup of the testcase:

the default implementation in PHPUnit will automatically truncate all tables specified and then insert the data from your data set in the order specified by the data set.

so there should be no need to do that manually. Even if there is an error, PHPUnit will clear the table on next run.

Gordon