Nested TransactionScope for NUnit TestFixure and SetUp

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() { // Sql stuff here that will get undone from the TestFixtureTearDown scope dispose } [Test] public void DoSqlStuff1() { // Sql stuff here that will get undone from the TransactionBackedTest } [Test] public void DoSqlStuff2() { // Sql stuff here that will get undone from the TransactionBackedTest } } 

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

+7
c # sql unit-testing nunit transactions
source share
1 answer

You did not say which database you are using.

In MS SQL Server, if you BEGIN TRANSACTION inside another transaction (make them nested) and then ROLLBACK TRANSACTION inside the nested transaction, it will roll back everything (the whole external transaction also).

ROLLBACK TRANSACTION without savepoint or transaction name returns to the start of the transaction. When transactions are nesting, the same operator returns all internal transactions to the most external BEGIN TRANSACTION statement.

To be able to roll back only the internal nested transaction, it should be run SAVE TRANSACTION <name> with some name for the nested transaction. Then you can ROLLBACK <name> roll back only the nested part.

If you use any other database, it can behave differently in nested transactions.

I have no idea how to make your classes valid BEGIN or SAVE TRANSACTION SQL statements, depending on whether the transaction is nested or not.

+3
source share

All Articles