在 NUnit 测试中实现类似 ICollectionFixture 的共享上下文

问题描述 投票:0回答:1

我使用 NUnit 进行单元测试,但我想实现一些听起来类似于 xUnit 的 ICollectionFixture 功能的东西。

具体来说,我认为这对于数据库集成测试很有用,因此我可以创建一个新数据库,从多个测试类针对该数据库运行所有测试,并在测试运行结束时删除数据库。

我知道我可以使用

OneTimeSetUp
/
OneTimeTearDown
属性为每个测试类执行此操作,但是 在这种情况下,我只想为整个测试运行创建一次数据库。

我可以使用 SetUpFixture

 属性
对命名空间中的所有测试执行此操作,但是我在这个命名空间中有不需要数据库的测试类,因此必须等待数据库建立会很烦人创建只是为了运行非数据库测试。

我想知道 OnetimeSetUp/SetUpFixture 和单例的混合是否可行,或者 NUnit 的

自定义属性是否有帮助,或者是否有其他方法可以解决这个问题?

nunit
1个回答
0
投票
您可以使用

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(); } }
    
© www.soinside.com 2019 - 2024. All rights reserved.