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.
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:
- Delegate the GetData method call to the first attribute.
- Loop through the results from the first attribute and check if the length of each object array matches the number of arguments required by the method.
- If so, yield the object array.
- If not, invoke GetData on the next attribute.
- Throw away the first objects from the result in order to continue from where it ran out of objects from the first result set and concatenate the rest to the previous array.
- Recursively repeat this process until all object arrays have the required length.
You may compile the code from the latest trunk version, alternatively the latest build (including strong names) can be downloaded from here.