I have been following Mark Seemann for a while ‘cause this guy is as smart as it gets. He wrote a book on dependency injection, released two awesome courses on unit testing in Pluralsight, and has written the AutoFixture unit testing library (with many other contributors).
AutoFixture is a .NET library that helps you write unit tests with less code by providing:
- Test data generators for primitive and complex types (in a constrained non-deterministic way – the data generation algorithm can be customized)
- Test data builders for any type
- AutoMocking of dependencies
- I repeat, A U T O M O C K I N G of dependencies
and in words of Mark Seemann:
AutoFixture is an open source framework for .NET designed to minimize the ‘Arrange’ phase of your unit tests. Its primary goal is to allow developers to focus on what is being tested rather than how to setup the test scenario, by making it easier to create object graphs containing test data.
What does this means in terms of that application you are developing right now if you were to adopt AutoFixture just this very moment?
- Write less unit test code
- Bye writing mother objects
- Bye writing your own test data builders
- Bye to manually providing dependencies for your system under test
- But wait Joe! There’s more!
- By using a SUT (system under test) factory we effectively isolate all tests from changes in the SUT constructor. You will probably have handled this via factory methods, builders or fixture objects. But with AutoFixture and automocking, adding a new dependency to a constructor will not break any test!
- Being able to automock dependencies means that you don’t need to explicitly mock/stub dependencies that are not used in a given test, which in turns means that tests become more readable, intentional and deliberate. The code that it is there takes active part in the test. All crud is removed, gone, caput.
You can easily start using AutoFixture in your project via Nuaet:
1 2 3
Note: I am using Nunit and RhinoMocks in this article but there is support for many other testing frameworks and mocking libraries.
Where each of these packages consist in the following:
- AutoFixture: this is AutoFixture itself
- AutoFixture.NUnit2: this is an improved integration between AutoFixture and NUnit that allows AutoFixture to inject test data and SUT directly in NUnit Test methods
- AutoFixture.AutoRhinoMock: this allows auto mocking of dependencies via Rhino.Mocks
I am going to go fast like lightning through some of the interesting features provided by AutoFixture, brace yourself:
Auto Mocking SUT Dependencies
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
Test Builders For Any Type
1 2 3 4 5 6
Generating Test Data
1 2 3 4
Note For The Intrepid: AutoFixture doesn’t work well if there are cycles in the object graph. If a complex type has cycles, you will need to tell AutoFixture to leave that property alone (
OmitAutoProperties) or set it explicitely.
Integration with NUnit
If we do a full integration with NUnit we can remove the Arrange stage and inject the test data directly as arguments:
1 2 3 4 5 6 7 8
And even auto mock SUT dependencies:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Mind Officially Blown Away. Booom… Boooom…. Booom… Boooom…. Booooom….
It even has testimonials. Open-source library with testimonials?!? Whatz?!?:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
It is time to Experiment Thyselve! Go
NuGet Install-Package AutoFixture Now!