When researching EF & Code First, I asked a question a while ago (ref this thread) reguarding how you keep your tests from overlapping each other. I want to seed the DB and was doing it like this for each test class (using MSTest):
public class CustomerSmokeTests {
private const string CONNECTION_STRING = "server=localhost;database=CPT_CustomerDataSync_SmokeTests;uid=sa;pwd=Password1!;";
private DatabaseFactory _dbfactory;
private CustomerCacheContext _customerContext;
[TestInitialize]
public void Setup() {
// set initializer to create new DB
Database.SetInitializer(new SmokeTestCreateDbWithDataIfNotExists());
Database.SetInitializer(new SmokeTestDropCreateDbWithDataAlways());
// create new DB connection to local SQL Server
_dbfactory = new DatabaseFactory(CONNECTION_STRING);
// connect to DB to auto generate it
_customerContext = _dbfactory.GetDataContext();
}
[TestCleanup]
public void Cleanup() {
_customerContext.Dispose();
_dbfactory.Dispose();
}
// tests
}
However the issue here is that each test creates/tears down the DB (not ideal as they overlap each other and fail... if you run the tests individually they all pass as desired... plus this greatly slows down the tests).
A good solution was to instead wrap them up in TransactionalScopes, however I'd like to ensure at the start of a test run, the DB is regenerated using the seed info (as the seed changes as tests are developed by my devs).
A brute force way to do this would be to create some sort of handler that in the test init, it would check to see if the DB was recently created and if not, create it with the seed info. If not, it would ignore that step. Then it would create a TransactionalScope() that would be rolled back in the test cleanup.
But is there a better way to handle this? Got the over-engineering feel with this brute force appraoch... ideas?