I get this base class to wrap each individual test in a transaction that rolls back
public abstract class TransactionBackedTest { private TransactionScope _transactionScope; [SetUp] public void TransactionSetUp() { var transactionOptions = new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted, Timeout = TransactionManager.MaximumTimeout }; _transactionScope = new TransactionScope(TransactionScopeOption.Required, transactionOptions); } [TearDown] public void TransactionTearDown() { _transactionScope.Dispose(); } }
Using this, I also tried setting up the TestFixure transaction in the same way:
[TestFixture] class Example: TransactionBackedTest { private TransactionScope _transactionScopeFixure; [TestFixtureSetUp] public void Init() { var transactionOptions = new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted, Timeout = TransactionManager.MaximumTimeout }; _transactionScopeFixure = new TransactionScope(TransactionScopeOption.Required, transactionOptions); SetupAllDataForAllTest(); } [TestFixtureTearDown] public void FixtureTearDown() { _transactionScopeFixure.Dispose(); } public void SetupAllDataForAllTest() {
The idea is that SetupAllDataForAllTest runs once at the beginning and inserts all the underlying data on which the tests are based. This baseline data should be deleted / rolled back after the tests are completed.
I also want each test to be isolated so that they cannot interfere with each other.
The problem that I am facing right now is that after the first test, she claims that the TestFixture transaction was closed, although I only wanted to close the SetUp transaction. My assumption is that if you Dispose() and the inner transaction dipolizes the outer, so I'm not sure how to accomplish what I want to do
c # sql unit-testing nunit transactions
LearningJrDev
source share