AutoFixture: NUnit Values and AutoData doesn't work

The AutoData overrides the [Values] tag of NUnit

[Test, AutoData]
public void Test(string name, [Values(1, -1)] int num)

num will be set by the AutoData and not from the Values

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Reactions: 3
  • Comments: 21 (19 by maintainers)

Most upvoted comments

I finally was able to find a solution to this issue in a way that satisfies the many requirements.

At the moment I have only a LinqPad script with the needed classes but I plan to make a PR out of it. The only issue is that it required few breaking changes and I’m discussing with @aivascu how to go forward.

I have focused on the AutoDataAttribute for now but I don’t think it will take long to bring the same modifications to InlineAutoDataAttribute.

The new AutoDataAttribute generates as many tests as the combination of the inputs of the method parameters. If a parameter is given no value from a value generator, AutoFixture is used. Also, it’s possible to freeze a value generated from the NUnit attributes.

Here are some examples. For each example, I show a LinqPad Dump of the generated test runs.

[Test]
[NUnitAutoData]
public void TestMethod1([Values(1,2)] int intValue, [Values] bool boolValue, string stringValue, [Range(10,11)] int anotherIntValue) { }

image

[Test]
[NUnitAutoData]
public void TestMethod2([Values(1,2,3), Frozen] int intValue, [Frozen] string stringValue, TestWithDependency dependency) { }

image

[Test]
[NUnitAutoData]
public void TestMethod3([ValueSource(nameof(SourceMethod))] int intValue, [Frozen] string stringValue, TestWithDependency dependency) { }

image

As you can see from the snippets, the Values, Range and ValueSource are used to generate individual test runs. Also, the values they provide are used to name the test so that each test run has a unique name.

What’s left to do:

  • Support InlineAutoData, required
  • Refine support types naming, required
  • Add support for combination strategies other than Combinatorial like Pairwise and Sequential, if needed
  • Support scenarios of ValueSource with methods with parameters fed by AutoFixture, not sure if possible

EDIT:

  • I forgot to attach the file. NUnitAutoDataAttribute.linq.txt (remove the .txt extension and open with LinqPad)
  • I just realized that the Frozen support is flaky at best. I’m working on it.

Following @steph4nc line, I created this gist

The small unit test I created is correctly executed and interacts nicely with the other NUnit attributes (in the example I used Values and Range).

Users should be able to inherit from this attribute and create their own version with a customized Fixture.

Notable to report, the object is created only once and used for all the iterations.

Here are some screenshots from Visual Studio 2017 Enterprise

ReSharper test session image

Live testing image

Console image