I'm trying to setup integration tests in .NET using xUnit and a single IHost
Reading the documentation of xUnit I found a way to share context (https://xunit.net/docs/shared-context) between by tests using Class Fixtures and Collection Fixtures, but for shared context in xUnit the tests runs sequentially and I wanted to create parallel tests.
Right now I have an AppFactory
public class AppFactory : WebApplicationFactory<IAppMarker> { ... }
an collection
[CollectionDefinition("AppCollection")]
public class AppCollection : ICollectionFixture<AppFactory> { }
and some tests classes that inherits from a a base test class
[Collection("AppCollection")]
public class BaseTest {
...
public BaseTest(
AppFactory appFactory
) {
AppFactory = appFactory;
HttpClient = appFactory.CreateClient();
}
...
}
public class Test1 : BaseTest { ... }
public class Test2 : BaseTest { ... }
The problem with this approach is that it runs sequentially in the collection, but uses a single IHost created in the collection
Reading the docs from Microsoft (https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-7.0) and checking the Sample App I saw that they used the IClassFixture and are creating many IHosts for the tests, making it parallel in Class Level.
the problem with this approach is that there are many IHosts (one per class) and the tests are parallel by class level
My idea to parralelize in method level is a bad idea? Is it possible to paralelize in method level with a single IHost using xUnit? Is is a good a approach to have a single IHost?