Collection fixture won't inject
Asked Answered
T

9

21

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
    }
}
Turgor answered 31/8, 2015 at 21:1 Comment(0)
T
28

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!

Turgor answered 31/8, 2015 at 21:1 Comment(2)
I was very much on same autopilot. Thanks! You saved me time :) – Valentijn
Thanks for the answer! Same autopilot... πŸ€¦β€β™‚οΈ – Philippic
D
10

In my case, the fixture and collection were in a shared testing assembly. I found that XUnit DI could not find it. So, I had to define a fixture that inherited those classes in the shared assembly to both share the functionality while getting it to register in my test classes.

Dejadeject answered 28/4, 2017 at 18:42 Comment(1)
had the same issue. it is stated on the page under collection-fixture Important note: Fixtures can be shared across assemblies, but collection definitions must be in the same assembly as the test that uses them. – Tribromoethanol
E
8

This can also happen if your Collection's constructor throws an error. You may need to debug that code by alternative means, since the error message provided by xUnit isn't helpful in this case.

Excessive answered 2/8, 2018 at 22:28 Comment(1)
Second this, I forgot to initialize a collection in an entity, and I got the same error as OP. – Sollie
B
7

I had the same error, but for me the issue was that I had forgot to make the CollectionDefinition class public e.g.

Wrong

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

Correct

[CollectionDefinition("Live tests")]
public class IntegrationTestCollection : ICollectionFixture<IntegrationTestFixture>
{
    // Intentionally left blank.
    // This class only serves as an anchor for CollectionDefinition.
}
Binah answered 15/2, 2017 at 21:1 Comment(0)
C
5

In my case, I forgot to inherit from IClassFixture interface...

Wrong...

public class DatabaseIntegrationTests
{

Correct...

public class DatabaseIntegrationTests : IClassFixture<DatabaseFixture>
{
Curative answered 3/12, 2016 at 19:13 Comment(3)
Sadly this is not a solution to the problem. In this case you are just making all of your classes use a fresh instance of the DatabaseFixture each time. Stick a break point in your fixture constructor and see how many times it is hit. – Freer
This got me double checking; that lead me to find I'd autocompleted ICollectionFixture instead of IClassFixture. – Simeon
Collection fixtures are not the same as class fixtures – Laywoman
E
1

Many of our TestFixture classes have similar names. So ensure the test fixture type on the definition matches exactly with the type passed into the constructor on the class containing the tests.

Entertaining answered 15/9, 2017 at 13:28 Comment(0)
F
1

I've just run into this and I had to put the collection definition into the same namespace as the test class. Not just the same assembly.

Freer answered 8/9, 2018 at 13:38 Comment(0)
K
0

In my case, i have two class library.

Tests.Infrastructure.csproj

And

Tests.Web.csproj

Database fixture wont injcet into tests that was stored in Tests.Web.csproj. Fixture was implemented in second class library, Tests.Infrastructore.csproj.

I moved implementation of fixture to Tests.Web.csproj, deleted Infrastructure and all works now

Kanishakanji answered 12/7, 2019 at 7:1 Comment(0)
A
0

According to this issue:

If you have multiple test projects, each project should have its own Fixture and Collection.

You can't use a shared Fixture or Collection between projects.

To be clear, what I believe to be the bug is that we only search for collection definitions in the test assembly, not in all the assemblies.

This does not mean that we would support a single collection fixture instances across test assemblies. Each test assembly runes isolated in its own App Domain. You could still get a new instance of the collection fixture per App Domain

Araujo answered 28/3, 2022 at 7:3 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.