据我所知,xUnit没有全局初始化/拆卸扩展点。但是,创建一个很容易。只需创建一个实现IDisposable
的基本测试类,并在构造函数中执行初始化,并在IDisposable.Dispose
方法中执行初始化。这应该是这样的:
public abstract class TestsBase : IDisposable
{
protected TestsBase()
{
// Do "global" initialization here; Called before every test method.
}
public void Dispose()
{
// Do "global" teardown here; Called after every test method.
}
}
public class DummyTests : TestsBase
{
// Add test methods
}
然而,基类的安装和拆卸的代码将在每次调用执行。这可能不是你想要的,因为它不是很有效。更优化的版本将使用IClassFixture<T>
接口来确保仅调用一次全局初始化/拆卸功能。对于这个版本,你不会从你的测试类扩展基类,但其实现的IClassFixture<T>
界面,T
指的是您的夹具类:
using Xunit;
public class TestsFixture : IDisposable
{
public TestsFixture()
{
// Do "global" initialization here; Only called once.
}
public void Dispose()
{
// Do "global" teardown here; Only called once.
}
}
public class DummyTests : IClassFixture<TestsFixture>
{
public void SetFixture(TestsFixture data)
{
}
}
这将导致TestsFixture
构造函数只被运行一次 为每个被测试的类。因此它取决于你想要在两种方法之间进行选择。
我猜这里是答案:http://stackoverflow.com/questions/12379949/xunit-resharper-how-to-run-setup-code-only-once –