TestFixtureData
The TestFixtureData class provides specific instance information for a parameterized fixture, although any object
deriving from TestFixtureParameters may be used. Unlike NUnit 2, you cannot implement IFixtureData, you must derive
from TestFixtureParameters.
The following example varies the example shown under TestFixture Attribute by using a
TestFixtureSourceAttribute with a data source in a separately defined class.
[TestFixtureSource(typeof(MyFixtureData), nameof(MyFixtureData.FixtureParams))]
public class ParameterizedTestFixture
{
private readonly string _eq1;
private readonly string _eq2;
private readonly string? _neq;
public ParameterizedTestFixture(string eq1, string eq2, string? neq)
{
_eq1 = eq1;
_eq2 = eq2;
_neq = neq;
}
public ParameterizedTestFixture(string eq1, string eq2)
: this(eq1, eq2, null) { }
public ParameterizedTestFixture(int eq1, int eq2, int neq)
{
_eq1 = eq1.ToString();
_eq2 = eq2.ToString();
_neq = neq.ToString();
}
[Test]
public void TestEquality()
{
Assert.That(_eq2, Is.EqualTo(_eq1));
Assert.That(_eq2.GetHashCode(), Is.EqualTo(_eq1.GetHashCode()));
}
[Test]
public void TestInequality()
{
Assert.That(_neq, Is.Not.EqualTo(_eq1));
if (_neq != null)
{
Assert.That(_neq.GetHashCode(), Is.Not.EqualTo(_eq1.GetHashCode()));
}
}
}
public class MyFixtureData
{
public static IEnumerable FixtureParams
{
get
{
yield return new TestFixtureData("hello", "hello", "goodbye");
yield return new TestFixtureData("zip", "zip");
yield return new TestFixtureData(42, 42, 99);
}
}
}
TestFixtureData supports the following properties and methods, which may be appended to an instance in any order.
Explicit()orExplicit(string)causes the fixture to be marked explicit, optionally specifying the reason for doing so.Ignore(string)causes the fixture to be ignored and specifies the reason, which is required.SetArgDisplayNames(string[])sets the list of display names to use as the parameters in the fixture name.SetArgDisplayNames(object[])sets the list of display names using objects that are automatically formatted. (NUnit 4.5+)TypeArgsspecifies theTypes to be used when targeting a generic fixture.
The following example uses the fluent interface to give each fixture a readable name, and to mark one fixture explicit and another ignored.
[TestFixtureSource(typeof(ServerFixtureData), nameof(ServerFixtureData.Servers))]
public class ServerTests
{
private readonly Uri _server;
public ServerTests(Uri server)
{
_server = server;
}
[Test]
public void UsesHttps()
{
Assert.That(_server.Scheme, Is.EqualTo(Uri.UriSchemeHttps));
}
}
public class ServerFixtureData
{
public static IEnumerable Servers
{
get
{
// Fixture is named ServerTests(Production) instead of ServerTests(https://example.com/)
yield return new TestFixtureData(new Uri("https://example.com/"))
.SetArgDisplayNames("Production");
yield return new TestFixtureData(new Uri("https://staging.example.com/"))
.SetArgDisplayNames("Staging")
.Explicit("Only run against staging on demand");
yield return new TestFixtureData(new Uri("https://legacy.example.com/"))
.SetArgDisplayNames("Legacy")
.Ignore("Legacy server is being decommissioned");
}
}
}
Note
Unlike TestCaseData, TestFixtureData has no public SetName method. To control how a fixture is named, use
SetArgDisplayNames.