I’m working on a website that has a lot of unit tests, each needing the database to be in some ‘default’ state. Right know I’m destroying the database with each test and loading the default state using an SQL script. And it’s very slow. So I thought that it would be useful if I could save that default state and restore the database to it at the beginning of each test.
Is this possible ? If not, why could I do to accelerate the process ? Right know I drop the entire database and re-create it. If I dropped only the rows wouldn’t that improve the performance ?
I’m working on a website that has a lot of unit tests, each needing
Share
You can set up the database to default state before all of your tests and then wrap each test in a transaction. After test finishes, you just rollback the transaction.
This approach is especially suitable when you want to run your test in concurrence, as they do not depend on any shared state.
However there are use cases where it does not fit. For example when there are multiple processes accessing the database involved in a test scenario, which is a common case in integration tests.
The real world example of such situation: Test runner (process #1) setting up data in database and phantomjs running in the background making request to app server (process #2). You can’t both test runner and app server share a single database transaction.