Collection fixture won't inject

Nate Barbettini picture Nate Barbettini · Aug 31, 2015 · Viewed 12.9k times · Source

I'm using xUnit 2.0 collection fixtures to share a common database setup/teardown between a number of different test classes. The fixture also provides some helper properties, so I'm injecting it into each test class.

I recreated the example in the docs, but when I run the test, it fails immediately with:

The following constructor parameters did not have matching fixture data: IntegrationTestFixture fixture

This seems to happen regardless of whether I'm using xUnit Facts or Theories, or which test runner I'm using.


Fixture:

public class IntegrationTestFixture : IDisposable
{
    public IntegrationTestFixture()
    {
        // (setup code)
        this.GeneratedTestName = [randomly generated];
    }

    public void Dispose()
    {
        // (teardown code)
    }

    public string GeneratedTestName { get; private set; }
}

Collection definition:

[CollectionDefinition("Live tests")]
public class IntegrationTestCollection : ICollectionFixture<IntegrationTestFixture>
{
    // Intentionally left blank.
    // This class only serves as an anchor for CollectionDefinition.
}

Test:

[CollectionDefinition("Live tests")]
public class SomeTests
{
    private readonly IntegrationTestFixture fixture;

    public SomeTests(IntegrationTestFixture fixture)
    {
        this.fixture = fixture;
    }

    [Fact]
    public void MyTestMethod()
    {
        // ... test here
    }
}

Answer

Nate Barbettini picture Nate Barbettini · Aug 31, 2015

This was a silly error and it took me a bit to figure out why it wasn't working:

[CollectionDefinition] goes on the collection definition class, but [Collection] goes on the test class. I was on autopilot and didn't notice this.

You'll also get this if you have multiple [CollectionDefinition] attributes with the same name on different classes. Just use one!