For a more general introduction to AutoFixture, see my previous post.

For a long time it has been an xUnit exclusive to utilize AutoData in your test suites. The extensibility model of NUnit, made the migration to NUnit cumbersome, to say the least. Recently, Gert Jansen van Rensburg, made the first serious attempt to solve this. Having followed his battles on GitHub, he released the first version of the AutoFixture.NUnit2 NuGet package.

Although installation and setup is not at smooth as with xUnit, the power of this extension is without question. This post provides a short setup guide on how to install and configure this to work on both local dev machines as well as on TeamCity. The challenge with the extensibility model of NUnit is the distribution of the Addin, the actual plugin or extension to NUnit. This .dll has to be present in the bin\addins folder of NUnit. This is not preferable in a larger multi dev environment, therefore an alternative approach is proposed.


  1. Go to your NuGet Package Manager, and Install-Package AutoFixture.NUnit2
    • If you have any previous version of AutoFixture installed it will be updated to the latest.
  2. And Install-Package NUnit.Runners
  3. Make a manual reference to tools\lib\nunit.core.interfaces.dll, which resides in your packages folder under NUnit.Runners.
  4. The installation of the AutoFixture.NUnit2 package has inserted this line in your AssemblyInfo.cs-file: [assembly: NUnit.Framework.RequiredAddinPloeh.AutoFixture.NUnit2.Addins.Constants.AutoDataExtension)]
    • Remove this for now.

Now everything is installed and ready to go, except that we need to add the NUnit Addin to the test assembly. I know this is cumbersome, and a bitter pill to swallow. As mentioned above, copying the Ploeh.AutoFixture.NUnit2.Addins.dll-file from the packages folder to your unit test runners bin\addins folder:

  • NUnit: C:\Program Files (x86)\NUnit 2.6.2\bin\addins
  • R#: C:\Program Files (x86)\JetBrains\ReSharper\v8.0\Bin\addins

As proposed here, I could also create a new class, which would inherit the Addin, and add this to the test assembly, but in my test setup I found a better spot.


Usually I setup my test fixtures using a base class, which among other things holds my AutoFixture configurations, a common setup could look like this:

Copy to Clipboard

Now this also makes it a perfect spot for my NUnit Addin class, which I’ll include by inheritance:

Copy to Clipboard

So adding the NUnitAddin attribute and inheriting the Ploeh.AutoFixture.NUnit2.Addins.Addin class, I don’t have to copy the addin .dll to the approriate bin\addins folder. Please note the using statement. This way it is by far easier to setup and distribute in the large developer environment. Since also works with TeamCity, otherwise you would have to setup your NUnit Test Launcher on TeamCity to locate the addin, not advisable and a poorly documented approach.


With all these efforts behind us, we can now easily auto-generate test data via parameters, such as this:

Copy to Clipboard

In this test case foo and baa will be assigned auto-generate values by AutoFixture. Now there is much more in the box. So far, the NUnit TestCase attribute have had a rather sparse set of allowed types, such as ints, strings, bool, etc. With AutoData it is now possible auto-generate types as decimal, and even complex types:

Copy to Clipboard

This is all great and awesome, but wait there is some minors that also might come in handy.

You might have some test scenarios where you would still like to supply your own test data. This will work great along AutoData:

Copy to Clipboard

This will generate two test cases, the first one with auto-generated test data from AutoFixture, and the second one with the test data from the TestCase attribute. Note, that the types must be allowed by the TestCase attribute.

Lets consider a more advanced scenario. I have previously described how to use AutoFixture as an auto-mocking container. This is how we would auto-generate and freeze our dependencies using AutoData:

Copy to Clipboard

In this test case we let AutoFixture freeze the dependencies that I would like to setup expectations for, and AutoFixture will the inject them during the creation of the MyClass sut.

These are some of the great new possibilities of the latest extension to AutoFixture, you can probably come up with other awesome scenarios.

Finally, I must send all credits for the extension to Gert Jansen van Rensburg. Read his blog and follow him on Twitter.

Recent Posts