AutoFixture is an open source library 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.
Introduction
AutoFixture is designed to make Test-Driven Development more productive and unit tests more refactoring-safe. It does so by removing the need for hand-coding anonymous variables as part of a test's Fixture Setup phase. Among other features, it also offers a generic implementation of the Test Data Builder pattern.
Overview
When writing unit tests, you typically need to create some objects that represent the initial state of the test. Often, an API will force you to specify much more data than you really care about, so you frequently end up creating objects that has no influence on the test, simply to make the code compile.
AutoFixture can help by creating such Anonymous Variables for you. Here's an example:
[Test]
public void Echo_WithAnonymousInteger_ReturnsSameInteger()
{
// Arrange
Fixture fixture = new Fixture();
int expectedNumber = fixture.Create<int>();
MyClass sut = fixture.Create<MyClass>();
// Act
int result = sut.Echo(expectedNumber);
// Assert
Assert.AreEqual(expectedNumber, result, "The method did not return the expected number");
}
This example illustrates the basic principle of AutoFixture:
AutoFixture can create values of virtually any type without the need for you to explicitly define which values should be used.
The number expectedNumber
is created by a call to Fixture.Create<T>
, which will create a regular integer value, saving you the effort of explicitly coming up with one.
The example also illustrates how AutoFixture can be used as a SUT Factory that creates the actual System Under Test.
Resources
The AutoFixture project is hosted on GitHub at github.com/AutoFixture, where you can find more sample code and documentation.