如何連續運行NUnit測試夾具?

[英]How to run NUnit test fixtures serially?


I have several suites of integration tests implemented in C#/NUNit. Each test suite is a separate class, each fixture setup creates and populates a SQL Server database from scripts. This all used to work just fine prior to Resharper 5.1.

我在C#/ NUNit中實現了幾套集成測試。每個測試套件都是一個單獨的類,每個夾具設置從腳本創建並填充SQL Server數據庫。這一切都曾經在Resharper 5.1之前工作得很好。

Unfortunately, Resharper 5.1 begins to run several fixtures at the same time. This is a breaking change - they are all attempting to create and populate the same database, which obviously ends up in an mess. Is there any way I could have Resharper run my test fixtures serially?

不幸的是,Resharper 5.1開始同時運行多個燈具。這是一個重大改變 - 他們都在嘗試創建和填充相同的數據庫,這顯然最終會陷入混亂。有什么方法可以讓Resharper連續運行我的測試夾具嗎?

If not, what would you suggest to run my NUnit test fixtures serially, one fixture at a time? The order in which individual tests run does not matter.

如果沒有,你會建議連續運行我的NUnit測試夾具,一次一個夾具?單個測試運行的順序無關緊要。

4 个解决方案

#1


22  

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...

我不知道是否有可能阻止ReSharper並行運行測試;如果沒有,以下hack可能會起作用:使用靜態只讀Monitor成員創建一個靜態類。然后,在[TestFixtureSetUp]中,在監視器上調用Enter(),並在[TestFixtureTearDown]中調用監視器上的Exit()。這樣,一次只允許運行一個測試夾具。不漂亮,但......

#2


1  

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.

你確定嗎 ?我只是嘗試了這個...通過在3個差異NUnit燈具中測試下面的形式的跟蹤,然后是“全部運行”。似乎沒有並行運行。

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)

我看到的輸出是:(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

#3


0  

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();
    }
}

#4


0  

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.

給它們字母名稱,即在它們前面加上表示其運行順序的字母。如果你需要這個,你應該能夠接受這個令人討厭的命名約定。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/06/21/7206377516be0aabdf1b9c42f5373b04.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com