Search

Project Description

Write maintainable unit tests, faster.

AutoFixture makes it easier for developers to do Test-Driven Development by automating non-relevant Test Fixture Setup, allowing the Test Developer to focus on the essentials of each test case.

Overview

(Jump straight to the CheatSheet if you just want to see some code samples right away.)

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 offers a generic implementation of the Test Data Builder pattern.

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 a simple example:

[TestMethod]
public void IntroductoryTest()
{
    // Fixture setup
    Fixture fixture = new Fixture();

    int expectedNumber = fixture.CreateAnonymous<int>();
    MyClass sut = fixture.CreateAnonymous<MyClass>();
    // Exercise system
    int result = sut.Echo(expectedNumber);
    // Verify outcome
    Assert.AreEqual<int>(expectedNumber, result, "Echo");
    // Teardown
}

This example illustrates the basic principle of AutoFixture: It 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 CreateAnonymous<T> - this will create a 'nice', 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 (the MyClass instance).

Given the right combination of unit testing framework and extensions for AutoFixture, we can further reduce the above test to be even more declarative:

[Theory, AutoData]
public void IntroductoryTest(
    int expectedNumber, MyClass sut)
{
    int result = sut.Echo(expectedNumber);
    Assert.Equal(expectedNumber, result);
}

Notice how we can reduce unit tests to state only the relevant parts of the test. The rest (variables, Fixture object) is relegated to attributes and parameter values that are supplied automatically by AutoFixture. The test is now only two lines of code.

Using AutoFixture is as easy as referencing the library and creating a new instance of the Fixture class!

AutoFixture is available via NuGet (as AutoFixture, AutoFixture.AutoMoq, AutoFixture.AutoRhinoMocks, AutoFixture.AutoFakeItEasy and AutoFixture.Xunit).

Resources

CheatSheet
FAQ
Read more on ploeh blog.
 ploeh blog - AutoFixture News Feed 
Tuesday, January 03, 2012  |  From ploeh blog - AutoFixture
Tuesday, October 25, 2011  |  From ploeh blog - AutoFixture
Tuesday, September 06, 2011  |  From ploeh blog - AutoFixture
 ploeh blog - AutoFixture News Feed 

Nikos Baxevanis also provides good posts about AutoFixture:
 Bonus Bits News Feed 
Wednesday, December 14, 2011  |  From Bonus Bits
Sunday, December 11, 2011  |  From Bonus Bits
Sunday, September 18, 2011  |  From Bonus Bits
 Bonus Bits News Feed 

Enrico Campidoglio also writes great AutoFixture blog posts:
 Thoughtology » AutoFixture News Feed 
Thursday, December 15, 2011  |  From Thoughtology » AutoFixture
Tuesday, September 06, 2011  |  From Thoughtology » AutoFixture
Monday, August 01, 2011  |  From Thoughtology » AutoFixture
 Thoughtology » AutoFixture News Feed 
Last edited Jan 11 at 1:15 PM by baxevanis, version 26
Updating...
© 2006-2012 Microsoft | Get Help | Privacy Statement | Terms of Use | Code of Conduct | Advertise With Us | Version 2012.1.11.18365