Skip to content

Passing collection fixture to a class fixture #2699

Answered by bradwilson
rc14193 asked this question in Question
Discussion options

You must be logged in to vote

Yes, this does work as you'd expect:

using Xunit;

public class Fixture1
{
    public static int CtorCount = 0;

    public Fixture1() => CtorCount++;
}

public class Fixture2
{
    public Fixture1 F1;

    public Fixture2(Fixture1 f1) => F1 = f1;
}

[CollectionDefinition(nameof(MyCollection))]
public class MyCollection : ICollectionFixture<Fixture1>
{ }

[Collection(nameof(MyCollection))]
public class UnitTest1 : IClassFixture<Fixture2>
{
    Fixture2 f2;

    public UnitTest1(Fixture2 f2) => this.f2 = f2;

    [Fact]
    public void Test1()
    {
        Assert.NotNull(f2.F1);
        Assert.Equal(1, Fixture1.CtorCount);
    }
}

[Collection(nameof(MyCollection))]
public class UnitTest2 : 

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
2 replies
@DalekBaldwin
Comment options

@bradwilson
Comment options

Comment options

You must be logged in to vote
2 replies
@maranmaran
Comment options

@bradwilson
Comment options

Answer selected by bradwilson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants