【问题标题】:Share state between tests that run in parallel with XUnit.net在与 XUnit.net 并行运行的测试之间共享状态
【发布时间】:2017-01-26 17:04:49
【问题描述】:

我有一组需要共享状态的 xunit.net 测试。希望我希望这些测试能够并行运行。所以我希望跑步者这样做:

  • 创建共享夹具
  • 使用该夹具并行运行所有测试

阅读 xunit 文档时,它说要在测试类之间共享状态,我需要定义一个“集合夹具”,然后将我的所有测试类都放入该新集合中(例如:[Collection("Database collection")])。但是当我把我的测试类放在同一个夹具中时,它们不再并行运行,所以它达到了目的:(

在 XUnit 中是否有内置的方法来做我想做的事情?

我的后备方案是将我的共享状态放入静态类。

【问题讨论】:

  • 你能分享一个你想测试的例子吗?
  • 例如,我需要在数据库中创建一个用户,该用户将以只读模式被一组并行运行的测试共享。
  • 很遗憾,没有明确的示例如何做到这一点。我尝试了一大堆组合,但没有一个有效。如果我得到它的工作,我会在这里发布一个答案。

标签: xunit.net


【解决方案1】:

您可以使用下面粘贴的示例中的AssemblyFixture example 扩展 xUnit,以创建一个夹具,该夹具可以在并行运行时通过测试进行访问。

使用这种方法,fixture 在测试之前创建,然后注入到引用它的测试中。我使用它来创建一个用户,然后共享该特定集合运行。

还有一个nuget包xunit.assemblyfixture

using System;
using Xunit;

// The custom test framework enables the support
[assembly: TestFramework("AssemblyFixtureExample.XunitExtensions.XunitTestFrameworkWithAssemblyFixture", "AssemblyFixtureExample")]

// Add one of these for every fixture classes for the assembly.
// Just like other fixtures, you can implement IDisposable and it'll
// get cleaned up at the end of the test run.
[assembly: AssemblyFixture(typeof(MyAssemblyFixture))]

public class Sample1
{
    MyAssemblyFixture fixture;

    // Fixtures are injectable into the test classes, just like with class and collection fixtures
    public Sample1(MyAssemblyFixture fixture)
    {
        this.fixture = fixture;
    }

    [Fact]
    public void EnsureSingleton()
    {
        Assert.Equal(1, MyAssemblyFixture.InstantiationCount);
    }
}

public class Sample2
{
    MyAssemblyFixture fixture;

    public Sample2(MyAssemblyFixture fixture)
    {
        this.fixture = fixture;
    }

    [Fact]
    public void EnsureSingleton()
    {
        Assert.Equal(1, MyAssemblyFixture.InstantiationCount);
    }
}

public class MyAssemblyFixture : IDisposable
{
    public static int InstantiationCount;

    public MyAssemblyFixture()
    {
        InstantiationCount++;
    }

    public void Dispose()
    {
        // Uncomment this and it will surface as an assembly cleanup failure
        //throw new DivideByZeroException();
    }
}

【讨论】:

    【解决方案2】:

    您不想在测试之间共享状态,您只想共享运行测试所需的设置。您可以在此处阅读有关如何在 xUnit 中执行此操作的所有信息(有漂亮的示例):http://xunit.github.io/docs/shared-context.html

    如果你碰巧使用实体框架,我也在这里写了一些关于它的东西:https://robertengdahl.blogspot.com/2017/01/testing-against-entityframework-using.html

    【讨论】:

    • 你是对的。我用错了词。我想分享 setup 而不是 state。话虽如此,如果我们查看您的第一个链接,我想做的是 CollectionFixture。要为多个测试类重用 same 集合夹具(仅实例化一次),所有测试类必须放在同一个 [Collection] 中。然后,如果它们在同一个 [Collection] 中,它们将不再并行运行。
    • 对不起,我忘了你想并行运行这些。由于并行运行不需要相同的夹具实例,因此您可以将夹具实例作为测试类的成员变量,如第二个链接所示。您将获得并行测试的速度,以及隔离测试的安全性。
    • @RobertJørgensgaardEngdahl 您的链接不再有效。可以更新吗?看起来第一个应该是xunit.net/docs/shared-context。但是,如果是这种情况,那么我看不到有关共享内容的页面如何回答这个问题。 Class Fixtures 共享的数据仅在同一类中的测试之间共享,而 Collection Fixtures 遇到了问题中概述的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 1970-01-01
    • 2019-05-07
    • 2017-08-13
    • 1970-01-01
    相关资源
    最近更新 更多