I understand that by default xUnit will execute tasks within a class in series, but will execute tasks across classes in parallel.
I've created 3 test classes, but They each need common setup data (in a db - yes i know including a db is an unfortunate circumstance, but that inclusion can't change).
So to initialize the data set in the db, I created a fixture:
/// <summary> /// This fixture is used to initialize a known state of test data in the database /// </summary> public class DBFixture { /// <summary> /// Clear out and create new test data. Ensure we have a known state of input data. /// </summary> public DBFixture() { string script = File.ReadAllText("Database\\DataSetup.sql"); using (SqlConnection con = new SqlConnection(...)) { var result = con.QueryMultiple(script); } } } Then to associate the fixture to multiple classes, I create a class that associates the fixture to a collection.
/// <summary> /// /// </summary> /// <see cref="https://xunit.net/docs/shared-context"/> [CollectionDefinition("My Collection")] public class MyCollection: ICollectionFixture<DBFixture> { // This class has no code, and is never created. Its purpose is simply // to be the place to apply [CollectionDefinition] and all the // ICollectionFixture<> interfaces. //This class, and it's CollectionDefinition attribute tell the xUnit framework that any Test class that has a Collection attribute with the same collection name, will use the same instance of DBFixture. } Then I create my test classes, with my tests, and associate them to my collection.
[Collection("My Collection")] public class MyTests { ... } [Collection("My Collection")] public class MyTests2 { ... } When I run all my tests, there does not appear to be any parallelization, and I think it's because now all my test classes are part of the same collection. Is there a way to have a common fixture instance across test class & have parallel execution?