Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combine Class Fixtures and Collection Fixtures in xunit

For running Protractor end to end tests in xunit I want to Combine Class Fixtures and Collection Fixtures in xunit.
I created a collection fixture DatabaseServerFixture[Collection] to run the database and Server, so the database and web services are available for all tests all the time and database/server setup is only done once for all tests for faster execution.
I've set up a second BrowserFixture to share one instance of a browser between all tests in one class, since I want to be able to run tests from different classes parallel, each class owning it's own browser class.

Problem is: I need to reference the BrowserFixture to use in my test classes, so I can't reference the DatabaseServerFixture. And since the DatabaseServerFixture is never referenced, it is not created => no database, so all tests are failing.

I don't need to be able to Access the DatabaseServerFixture from my tests, but I Need it to start before all tests. How do I get xunit to start it even though it seems I'm not using it anywhere?

I tried creating a dummy test which uses the DatabaseServerFixture, but it is not running for the other tests, so it didn't help.

like image 879
Sam Avatar asked Jan 20 '17 15:01

Sam


1 Answers

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. 
   }
}

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!

like image 73
Insight Coder Avatar answered Oct 27 '22 16:10

Insight Coder