AutoFixture.NUnit4
4.19.0
Prefix Reserved
See the version list below for details.
dotnet add package AutoFixture.NUnit4 --version 4.19.0
NuGet\Install-Package AutoFixture.NUnit4 -Version 4.19.0
<PackageReference Include="AutoFixture.NUnit4" Version="4.19.0" />
<PackageVersion Include="AutoFixture.NUnit4" Version="4.19.0" />
<PackageReference Include="AutoFixture.NUnit4" />
paket add AutoFixture.NUnit4 --version 4.19.0
#r "nuget: AutoFixture.NUnit4, 4.19.0"
#addin nuget:?package=AutoFixture.NUnit4&version=4.19.0
#tool nuget:?package=AutoFixture.NUnit4&version=4.19.0
AutoFixture.NUnit4
AutoFixture.NUnit4 is a .NET library that integrates AutoFixture with NUnit 4.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.
Table of Contents
Installation
AutoFixture packages are distributed via NuGet.<br /> 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.NUnit4 --version x.x.x
<PackageReference Include="AutoFixture.NUnit4" Version="x.x.x" />
Getting Started
Basic Usage
AutoFixture.NUnit4
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.NUnit4;
[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.That(result, Is.EqualTo(a + b));
}
}
Freezing Dependencies
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.NUnit4;
using AutoFixture;
[TestFixture]
public class ConsumerTests
{
[Test, AutoData]
public void Consumer_UsesSameDependency(
[Frozen] Dependency dependency, Consumer consumer)
{
// Assert
Assert.That(dependency, Is.SameAs(consumer.Dependency));
}
}
Integrations
AutoFixture offers a variety of utility packages and integrations with most of the major mocking libraries and testing frameworks.
[!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 (e.g. mocking) library.<br /> Make sure to install the latest version of the integrated library package, alongside the AutoFixture packages.
Core packages
The core packages offer the full set of AutoFixture's features without requring any testing framework or third party integration.
Product | Package | Stable | Preview | Downloads |
---|---|---|---|---|
The core package | AutoFixture | |||
Assertion idioms | AutoFixture.Idioms | |||
Seed extensions | AutoFixture.SeedExtensions |
Mocking libraries
AutoFixture offers integations with most major .NET mocking libraries.<br/> These integrations enable such features as configuring mocks, auto-injecting mocks, etc.
Product | Package | Stable | Preview | Downloads |
---|---|---|---|---|
Moq | AutoFixture.AutoMoq | |||
NSubstitute | AutoFixture.AutoNSubstitute | |||
FakeItEasy | AutoFixture.AutoFakeItEasy | |||
Rhino Mocks | AutoFixture.AutoRhinoMocks |
Testing frameworks
AutoFixture offers integrations with most major .NET testing frameworks.<br /> These integrations enable auto-generation of test cases, combining auto-generated data with inline arguments, etc.
Product | Package | Stable | Preview | Downloads |
---|---|---|---|---|
xUnit v3 | AutoFixture.Xunit3 | |||
xUnit v2 | AutoFixture.Xunit2 | |||
xUnit v1 | AutoFixture.Xunit | |||
NUnit v4 | AutoFixture.NUnit4 | |||
NUnit v3 | AutoFixture.NUnit3 | |||
NUnit v2 | AutoFixture.NUnit2 | |||
Foq | AutoFixture.AutoFoq |
You can check the compatibility with your target framework version on the wiki or on the NuGet website.
Contributing
Contributions are welcome!<br/> If you would like to contribute, please review our contributing guidelines and open an issue or pull request.
License
AutoFixture is Open Source software and is released under the MIT license.<br /> The licenses allows the use of AutoFixture libraries in free and commercial applications and libraries without restrictions.
.NET Foundation
This project is supported by the .NET Foundation.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Framework | net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.NETFramework 4.6.2
- AutoFixture (>= 4.18.1)
- NUnit (>= 4.0.0 && < 5.0.0)
-
net6.0
- AutoFixture (>= 4.18.1)
- NUnit (>= 4.0.0 && < 5.0.0)
- System.Net.Http (>= 4.3.4)
- System.Security.Cryptography.X509Certificates (>= 4.3.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on AutoFixture.NUnit4:
Repository | Stars |
---|---|
recyclarr/recyclarr
Automatically sync TRaSH Guides to your Sonarr and Radarr instances
|
Version | Downloads | Last updated |
---|---|---|
5.0.0-preview0012 | 29,860 | 1/21/2025 |
5.0.0-preview0011 | 209,286 | 4/15/2024 |
4.19.0 | 567 | 4/15/2025 |