我使用 NUnit 进行单元测试,但我想实现一些听起来类似于 xUnit 的 ICollectionFixture 功能的东西。
具体来说,我认为这对于数据库集成测试很有用,因此我可以创建一个新数据库,从多个测试类针对该数据库运行所有测试,并在测试运行结束时删除数据库。
我知道我可以使用
OneTimeSetUp
/OneTimeTearDown
属性为每个测试类执行此操作,但是
在这种情况下,我只想为整个测试运行创建一次数据库。
我可以使用 SetUpFixture
属性对命名空间中的所有测试执行此操作,但是我在这个命名空间中有不需要数据库的测试类,因此必须等待数据库建立会很烦人创建只是为了运行非数据库测试。 我想知道 OnetimeSetUp/SetUpFixture 和单例的混合是否可行,或者 NUnit 的
OneTimeSetUp
和
OneTimeTearDown
创建父抽象测试类。 那么这个共享资源只会在父测试类的子类中使用。NUnit 文档中的更多信息:
https://docs.nunit.org/articles/nunit/writing-tests/attributes/onetimesetup.html#inheritance.
[TestFixture]
public abstract class BaseTests
{
// shared resources initialized only once
protected TestKit _amqpTestKit;
protected HttpClient _httpClient;
[OneTimeSetUp]
public void InitApp()
{
var endpoint = Endpoint.Create(
host: "localhost",
port: 5672,
"test",
"test",
Scheme.Amqp
);
_amqpTestKit = new TestKit(endpoint);
// setup the application
var application = new ApplicationFactory();
// trigger the app to start
_httpClient = application.CreateClient();
}
}