What’s the best way to initialize a database during test setup/teardown?
There are two main approaches:
- Establish a “golden” database that corresponds with your test cases. Drop the old database in the test environment, then load the entire golden copy, just as if you were restoring it.
- Set up the database once, then simply use it throughout your testing.
The restore/drop approach is cleanest, but even that can have some gnarly problems: date and time sensitive test cases might require database dates within a certain range of the test run; there may be external databases/services that require live synch, etc.
If DB fields contain confidential data, you may have to obfuscate certain fields (and their dependent values), or generate and use a synthetic population. It might be easier to copy/subset a live database, parameterize your test cases (i.e., make them data-driven), then generate a consistent test suite from the extracted data. You’d drop this DB after each test run, unless needed as archival evidence.