如何串行运行 NUnit 测试装置?

发布于 2024-11-16 20:30:06 字数 277 浏览 3 评论 0原文

我有几套在 C#/NUNit 中实现的集成测试。每个测试套件都是一个单独的类,每个夹具设置都会从脚本创建并填充 SQL Server 数据库。在 Resharper 5.1 之前,这一切都运行得很好。

不幸的是,Resharper 5.1 开始同时运行多个装置。这是一个重大变化 - 他们都试图创建和填充相同的数据库,这显然最终会陷入混乱。有什么方法可以让 Resharper 连续运行我的测试装置吗?

如果没有,您建议如何连续运行我的 NUnit 测试装置,一次运行一个装置? 各个测试的运行顺序并不重要。

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.

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蘸点软妹酱 2024-11-23 20:30:06

我不知道是否可以阻止ReSharper并行运行测试;如果没有,以下 hack 可能会起作用:创建一个带有静态只读 Monitor 成员的静态类。然后,在[TestFixtureSetUp]中,在监视器上调用Enter(),并在[TestFixtureTearDown]中调用监视器上的Exit() ]。这样,一次只允许运行一个测试装置。不过不漂亮...

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

无人接听 2024-11-23 20:30:06

你确定吗?我刚刚尝试过这一点.. 通过在 3 个 diff NUnit 固定装置中的测试中放置以下形式的跟踪,然后是“全部运行”。似乎没有并行运行。

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

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

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
烂柯人 2024-11-23 20:30:06

除非被测试的代码有自己的事务管理,否则您可以在事务内运行每个测试。这样,不同的测试就不会互相干扰。此外,您不必担心每次测试后的清理工作,因为每个测试的事务可以在测试完成后简单地中止。

在我们的项目中,我们通常让集成测试派生自一个或多或少像这样的类:

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

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();
    }
}
国际总奸 2024-11-23 20:30:06

给它们按字母顺序命名,即在它们前面加上一个表示它们运行顺序的字母。如果您需要这种情况发生,您也应该能够接受这种令人讨厌的命名约定。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文