Skip to content
\n

\"image\"

\n

Note that the discussion about \"creation order\" is when class fixtures depend on class fixtures (or collection fixtures depend on collection fixtures). Since collection fixtures and class fixtures are created at different times by definition, you can always be sure that collection fixtures are created before class fixtures.

","upvoteCount":4,"url":"https://github.com/xunit/xunit/discussions/2699#discussioncomment-5665922"}}}

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 6 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
4 replies
@maranmaran
Comment options

@bradwilson
Comment options

@aeLeszekLichowski
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
5 participants