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.
Check the testimonials to see what other people have to say about AutoFixture.
(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:
[Fact]
public void IntroductoryTest()
{
// Arrange
Fixture fixture = new Fixture();
int expectedNumber = fixture.Create<int>();
MyClass sut = fixture.Create<MyClass>();
// Act
int result = sut.Echo(expectedNumber);
// Assert
Assert.Equal(expectedNumber, result);
}
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 Create<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);
}
[Test, 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 packages are distributed via NuGet.
To install the packages you can use the integrated package manager of your IDE, the .NET CLI, or reference the package directly in your project file.
dotnet add package AutoFixture --version 4.18.0
<PackageReference Include="AutoFixture" Version="4.18.0" />
AutoFixture offers a variety of utility packages and integrations with most of the major mocking libraries and testing frameworks.
The core packages offer the full set of AutoFixture's features without requring any testing framework or third party integration.
Product | Package | Latest stable | Latest preview |
---|---|---|---|
The core package | AutoFixture | ||
Assertion idioms | AutoFixture.Idioms | ||
Seed extensions | AutoFixture.SeedExtensions |
AutoFixture offers integations with most major .NET mocking libraries.
These integrations enable such features as configuring mocks, auto-injecting mocks, etc.
Product | Package | Latest stable | Latest preview |
---|---|---|---|
Moq | AutoFixture.AutoMoq | ||
NSubstitute | AutoFixture.AutoNSubstitute | ||
FakeItEasy | AutoFixture.AutoFakeItEasy | ||
Rhino Mocks | AutoFixture.AutoRhinoMocks |
NOTE: Since AutoFixture tries maintain compatibility with a large number of package versions, the packages bundled with AutoFixture might not contain the latest features of your mocking library.
Make sure to install the latest version of the mocking library package, alongside the AutoFixture package.
AutoFixture offers integrations with most major .NET testing frameworks.
These integrations enable auto-generation of test cases, combining auto-generated data with inline arguments, etc.
Product | Package | Latest stable | Latest preview |
---|---|---|---|
Foq | AutoFixture.AutoFoq | ||
xUnit v1 | AutoFixture.Xunit | ||
xUnit v2 | AutoFixture.Xunit2 | ||
NUnit v2 | AutoFixture.NUnit2 | ||
NUnit v3 | AutoFixture.NUnit3 |
You can check the compatibility with your target framework version on the wiki or on the NuGet website.
The artifacts of the next major version are published to the MyGet feed:
https://www.myget.org/F/autofixture/api/v3/index.json
(Visual Studio 2015+)https://www.myget.org/F/autofixture/api/v2
(Visual Studio 2012+)
You can use this feed to early access and test the next major version of the AutoFixture.
Notice, this feed exists for the preview purpose only, so use it with caution:
- new versions of packages might contain breaking changes and API could change drastically from package to package. By other words, we don't follow the SemVer policy for the packages in this feed;
- packages might be cleaned up over time (MyGet has storage limits), so don't consider this feed for the permanent usage (or at least ensure to make a copy of the used packages somewhere else).
- Pluralsight course
- ploeh blog
- Nikos Baxevanis' blog
- Enrico Campidoglio's blog
- Gert Jansen van Rensburg's blog
- Questions on Stack Overflow
If you have questions, feel free to ask. The best places to ask are:
AutoFixture is Open Source software and is released under the MIT license.
The licenses allows the use of AutoFixture libraries in free and commercial applications and libraries without restrictions.
This project is supported by the .NET Foundation.