Skip to content
Alex Povar edited this page Oct 17, 2017 · 7 revisions

This page contains the well known issues with AutoFixture and its integration libraries.

Test name strategies for NUnit3

The AutoData and InlineAutoData attributes shipped with AutoFixture.NUnit3 glue library usually produce unique argument values per each test enumeration. Currently NUnit3 test framework doesn't natively support tests with dynamic parameter values (opposed to xUnit which provides such support). Such tests could confuse some NUnit test runners (e.g. Visual Studio Test Runner, NCrunch) as they are unable to match the tests after discovery and, therefore, run them.

To solve this issue you can specify a test name strategy for the AutoData and InlineAutoData attributes. To change the strategy, you should derive from the AutoDataAttribute or InlineAutoDataAttribute classes and set the TestMethodBuilder property to required value. Later you should those custom attributes instead of the predefined ones.

For now only two test method builders are available:

  • VolatileNameTestMethodBuilder - test name includes the argument values (default NUnit behavior, incompatible with some runners).
  • FixedNameTestMethodBuilder - test name includes the placeholders instead of dynamically generated values.

In v3 the default strategy is VolatileNameTestMethodBuilder. That was done for the backward compatibility with the existing behavior.
In v4 the default strategy is FixedNameTestMethodBuilder. That change has been applied for better compatibility with test runners out-of-the-box.

Usage sample

To use the feature we defined our own AutoDataFixedName and InlineAutoDataFixedName attributes and used them instead of the default attributes in tests

// Custom attributes with specified test method builder.
// Should be used instead of predefined ones.
public class AutoDataFixedName : AutoDataAttribute
{
    public AutoDataFixedName()
    {
        this.TestMethodBuilder = new FixedNameTestMethodBuilder();
    }
}

public class InlineAutoDataFixedName : InlineAutoDataAttribute
{
    public InlineAutoDataFixedName(params object[] arguments) : base(arguments)
    {
        this.TestMethodBuilder = new FixedNameTestMethodBuilder();
    }
}

public class Tests
{
    [Test, AutoDataFixedName]
    public void FixedNameTest(int a, string b)
    {
    }
  
    [Test]
    [InlineAutoDataFixedName(42)]
    public void FixedNameInlineDataTest(int a, string b)
    {
    }
}

As result, the dynamically generated argument values are now replaced with placeholders. That allows e.g. Visual Studio runner to run those tests and they now have persistent names:

  • FixedNameTest(auto<Int32>,auto<String>)
  • FixedNameInlineDataTest(42,auto<String>)
Clone this wiki locally