I am writing tests for our system that does joins across multiple databases. I chose to use MySQL for my test databases, i.e. test_database1 and test_database2 which I create by dumping my production schemas into the respective tables. Note that I don’t need any data at this point, only the schemas.
What is the best way for me to get these databases cleared out before I run a test? I have two requirements:
- There are some foreign key constraints which prevents me from just being able to TRUNCATE, so I need each table cleared out “manually”.
- I need both databases cleared out.
I looked into overriding:
class PHPUnit_Extensions_Database_Operation_Truncate implements PHPUnit_Extensions_Database_Operation_IDatabaseOperation
in DbUnit but it seems to rely on the DataSet contents which in my case will be empty since I preload my schema via mysqldump –no-data.
Any pointers?
You could create a utility function that rebuilds the database which can be called before tests which require a clean database or in you setUp() function:
This may not be the most performant approach but you will get a clean database for every test.
If you are willing or can to switch from MySQL for your tests, I have had success building an template sqlite database file and copying that file to a different location for testing. Each test just overwrites the test database file with the clean template file.
Note: Exec command are written for demonstration purposes only and have not been tested for exact switches/parameters.