【问题标题】:combine Class Fixtures and Collection Fixtures in xunit在 xunit 中结合类夹具和集合夹具
【发布时间】:2017-01-20 15:56:44
【问题描述】:

为了在 xunit 中运行 Protractor 端到端测试,我想在 xunit 中组合 Class Fixtures and Collection Fixtures
我创建了一个集合夹具 DatabaseServerFixture[Collection] 来运行数据库和服务器,因此数据库和 Web 服务始终可用于所有测试,并且数据库/服务器设置只对所有测试进行一次以加快执行速度。
我已经设置了第二个BrowserFixture 来在一个类中的所有测试之间共享一个浏览器实例,因为我希望能够并行运行来自不同类的测试,每个类都拥有自己的浏览器类。

问题是:我需要引用BrowserFixture 才能在我的测试类中使用,所以我不能引用DatabaseServerFixture。而且由于DatabaseServerFixture 从未被引用,因此它没有被创建=> 没有数据库,所以所有测试都失败了。

我不需要能够从我的测试中访问DatabaseServerFixture,但我需要在所有测试之前启动它。我如何让 xunit 启动它,即使它似乎我没有在任何地方使用它?

我尝试创建一个使用 DatabaseServerFixture 的虚拟测试,但它没有为其他测试运行,所以它没有帮助。

【问题讨论】:

  • 你的测试类不能实现IClassFixture<BrowserFixture>[Collection("DatabaseServerFixtureCollection")]装饰吗?
  • 实际上,我最终在我的 Fixture 中为 TestServer 提供了一个静态类,它运行良好并且能够对测试进行多线程处理。

标签: c# testing protractor xunit end-to-end


【解决方案1】:

我也有类似的需求,但由于类夹具需要来自集合夹具的信息进行初始化,这一点变得复杂。尽管我在 xUnit 中找不到它的文档,但依赖注入似乎可以很好地处理这种情况。我能够使它按如下方式工作(以您的课程为例):

public class BrowserFixture: IDisposable {
  public BrowserFixture(DatabaseServerFixture dbFixture) {
     // BrowserFixture initialization with dbFixture dependencies.
  }
  public void Dispose(){}
}

[Collection("DatabaseServerFixtureCollection")]
public void BrowserTests: IClassFixture<BrowserFixture> {
   public BrowserTests(
      DatabaseServerFixture dbFixture, 
      BrowserFixture browserFixture) {
      // BrowserTests initialization here. 
   }
}

这与 Mickaël Derriey 提到的类似,但将DatabaseServerFixture 额外注入到BrowserFixture 中,我发现这是一个常见的用例。希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 2021-08-08
    • 1970-01-01
    • 2018-04-30
    相关资源
    最近更新 更多