Skip to content

Instantly share code, notes, and snippets.

@bradwilson
Last active October 8, 2022 04:12
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bradwilson/8423477 to your computer and use it in GitHub Desktop.
Save bradwilson/8423477 to your computer and use it in GitHub Desktop.
Using test collections in xUnit.net v2

Test collections are the test grouping mechanism in xUnit.net v2. They serve two purposes:

  1. They delineate the "parallelism" boundary; that is, tests in the same collection will not be run in parallel against each other;
  2. 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 ...
}
@eddiesholl
Copy link

This feature is awesome, and totally nails a scenario I'm trying to solve. I'm having trouble getting the fixtures to be picked up though. The visual studio and console runners are both failing my tests with:

Result Message: The following constructor parameters did not have matching fixture data: ParentChildBeaconServiceTestFixture serviceFixture

My versions in use are:

  <package id="xunit.core" version="2.0.0-beta4-build2738" targetFramework="net45" />
  <package id="xunit.runner.visualstudio" version="0.99.8" targetFramework="net45" />

Would you expect the v2 functionality (in particular ICollectionFixture) to be available and stable via the current VS runner?

Cheers,

Eddie

@ddave09
Copy link

ddave09 commented Sep 18, 2014

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:

namespace name1
{
public class MFixture
{

}

[CollectionDefinition("Test1")]
public class CollectionClass : ICollectionFixture<MFixture>
{

}

[Collection("Test1")]
public class TestClass1
{
    // variables
    public TestClass1()
    {
        //code
    }

   [Fact]
   public void method1()
   {
       //code
   }

  [Fact]
  public void method2(){
  {
       //code
  }
}
}


namespace name2
{
public class MFixture1
{

}

[CollectionDefinition("Test2")]
public class CollectionClass1 : ICollectionFixture<MFixture1>
{

}

[Collection("Test2")]
public class TestClass2
{
    // variables
    public TestClass2()
    {
        //code
    }

   [Fact]
   public void method12()
   {
       //code
   }

  [Fact]
  public void method22(){
  {
       //code
  }
}
}

Thanks,
Dave

@mikeharder
Copy link

For me, the tests are run in parallel when using xunit.console.exe, but not when using the Visual Studio test runner.

@dde2801
Copy link

dde2801 commented Oct 30, 2015

Is there a short way to tell that all classes in an assembly belong to a collection?

@stajs
Copy link

stajs commented Nov 30, 2015

Visual Studio 2015, DNX, xunit.runner.dnx 2.1.0-rc1-build204. From command line tests are run in parallel. From VS Test Explorer they run serial.

@DunetsNM
Copy link

DunetsNM commented Jun 14, 2017

is there any way to enforce serial execution of tests without explicitly assigning same collection to each test class?
I'm using Stepen Cleary's Calculated Properties - which are not thread-safe - throughout solution both in business and view model layers.

UPD: please ignore, CollectionBehaviorAttribute it is

@azkurban
Copy link

azkurban commented Sep 4, 2020

This feature is awesome, and totally nails a scenario I'm trying to solve. I'm having trouble getting the fixtures to be picked up though. The visual studio and console runners are both failing my tests with:

Result Message: The following constructor parameters did not have matching fixture data: ParentChildBeaconServiceTestFixture serviceFixture

My versions in use are:

  <package id="xunit.core" version="2.0.0-beta4-build2738" targetFramework="net45" />
  <package id="xunit.runner.visualstudio" version="0.99.8" targetFramework="net45" />

Would you expect the v2 functionality (in particular ICollectionFixture) to be available and stable via the current VS runner?

Cheers,

Eddie

Hey Guys, is there someone who answered Eddie's question since 2014? I have the same problem with VS2019
Thanks,
Azamat

@mufasalg0
Copy link

Is there a way to run them in order? (Test Classes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment