我正在运行一组集成测试,虽然大多数测试都是在合理的时间内完成的,但是有两个测试在等待特定的条件(准确地说是金融市场条件),它们可以持续2-3个小时。因此,理想情况下,我想实现两件事:
在NUnit/XUnit (或其他测试运行程序)中有实现这一目标的方法吗?
发布于 2020-08-17 06:46:51
发布于 2020-08-17 15:51:11
在其他测试完成后启动这两个测试
您可以将这两个测试保存在一个独立的nunit测试项目中,从而允许您单独运行所有其他测试。
对于并行运行测试,这个博客有一篇很好的文章:
https://blog.sanderaernouts.com/running-unit-tests-in-parallel-with-nunit
使用可参数化属性标记测试夹具,并将并行作用域设置为ParallelScope.All.
IDisposable.
respectively.
中
[TestFixture]
[Parallelizable(ParallelScope.All)]
public class MyClassTests {
[Test]
public void MyParallelTest() {
using(var scope = new TestScope()) {
scope.Sut.DoSomething();
scope.Repository.Received(1).Save();
}
}
private sealed class TestScope : IDisposable {
public IRepository Repository{get;}
public MyClass Sut {get;}
public TestScope() {
Repository = Substitute.For<IRepository>();
Sut = new MyClass(Repository);
}
public void Dispose() {
//clean-up code goes here
Repository?.Dispose()
}
}
}您应该采取预防措施,以确保在并行运行时,您的测试不会相互干扰。
正如该条所述:
如何安全地并行运行测试
为了允许测试并行运行而不会相互干扰,我已经应用了以下模式一段时间:
[TestFixture]
[Parallelizable(ParallelScope.All)]
public class MyClassTests {
[Test]
public void MyParallelTest() {
using(var scope = new TestScope()) {
scope.Sut.DoSomething();
scope.Repository.Received(1).Save();
}
}
private sealed class TestScope : IDisposable {
public IRepository Repository{get;}
public MyClass Sut {get;}
public TestScope() {
Repository = Substitute.For<IRepository>();
Sut = new MyClass(Repository);
}
public void Dispose() {
//clean-up code goes here
Repository?.Dispose()
}
}
}这篇文章提供了更有价值的建议。我建议读一读,并感谢作者。
https://stackoverflow.com/questions/63331697
复制相似问题