在 xunit 中结合 Class Fixture 和 Collection Fixture

Sam*_*Sam 5 c# testing xunit end-to-end protractor

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

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

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

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

小智 9

I had a similar need which was complicated by the fact that the class fixture needed information from the collection fixture for initialization. Although I couldn't find it documented in xUnit, it appears that the dependency injection can handle this case just fine. I was able able to get it to work as follows (using your classes as an example):

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. 
   }
}
Run Code Online (Sandbox Code Playgroud)

This is similar to what Mickaël Derriey had mentioned, but with the additional injection of the DatabaseServerFixture into the BrowserFixture which I find to be a common use case. Hope this helps!