λ>_

Combining Data Theories in AutoFixture.Xunit extension

25 August 2011

xUnit.net supports parameterized tests via data theories which are types deriving from the Xunit.Extensions.DataAttribute type. Some popular attributes include:

InlineDataAttribute, PropertyDataAttribute and ClassDataAttribute.

AutoFixture.Xunit extension includes a very useful type, for providing auto-data theories, called AutoMoqDataAttribute

Imagine a Scenario where we have a unit test method with 3 parameters. We want the first parameters to be supplied by InlineData and the rest by AutoFixture using AutoMoqData.

[Theory]
[InlineAutoData("foo")]
[InlineAutoData("foo", "bar")]
public void Test(string s1, string s2, MyClass myClass)
{
}

This is now possible using the InlineAutoDataAttribute type which is now on the trunk and will be available on next public release (after version 2.1). It provides a data source for a data theory, with the data coming from inline values combined with auto-generated data specimens generated by AutoFixture.

InlineAutoDataAttribute derives from CompositeDataAttribute, an implementation of DataAttribute that composes other DataAttribute instances.

Here is how it works:

While this feature comes with AutoFixture.Xunit extension, the CompositeDataAttribute class depends only on the xunit.extensions assembly.

You may compile the code from the latest trunk version, alternatively the latest build (including strong names) can be downloaded from here.