Edit on GitHub

xUnit1041 Warning

Fixture arguments to test classes must have fixture sources

Cause

Test class constructors which indicate fixture data must have a source for those fixtures.

Reason for rule

Fixture data must come from a fixture source. Any constructor argument which is fixture data, but does not have an associated fixture source, is an error condition and every test in the class will fail at runtime.

There are three fixture sources:

For more information on fixtures and shared context, please see the documentation.

Note: Collection definition classes must be defined in the same assembly as the test. You may get this analyzer on your source code if you’ve mistakenly put your collection definition class into the wrong assembly.

Note: Third party fixture source libraries are not supported by this analyzer. For projects which use third party collection sources, you should disable this rule (conditionally or globally). Ideally third party fixture source libraries should provide their own customization of xUnit1041 that includes their own rules in addition to the ones built into this analyzer.

How to fix violations

Provide one of the above mentioned fixture sources for the fixture data.

Examples

Violates

using Xunit;

public class Fixture { }

public class xUnit1041
{
    public xUnit1041(Fixture fixture)
    { }

    [Fact]
    public void TestMethod()
    { }
}

Does not violate

For v2 and v3

using Xunit;

public class Fixture { }

public class xUnit1041 : IClassFixture<Fixture>
{
    public xUnit1041(Fixture fixture)
    { }

    [Fact]
    public void TestMethod()
    { }
}
using Xunit;

public class Fixture { }

[CollectionDefinition(nameof(MyCollection))]
public class MyCollection : IClassFixture<Fixture> { }

[Collection(nameof(MyCollection))]
public class xUnit1041
{
    public xUnit1041(Fixture fixture)
    { }

    [Fact]
    public void TestMethod()
    { }
}
using Xunit;

public class Fixture { }

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

[Collection(nameof(MyCollection))]
public class xUnit1041
{
    public xUnit1041(Fixture fixture)
    { }

    [Fact]
    public void TestMethod()
    { }
}

For v3 only

using Xunit;

[assembly: AssemblyFixture(typeof(Fixture))]

public class Fixture { }

public class xUnit1041
{
    public xUnit1041(Fixture fixture)
    { }

    [Fact]
    public void TestMethod()
    { }
}
Copyright © .NET Foundation. Contributions welcomed at https://github.com/xunit/xunit/tree/gh-pages.