How to run NUnit test fixtures serially?

假装没事ソ 提交于 2019-11-30 04:49:56

I don't know whether it is possible to prevent ReSharper from running tests in parallel; if not, the following hack might work: Create a static class with a static readonly Monitor member. Then, in [TestFixtureSetUp], call Enter() on the monitor, and call Exit() on the monitor in [TestFixtureTearDown]. That way, only one test fixture will be allowed to run at a time. Not pretty, though...

Are you sure about this ? I just tried this out.. by putting a trace of the following form in tests in 3 diff NUnit fixtures followed by a "Run all". Doesn't seem to be running in parallel.

Trace.WriteLine(DateTime.Now.ToString("hh:mm:ss.ffff") + "VC:Start");
Trace.WriteLine(DateTime.Now.ToString("hh:mm:ss.ffff") + "VC:Done");

Output I see is : (R# Build 5.1.1753.1)

01:06:41.6639IOC
01:06:41.6649Done - IOC

01:06:41.6679VC:Start
01:06:41.6729VC:Done

01:06:41.2439Mef
01:06:41.6589Mef-Done

Unless the code being tested does its own transaction management, you can run each test inside a transaction. In that way, the different tests won't disturb each other. Also, you don't have to worry about cleaning up after each test, since the each test's transaction can simply abort after the test is completed.

In our projects, we usually let our integration tests derive from a class that looks more or less like this:

public class TransactionalTestFixture
{
    private TransactionScope TxScope;

    [SetUp]
    public void TransactionalTestFixtureSetUp()
    {
        TxScope = new TransactionScope(TransactionScopeOption.RequiresNew, 
                                       new TransactionOptions {IsolationLevel = IsolationLevel.Serializable});
    }

    [TearDown]
    public void TransactionalTestFixtureTearDown()
    {
        TxScope.Dispose();
    }
}

Give them alphabetical names, i.e. prefix them with a letter that signifies their running order. If you need this to happen you should be able to accept this nasty naming convention too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!