AutoFixture.Xunit 4.19.0

Prefix Reserved
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package AutoFixture.Xunit --version 4.19.0
                    
NuGet\Install-Package AutoFixture.Xunit -Version 4.19.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AutoFixture.Xunit" Version="4.19.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AutoFixture.Xunit" Version="4.19.0" />
                    
Directory.Packages.props
<PackageReference Include="AutoFixture.Xunit" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AutoFixture.Xunit --version 4.19.0
                    
#r "nuget: AutoFixture.Xunit, 4.19.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=AutoFixture.Xunit&version=4.19.0
                    
Install AutoFixture.Xunit as a Cake Addin
#tool nuget:?package=AutoFixture.Xunit&version=4.19.0
                    
Install AutoFixture.Xunit as a Cake Tool

AutoFixture.xUnit

License NuGet version NuGet preview version NuGet downloads

AutoFixture.xUnit is a .NET library that integrates AutoFixture with xUnit 1.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.

While this package is still being developed, the xUnit 1 package is deprecated.<br/> This package is intended only for legacy projects that are still using xUnit 1.x.<br/>

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.xUnit --version x.x.x
<PackageReference Include="AutoFixture.xUnit" Version="x.x.x" />

Getting Started

Basic Usage

AutoFixture.xUnit 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.xUnit;

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);
    }
}

Inline Auto-Data

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.xUnit;
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);
    }
}

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 Xunit;
using AutoFixture.xUnit;
using AutoFixture;

public class ConsumerTests
{
    [Theory, AutoData]
    public void Consumer_UsesSameDependency(
        [Frozen] Dependency dependency, Consumer consumer)
    {
        // Assert
        Assert.AreSame(dependency, consumer.Dependency);
    }
}

Integrations

AutoFixture offers a variety of utility packages and integrations with most of the major mocking libraries and testing frameworks.

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 NuGet NuGet NuGet
Assertion idioms AutoFixture.Idioms NuGet NuGet NuGet
Seed extensions AutoFixture.SeedExtensions NuGet NuGet NuGet

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 NuGet NuGet NuGet
NSubstitute AutoFixture.AutoNSubstitute NuGet NuGet NuGet
FakeItEasy AutoFixture.AutoFakeItEasy NuGet NuGet NuGet
Rhino Mocks AutoFixture.AutoRhinoMocks NuGet NuGet NuGet

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 NuGet NuGet NuGet
xUnit v2 AutoFixture.Xunit2 NuGet NuGet NuGet
xUnit v1 AutoFixture.Xunit NuGet NuGet NuGet
NUnit v4 AutoFixture.NUnit4 NuGet NuGet NuGet
NUnit v3 AutoFixture.NUnit3 NuGet NuGet NuGet
NUnit v2 AutoFixture.NUnit2 NuGet NuGet NuGet
Foq AutoFixture.AutoFoq NuGet NuGet NuGet

You can check the compatibility with your target framework version on the wiki or on the NuGet website.

Contributing

Contributions to AutoFixture.xUnit are welcome! 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 Compatible and additional computed target framework versions.
.NET Framework net452 is compatible.  net46 was computed.  net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on AutoFixture.Xunit:

Package Downloads
Trove.Bundle.Xunit.Moq

Bundles together various packages to use xUnit and Moq.

F2F.Testing.Xunit.FakeItEasy

Brings an AutoMockFeature which initializes AutoFixture with FakeItEasy.

Treevs.Essentials.AutoFixture.Xunit

Use Xunit Theories + AutoFixture.Xunit, in a neat and concise manner.

Selkie.XUnit.Extensions

This small package contains XUnit extensions: AutoNSubstituteDataAttribute and InlineAutoNSubstituteDataAttribute.

GitHub repositories (3)

Showing the top 3 popular GitHub repositories that depend on AutoFixture.Xunit:

Repository Stars
Glimpse/Glimpse
The open source diagnostics platform for the web
ploeh/Hyprlinkr
A URI building helper library for ASP.NET Web API
GreanTech/AtomEventStore
A server-less .NET Event Store based on the Atom syndication format
Version Downloads Last updated
5.0.0-preview0012 567 1/21/2025
5.0.0-preview0011 252 4/15/2024
5.0.0-preview0010 129 3/25/2024
5.0.0-preview0009 127 3/10/2024
5.0.0-preview0008 158 2/18/2024
5.0.0-preview0007 7,566 12/9/2023
5.0.0-preview0006 139 12/4/2023
5.0.0-pdb-test0003 133 12/10/2023
5.0.0-pdb-test0002 111 12/10/2023
5.0.0-pdb-test0001 113 12/10/2023
5.0.0-dataattributes0001 142 12/27/2023
4.19.0 1,185 4/14/2025
4.18.1 44,036 11/28/2023
4.18.0 34,750 2/21/2023
4.17.0 90,561 4/20/2021
4.16.0 3,789 4/1/2021
4.15.0 27,241 12/18/2020
4.14.0 44,776 9/10/2020
4.13.0 10,268 7/9/2020
4.12.0 1,175 6/28/2020
4.11.0 72,817 7/8/2019
4.10.0 4,492 6/13/2019
4.9.0 2,833 5/28/2019
4.8.0 4,650 1/30/2019
4.7.0 1,339 1/26/2019
4.6.0 1,677 12/10/2018
4.5.1 1,609 11/25/2018
4.5.0 12,051 7/12/2018
4.4.0 2,362 5/26/2018
4.3.0 1,859 5/2/2018
4.2.1 2,101 4/16/2018
4.2.0 5,405 3/21/2018
4.1.0 2,354 2/15/2018
4.0.1 2,115 1/22/2018
4.0.0 7,853 1/10/2018
4.0.0-rc1 1,109 10/18/2017
3.51.0 52,213 10/5/2017
3.50.7 12,564 9/18/2017
3.50.6 6,647 8/16/2017
3.50.5 23,375 8/9/2017
3.50.4 1,613 8/7/2017
3.50.3 17,557 7/5/2017
3.50.2 68,834 8/31/2016
3.50.1 6,696 8/12/2016
3.50.0 4,172 8/11/2016
3.49.1 18,203 8/3/2016
3.49.0 2,975 7/5/2016
3.48.0 2,067 6/28/2016
3.47.8 5,563 6/19/2016
3.47.7 1,891 6/18/2016
3.47.6 5,231 6/13/2016
3.47.5 1,910 6/11/2016
3.47.4 9,653 6/3/2016
3.47.3 25,424 6/2/2016
3.47.2 1,742 6/2/2016
3.47.1 1,739 6/1/2016
3.47.0 1,735 5/31/2016
3.46.0 30,419 5/15/2016
3.45.3 1,732 5/12/2016
3.45.2 2,509 5/7/2016
3.45.1 2,323 4/25/2016
3.45.0 3,489 4/24/2016
3.44.1 3,476 4/20/2016
3.44.0 2,139 4/15/2016
3.43.4 1,829 4/14/2016
3.43.3 1,720 4/13/2016
3.43.2 1,823 4/12/2016
3.43.1 1,729 4/10/2016
3.43.0 3,612 3/28/2016
3.42.0 2,013 3/20/2016
3.41.1 6,134 3/13/2016
3.41.0 1,880 3/12/2016
3.40.0 7,587 2/2/2016
3.39.0 2,212 1/22/2016
3.38.1 2,020 1/3/2016
3.38.0 1,744 12/31/2015
3.37.3 2,101 12/8/2015
3.37.2 1,667 12/8/2015
3.37.1 1,704 12/3/2015
3.37.0 1,998 11/25/2015
3.36.12 3,526 11/20/2015
3.36.11 2,255 11/16/2015
3.36.9 30,938 10/21/2015
3.36.8 2,652 10/8/2015
3.36.1 1,931 10/6/2015
3.36.0 2,930 10/2/2015
3.35.1 1,958 10/2/2015
3.34.2 2,985 9/16/2015
3.34.1 1,892 9/16/2015
3.34.0 2,006 9/12/2015
3.33.0 1,972 9/5/2015
3.32.2 1,749 9/5/2015
3.32.0 1,798 9/5/2015
3.31.0 4,397 7/31/2015
3.30.8 5,478 6/17/2015
3.30.7 1,987 6/14/2015
3.30.6 2,268 6/5/2015
3.30.4 4,281 4/29/2015
3.30.3 2,761 4/19/2015
3.30.2 3,107 4/11/2015
3.30.1 7,311 4/10/2015
3.30.0 36,482 4/9/2015
3.29.0 1,855 4/9/2015
3.27.0 1,850 4/8/2015
3.25.0 1,819 4/8/2015
3.24.6 1,980 4/7/2015
3.24.5 1,821 4/6/2015
3.24.4 1,785 4/4/2015
3.24.3 5,694 3/12/2015
3.24.2 5,371 3/7/2015
3.24.1 6,412 2/28/2015
3.24.0 3,631 2/19/2015
3.23.2 2,239 2/18/2015
3.23.1 5,287 2/4/2015
3.23.0 5,127 1/27/2015
3.22.0 5,695 12/21/2014
3.21.1 7,212 11/13/2014
3.21.0 3,797 10/20/2014
3.20.4 2,320 10/15/2014
3.20.2 3,932 9/16/2014
3.20.1 2,632 9/7/2014
3.20.0 7,561 8/20/2014
3.19.2 4,353 8/5/2014
3.19.1 6,503 6/19/2014
3.19.0 2,605 6/12/2014
3.18.10 2,937 6/5/2014
3.18.9 2,781 6/1/2014
3.18.8 15,892 5/24/2014
3.18.7 22,753 5/17/2014
3.18.6 2,519 5/6/2014
3.18.5 4,936 4/25/2014
3.18.4 2,200 4/24/2014
3.18.3 3,170 4/13/2014
3.18.2 2,218 4/12/2014
3.18.1 18,462 3/29/2014
3.18.0 2,851 3/23/2014
3.17.0 2,567 3/15/2014
3.16.10 2,417 3/10/2014
3.16.9 2,035 3/9/2014
3.16.8 1,894 3/9/2014
3.16.7 1,870 3/8/2014
3.16.6 2,942 3/2/2014
3.16.5 3,632 1/31/2014
3.16.4 2,923 1/2/2014
3.16.3 4,325 12/22/2013
3.16.2 1,924 12/21/2013
3.16.1 2,823 12/5/2013
3.16.0 2,474 11/30/2013
3.15.1 2,694 11/26/2013
3.15.0 2,007 11/24/2013
3.14.0 1,943 11/23/2013
3.13.0 4,169 11/6/2013
3.12.1 2,903 10/20/2013
3.12.0 2,100 10/15/2013
3.11.0 2,249 10/13/2013
3.10.1 4,657 10/9/2013
3.10.0 2,205 10/8/2013
3.9.1 2,219 10/2/2013
3.9.0 2,306 9/24/2013
3.8.1 1,950 9/23/2013
3.8.0 2,098 9/22/2013
3.7.0 2,224 9/19/2013
3.6.8 2,822 9/6/2013
3.6.7 2,211 8/31/2013
3.6.6 3,610 8/1/2013
3.6.5 3,358 7/22/2013
3.6.3 2,673 7/14/2013
3.6.2 2,515 7/13/2013
3.6.1 2,450 7/11/2013
3.6.0 16,671 7/9/2013
3.5.1 2,545 7/2/2013
3.5.0 2,230 7/1/2013
3.4.1 2,391 6/27/2013
3.4.0 2,193 6/24/2013
3.3.0 2,369 6/23/2013
3.2.1 6,595 6/3/2013
3.2.0 2,621 6/1/2013
3.1.0 2,200 5/26/2013
3.0.9 2,252 5/15/2013
3.0.8 3,161 4/9/2013
3.0.7 2,230 4/6/2013
3.0.6 2,266 4/4/2013
3.0.5 2,043 4/2/2013
3.0.3 2,370 3/16/2013
3.0.2 3,137 3/5/2013
3.0.1 23,024 3/1/2013
2.16.2 4,035 1/27/2013
2.16.1 2,249 1/10/2013
2.16.0 2,149 1/6/2013
2.15.6 2,004 1/4/2013
2.15.5 2,551 12/7/2012
2.15.4 2,565 11/30/2012
2.15.3 2,235 11/29/2012
2.15.2 2,447 11/22/2012
2.15.1 2,198 11/21/2012
2.15.0 2,280 11/21/2012
2.14.1 2,324 11/19/2012
2.14.0 2,268 11/14/2012
2.13.5 2,278 11/9/2012
2.13.4 2,419 10/31/2012
2.13.3 8,549 10/9/2012
2.12.0 3,068 7/28/2012
2.11.4 2,186 7/4/2012
2.11.3 2,028 7/3/2012
2.11.2 2,059 6/24/2012
2.11.1 3,669 5/26/2012
2.11.0 2,078 5/6/2012
2.10.0 18,127 4/10/2012
2.9.2 2,250 3/7/2012
2.9.1 2,420 2/20/2012
2.9.0 2,083 2/19/2012
2.8.0 2,261 1/29/2012
2.7.2 2,208 12/28/2011
2.7.1 2,196 12/16/2011
2.7.0 2,217 12/12/2011
2.6.0 2,144 12/8/2011
2.5.2 2,088 12/8/2011
2.5.1 2,160 12/2/2011
2.5.0 2,182 12/1/2011
2.4.1 2,742 10/1/2011
2.4.0 2,218 9/18/2011
2.3.2 2,209 9/12/2011
2.3.1 2,249 9/11/2011
2.2.45 2,214 9/9/2011
2.2.44 3,624 9/6/2011
2.1.0 2,562 5/5/2011
2.0.0 2,707 4/16/2011