Class ClassAutoDataAttribute

Namespace: AutoFixture.Xunit3
Assembly: AutoFixture.Xunit3.dll

Provides a data source for a data theory, with the data coming from a class which must implement IEnumerable<object?[]>, combined with auto-generated data specimens generated by AutoFixture.

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
[CLSCompliant(false)]
public class ClassAutoDataAttribute : DataAttribute

Inheritance

DataAttribute ← ClassAutoDataAttribute

Constructors

ClassAutoDataAttribute(Type, params object[])

Initializes a new instance of the ClassAutoDataAttribute class.

public ClassAutoDataAttribute(Type sourceType, params object[] parameters)

Parameters

sourceType Type

The type of the class that provides the data.

parameters object[]

The parameters passed to the data provider class constructor.

ClassAutoDataAttribute(Func<IFixture>, Type, params object[])

Initializes a new instance of the ClassAutoDataAttribute class.

protected ClassAutoDataAttribute(Func<IFixture> fixtureFactory, Type sourceType, params object[] parameters)

Parameters

fixtureFactory Func<IFixture>

The fixture factory that provides missing data from sourceType.

sourceType Type

The type of the class that provides the data.

parameters object[]

The parameters passed to the data provider class constructor.

Examples

In the following example MyTestData is a class that provides test data, that would be complicated or probably impossible to provide using other options. The missing arguments for the test are being supplied from the Fixture instance.

[Theory]
[CustomAutoClassData(typeof(MyTestData))]
public void ClassDataSuppliesExtraValues(int sum, int[] numbers, Person client)
{
    var actual = numbers.Sum(x => x);

    Assert.Equal(sum, actual);
    Assert.NotNull(client);
}

private class CustomAutoClassData : ClassAutoDataAttribute
{
    public CustomAutoClassData(Type sourceType) :
        base(() => new Fixture(), sourceType)
    {
    }
}

private class MyTestData : IEnumerable<object[]>
{
    public IEnumerator<object[]> GetEnumerator()
    {
        yield return new object[] { 0, new int[0] };
        yield return new object[] { 4, new int[] { 1, 2, 1} };
        yield return new object[] { 23, new int [] { 0, 13, 15, -5 } };
    }

    IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
}

Properties

FixtureFactory

Gets the fixture factory that provides the missing data from SourceType.

public Func<IFixture> FixtureFactory { get; }

Property Value

Func<IFixture>

Parameters

Gets the constructor parameters for SourceType.

public object[] Parameters { get; }

Property Value

object[]

SourceType

Gets the type of the class that provides the data.

public Type SourceType { get; }

Property Value

Type

Methods

GetData(MethodInfo, DisposalTracker)

public override ValueTask<IReadOnlyCollection<ITheoryDataRow>> GetData(MethodInfo testMethod, DisposalTracker disposalTracker)

Parameters

testMethod MethodInfo

disposalTracker DisposalTracker

Returns

ValueTask<IReadOnlyCollection<ITheoryDataRow>>

SupportsDiscoveryEnumeration()

public override bool SupportsDiscoveryEnumeration()

Returns

bool