Test collections are the test grouping mechanism in xUnit.net v2. They serve two purposes:
- They delineate the "parallelism" boundary; that is, tests in the same collection will not be run in parallel against each other;
- They offer collection-wide fixtures through the use of
ICollectionFixture<TFixtureType>
.
The simplest way to use test collections is simply by name. Put two classes into the same named collection, and they can derive benefit #1 above:
[Collection("MyCollection")]
public class TestClass1
{
// ... tests here ...
}
[Collection("MyCollection")]
public class TestClass2
{
// ... tests here ...
}
In order to derive benefit #2, you must declare a collection class, so that you have a centralized location to place the ICollectionFixture
interface. Test classes continue to use the [Collection]
attribute:
public class MyFixture
{
// Uses ctor and IDisposable.Dispose to setup/cleanup
}
[CollectionDefinition("MyCollection")
public class CollectionClass : ICollectionFixture<MyFixture> { }
[Collection("MyCollection")]
public class TestClass1
{
// ... tests here ...
}
[Collection("MyCollection")]
public class TestClass2
{
// ... tests here ...
}
The collection fixture will be created once before running any test in the collection, and destroyed after all tests in the collection have been run. If the tests need access to the fixture, they can take it via their constructor:
[Collection("MyCollection")]
public class TestClass1
{
public TestClass1(MyFixture fixture)
{
}
// ... tests here ...
}
I have two classes TestClass1, TestClass2
I have set MaxParallel threads through assembly in AssemblyInfo.cs in my test project :
[assembly: Xunit.CollectionBehaviorAttribute(MaxParallelThreads = 4)]
I have installed xunit-2.0.0-beta4-build2738(Prerelease). Also installed Xunit runner to find the test.
VS 2013 finds the tests but when I run all the tests, it still runs tests serially. I want them to run in parallel.
If you could help that would be great!
My code below:
Thanks,
Dave