在 运行 与 XUnit.net 并行的测试之间共享状态
Share state between tests that run in parallel with XUnit.net
我有一组 xunit.net 测试需要共享状态。希望这些测试能够并行进行 运行。所以我希望 运行ner 做:
- 创建共享夹具
- 运行 并行 使用该夹具的所有测试
在阅读 xunit 文档时,它说要在测试 classes 之间共享状态,我需要定义一个 'collection fixture' 然后但是我所有的测试 classes该新系列(例如:[Collection("Database collection")]
)。但是,当我将我的测试 class 放在同一个夹具中时,它们不再并行 运行 所以它达到了目的:(
在 XUnit 中是否有内置的方法来做我想做的事情?
我的后备方案是将我的共享状态设为静态 class。
您不想在测试之间共享状态,您只想将测试所需的设置共享给 运行。您可以在此处阅读有关如何在 xUnit 中执行此操作的所有信息(有很好的示例):http://xunit.github.io/docs/shared-context.html
如果你碰巧使用 Entity Framework 我也在这里写了一些关于它的东西:https://robertengdahl.blogspot.com/2017/01/testing-against-entityframework-using.html
您可以使用下面粘贴的示例中的 AssemblyFixture example 扩展 xUnit,以创建一个可以在并行 运行 时通过测试访问的固定装置。
使用这种方法,夹具在测试之前创建,然后注入到引用它的测试中。我用它来创建一个用户,然后为该特定集 运行.
共享
还有一个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();
}
}
我有一组 xunit.net 测试需要共享状态。希望这些测试能够并行进行 运行。所以我希望 运行ner 做:
- 创建共享夹具
- 运行 并行 使用该夹具的所有测试
在阅读 xunit 文档时,它说要在测试 classes 之间共享状态,我需要定义一个 'collection fixture' 然后但是我所有的测试 classes该新系列(例如:[Collection("Database collection")]
)。但是,当我将我的测试 class 放在同一个夹具中时,它们不再并行 运行 所以它达到了目的:(
在 XUnit 中是否有内置的方法来做我想做的事情?
我的后备方案是将我的共享状态设为静态 class。
您不想在测试之间共享状态,您只想将测试所需的设置共享给 运行。您可以在此处阅读有关如何在 xUnit 中执行此操作的所有信息(有很好的示例):http://xunit.github.io/docs/shared-context.html
如果你碰巧使用 Entity Framework 我也在这里写了一些关于它的东西:https://robertengdahl.blogspot.com/2017/01/testing-against-entityframework-using.html
您可以使用下面粘贴的示例中的 AssemblyFixture example 扩展 xUnit,以创建一个可以在并行 运行 时通过测试访问的固定装置。
使用这种方法,夹具在测试之前创建,然后注入到引用它的测试中。我用它来创建一个用户,然后为该特定集 运行.
共享还有一个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();
}
}