AutoFixture.xUnit2 is a .NET library that integrates AutoFixture with xUnit 2.x, allowing you to effortlessly generate test data for your unit tests. By automatically populating your test parameters, it helps you write cleaner, more maintainable tests without having to manually construct test objects.
Warning
While this package is still being developed, the xUnit 2 package is deprecated.
This package is intended for legacy projects that are still using xUnit 2.x.
Use at your own risk.
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.xUnit2 --version x.x.x<PackageReference Include="AutoFixture.xUnit2" Version="x.x.x" />AutoFixture.xUnit2 provides an [AutoData] attribute that automatically populates test method parameters with generated data.
For example, imagine you have a simple calculator class:
public class Calculator
{
public int Add(int a, int b) => a + b;
}You can write a test using AutoFixture to provide the input values:
using Xunit;
using AutoFixture.xUnit2;
public class CalculatorTests
{
[Theory, AutoData]
public void Add_SimpleValues_ReturnsCorrectResult(
Calculator calculator, int a, int b)
{
// Act
int result = calculator.Add(a, b);
// Assert
Assert.AreEqual(a + b, result);
}
}You can also combine auto-generated data with inline arguments using the [InlineAutoData] attribute.
This allows you to specify some parameters while still letting AutoFixture generate the rest.
using Xunit;
using AutoFixture.xUnit2;
using AutoFixture;
public class CalculatorTests
{
[Theory, InlineAutoData(5, 8)]
public void Add_SpecificValues_ReturnsCorrectResult(
int a, int b, Calculator calculator)
{
// Act
int result = calculator.Add(a, b);
// Assert
Assert.AreEqual(13, result);
}
}AutoFixture's [Frozen] attribute can be used to ensure that the same instance of a dependency is injected into multiple parameters.
For example, if you have a consumer class that depends on a shared dependency:
public class Dependency { }
public class Consumer
{
public Dependency Dependency { get; }
public Consumer(Dependency dependency)
{
Dependency = dependency;
}
}You can freeze the Dependency so that all requests for it within the test will return the same instance:
using Xunit;
using AutoFixture.xUnit2;
using AutoFixture;
public class ConsumerTests
{
[Theory, AutoData]
public void Consumer_UsesSameDependency(
[Frozen] Dependency dependency, Consumer consumer)
{
// Assert
Assert.AreSame(dependency, consumer.Dependency);
}
}Contributions are welcome!
If you would like to contribute, please review our contributing guidelines and open an issue or pull request.
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.