Last week I blogged about XUnit Collection Fixtures. Something I wasn’t fully aware of but does make sense if you think about it is that by using the [Collection] attribute all tests that share the same context in the same thread. So you don’t need to worry about concurrent test runs.
By default XUnit tests that are part of the same test class will not run in parallel. For example, in the code below Test1 and Test2 will never run at the same time:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestClass1 | |
{ | |
[Fact] | |
public void Test1() | |
{ | |
} | |
[Fact] | |
public void Test2() | |
{ | |
} | |
} |
If you want to achieve the same behavior while having tests in multiple classes, you can put tests in the same collection:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Collection("Test Collection")] | |
public class TestClass1 | |
{ | |
[Fact] | |
public void Test1() | |
{ | |
} | |
} | |
[Collection("Test Collection")] | |
public class TestClass2 | |
{ | |
[Fact] | |
public void Test2() | |
{ | |
} | |
} |