Consider using file resources (sql scripts to be precise). Having SQL stored as strings within class often ends up being maintainability nightmare.
In my C# projects what I usually do is:
- create scripts inserting data required by given test (setup)
- create script reverting database to its state before inserts (teardown)
- add those scripts to project resources (so that they can be stored in .sql/text files and easily run on database when needed, and don't clutter class code)
And run them before/after test starts (within test method body though). For example (pseudocode):
public void DeleteClient_DeletesClientAndOrderHistory()
{
ExecuteSql(Resources.Scripts.DeleteClientTest_SetupScript);
// perform test
ExecuteSql(Resources.Scripts.DeleteClientTest_TeardownScript);
}
Of course, you wrap such constructs with catch-finally
and assure other kinds of revert-to-starting point safety mechanisms. This however will have rather large impact on tests execution time. You can consider two extra options:
- having class-wide setup and teardown, where you insert all data required by tested DAO class (runs once per class)
- having assembly/namespace-wide setup and teardown, where you insert all data required by all DAO classes (runs once per all classes in assembly/namespace)
Naturally, you'll have to check whether your framework supports such methods (for example, NUnit does with [TestFixtureSetup]
(class-wide) [SetUpFixture]
(assembly/namespace-wide) attributes).