简体   繁体   English

NUnit TestFixure和SetUp的嵌套TransactionScope

[英]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: 使用这个我也尝试以相同的方式设置TestFixure事务:

[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. 想法是SetupAllDataForAllTest在开始时运行一次并插入测试依赖的所有基础数​​据。 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. 我现在遇到的问题是,在第一次测试之后,它表明TestFixture事务已经关闭,即使我只希望它关闭SetUp事务。 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 我的假设是,如果你Dispose()和内部事务它会外在的,所以我不知道如何完成我想做的事情

You didn't say what database you use. 您没有说出您使用的数据库。

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 outer transaction as well). 在MS SQL Server中,如果您在另一个事务中进行BEGIN TRANSACTION (使它们嵌套),然后在嵌套事务中使用ROLLBACK TRANSACTION ,它将回滚所有内容(整个外部事务也是如此)。

ROLLBACK TRANSACTION without a savepoint_name or transaction_name rolls back to the beginning of the transaction. 没有savepoint_name或transaction_name的ROLLBACK TRANSACTION回滚到事务的开头。 When nesting transactions, this same statement rolls back all inner transactions to the outermost BEGIN TRANSACTION statement. 嵌套事务时,此同一语句将所有内部事务回滚到最外面的BEGIN TRANSACTION语句。

To be able to roll back only the inner nested transaction it should be started by SAVE TRANSACTION <name> with some name for the nested transaction. 为了能够仅回滚内部嵌套事务,它应该由SAVE TRANSACTION <name>启动,并为嵌套事务指定一些名称。 Then you can ROLLBACK <name> to roll back just the nested part. 然后你可以ROLLBACK <name>来回滚嵌套的部分。

If you are using some other database, it may behave differently in nested transactions. 如果您正在使用其他数据库,则嵌套事务中的行为可能会有所不同。

I have no idea how to make your classes issue correct BEGIN or SAVE TRANSACTION SQL statements depending on whether the transaction is nested or not. 我不知道如何使您的类发出正确的BEGIN or SAVE TRANSACTION SQL语句,具体取决于事务是否嵌套。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM