lundi 31 août 2015

Nested TransactionScope for NUnit TestFixure and SetUp

I derive from this base class in order to enclose each indivdual test into a transaction that is rolled 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 to setup a TestFixure transaction 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 being that SetupAllDataForAllTest is ran once at the beginning and inserts all the base data that tests rely on. This base data needs to be deleted/rolledback once the tests are complete.

I also want each test isolated so they cannot interfere with each other as well.

The issue I am having right now is that after the first test, it states the TestFixture transaction has been closed, even though I only wanted it to close the SetUp transaction. My assumption is that if you Dispose() and inner transaction it diposes the outer, so I am not sure how to accomplish what I want to do

Aucun commentaire:

Enregistrer un commentaire