I have the following base object in my Tests
[TestClass]
public abstract class TestClassBase
{
private TransactionScope _transactionScope;
[TestInitialize]
public virtual void TestInitialize()
{
_transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { Timeout = new TimeSpan(0, 10, 0) });
}
[TestCleanup]
public virtual void TestCleanup()
{
_transactionScope.Dispose();
}
}
I have a Test that does the following
[TestMethod]
public void SaveToDatebaseTest(
{
Program program = new Program();
program.Name = "Unit Test Program, I should not exists, when Test is done";
ProgramManager programManager = new ProgramManager()
programManager.Save(program);
}
When I run the test, the records still exists in the database.
I want to avoid using TransactionScope within every test method
If you are using VS2005 or VS2008 (not sure about 2010), you can try MSTestExtensions for automatic database transaction rollback after tests methods are completed. I've used it with MS SQLServer and it's Distributed Transaction Coordinator service running.
I know this may not be exactly what you are looking for, but it may be of help to your situation.