Edit on GitHub

xUnit1007 Error

ClassData must point at a valid class

Cause

The type referenced by the [ClassData] attribute does not implement IEnumerable<object[]> or does not have a public parameterless constructor.

Reason for rule

xUnit.net will attempt to instantiate and enumerate the type specified in [ClassData] in order to retrieve test data for the theory. In order for instantiation to succeed, there must be a public parameterless constructor. In order for enumeration to work, the type must implement IEnumerable<object[]>.

How to fix violations

To fix a violation of this rule, make sure that the type specified in the [ClassData] attribute meets all of these requirements:

Examples

Violates

using Xunit;

class xUnit1007_TestData { }

public class xUnit1007
{
    [Theory]
    [ClassData(typeof(xUnit1007_TestData))]
    public void TestMethod(int quantity, string productType)
    { }
}

Does not violate

using System.Collections;
using System.Collections.Generic;
using Xunit;

class xUnit1007_TestData : IEnumerable<object[]>
{
    public IEnumerator<object[]> GetEnumerator()
    {
        yield return new object[] { 12, "book" };
        yield return new object[] { 9, "magnifying glass" };
    }

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

public class xUnit1007
{
    [Theory]
    [ClassData(typeof(xUnit1007_TestData))]
    public void TestMethod(int quantity, string productType)
    { }
}
Copyright © .NET Foundation. Contributions welcomed at https://github.com/xunit/xunit/tree/gh-pages.