AutoFixture
repository·release/5.0.0·Indexed 25 days ago
https://github.com/autofixture/autofixtureA .NET library that automates the creation of test data (anonymous variables) to make unit tests more maintainable and focused on logic rather than dependency setup. It implements a generic version of the Test Data Builder pattern and provides integrations for mocking libraries like Moq, NSubstitute, and FakeItEasy, as well as testing frameworks including xUnit and NUnit.
What's inside AutoFixture
- AutoFixture is a library designed to make Test-Driven Development (TDD) more productive and unit tests more refactoring-safe. It automates the setup of non-relevant test fixtures by generating anonymous variables for virtually any type, removing the need to hand-code data that doesn't influence the specific test case. It implements a generic version of the Test Data Builder pattern.
Access AutoFixture vNext Preview Packages
release/5.0.0Artifacts for the next major version are published to NuGet with a
previewsuffix (e.g.,5.0.0-preview00007). Use these for early access and testing.Warning: Preview versions may contain breaking changes and do not follow SemVer policy. Use them with caution.
Install AutoFixture via .NET CLI or NuGet
release/5.0.0You can install AutoFixture using the .NET CLI or by adding a
PackageReferencedirectly to your project file. AutoFixture is distributed via NuGet.dotnet add package AutoFixture --version 4.18.0<PackageReference Include="AutoFixture" Version="4.18.0" />Use AutoData attributes with NUnit
release/5.0.0When using NUnit, you can use the
[Test, AutoData]attribute combination to automatically inject parameters into your test methods. This reduces the test code to only the essential logic by delegating variable and object creation to AutoFixture via attributes.[Test, AutoData] public void IntroductoryTest(int expectedNumber, MyClass sut) { int result = sut.Echo(expectedNumber); Assert.Equal(expectedNumber, result); }Generate anonymous variables with the Fixture class
release/5.0.0To use AutoFixture in a standard test setup, instantiate the
Fixtureclass and use theCreate<T>()method to generate values or objects of a specific type. This allows you to create both simple values (likeint) and complex System Under Test (SUT) objects without manual initialization.[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); }Use AutoData attributes with xUnit
release/5.0.0When using xUnit, you can use the
[Theory, AutoData]attribute combination to make tests more declarative. This automatically supplies the parameters of your test method using AutoFixture, eliminating the need to manually instantiate aFixtureobject or callCreate<T>()within the test body.[Theory, AutoData] public void IntroductoryTest(int expectedNumber, MyClass sut) { int result = sut.Echo(expectedNumber); Assert.Equal(expectedNumber, result); }Reference AutoFixture Core Packages
release/5.0.0The core packages provide the full set of AutoFixture features without requiring a specific testing framework or third-party integration.
AutoFixture: The main core package.AutoFixture.Idioms: Provides assertion idioms.AutoFixture.SeedExtensions: Provides seed extensions.
Reference AutoFixture Mocking Library Integrations
release/5.0.0These packages enable AutoFixture to work with major .NET mocking libraries, allowing for features like auto-injecting mocks and configuring them automatically.
AutoFixture.AutoMoq: Integration for Moq.AutoFixture.AutoNSubstitute: Integration for NSubstitute.AutoFixture.AutoFakeItEasy: Integration for FakeItEasy.AutoFixture.AutoRhinoMocks: Integration for Rhino Mocks.
Note: To ensure you have the latest features of your mocking library, install the latest version of the mocking library package alongside the corresponding AutoFixture integration package.
Reference AutoFixture Testing Framework Integrations
release/5.0.0These packages enable AutoFixture to integrate with major .NET testing frameworks for auto-generating test cases and combining auto-generated data with inline arguments.
AutoFixture.Xunit3: Integration for xUnit v3.AutoFixture.Xunit2: Integration for xUnit v2.AutoFixture.Xunit: Integration for xUnit v1.AutoFixture.NUnit4: Integration for NUnit v4.AutoFixture.NUnit3: Integration for NUnit v3.AutoFixture.NUnit2: Integration for NUnit v2.AutoFixture.AutoFoq: Integration for Foq.