Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment
@eddiesholl

eddiesholl Aug 31, 2014

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

eddiesholl commented Aug 31, 2014

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

This comment has been minimized.

Show comment
Hide comment
@ddave09

ddave09 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

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

This comment has been minimized.

Show comment
Hide comment
@mikeharder

mikeharder Oct 30, 2014

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

mikeharder commented Oct 30, 2014

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

@dde2801

This comment has been minimized.

Show comment
Hide comment
@dde2801

dde2801 Oct 30, 2015

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

dde2801 commented Oct 30, 2015

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

@stajs

This comment has been minimized.

Show comment
Hide comment
@stajs

stajs 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.

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

This comment has been minimized.

Show comment
Hide comment
@DunetsNM

DunetsNM 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

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

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