AutoFixture.NUnit3 is a .NET library that integrates AutoFixture with NUnit 3.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 NUnit 3 package is deprecated.
This package is intended for legacy projects that are still using NUnit 3.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.NUnit3 --version x.x.x<PackageReference Include="AutoFixture.NUnit3" Version="x.x.x" />AutoFixture.NUnit3 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 NUnit.Framework;
using AutoFixture.NUnit3;
[TestFixture]
public class CalculatorTests
{
[Test, 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);
}
}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 NUnit.Framework;
using AutoFixture.NUnit3;
using AutoFixture;
[TestFixture]
public class ConsumerTests
{
[Test, AutoData]
public void Consumer_UsesSameDependency(
[Frozen] Dependency dependency, Consumer consumer)
{
// Assert
Assert.AreSame(dependency, consumer.Dependency);
}
}Contributions to AutoFixture.NUnit3 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.