Skip to content

kimlundjohansen/TUnit.AutoFixture

Repository files navigation

TestSuite.TUnit.AutoFixture

A streamlined, xUnit-compatible AutoFixture integration for TUnit - the modern .NET testing framework.

Build Status NuGet NuGet Downloads GitHub Release License: MIT

Features

  • xUnit-Compatible Naming - Use [AutoData] and [InlineAutoData] just like in xUnit.AutoFixture
  • Seamless Migration - Drop-in replacement for teams migrating from xUnit to TUnit
  • Frozen Parameters - Full support for [Frozen] attribute with 5 matching strategies
  • NSubstitute Integration - Auto-mocking with [AutoNSubstituteData]
  • Immutable Collections - Automatic support for ImmutableArray, ImmutableList, ImmutableDictionary, and more (.NET 5.0+)
  • AutoRegister - Automatic discovery and registration of customizations marked with [AutoRegister]
  • FixtureFactory - Centralized factory for creating configured IFixture instances
  • Custom Generators - Built-in generators for CancellationToken and DateOnly (.NET 6+)
  • Comprehensive Tests - 81 passing tests demonstrating all features

Package

Package Description NuGet
TestSuite.TUnit.AutoFixture Complete library with auto-generating test data and NSubstitute auto-mocking NuGet

All features in one package - No need to install separate packages for NSubstitute support!

Latest Release

Version 1.0.0 - Initial Stable Release (2026-01-21)

What's New in 1.0.0

  • [AutoData] attribute for automatic test data generation
  • [InlineAutoData] for combining explicit and auto-generated parameters
  • [Frozen] attribute with 5 matching strategies (ExactType, ImplementedInterfaces, DirectBaseType, BaseType, MemberOfFamily)
  • NSubstitute integration with [AutoNSubstituteData] and [InlineAutoNSubstituteData]
  • xUnit-compatible naming - seamless migration from xUnit.AutoFixture
  • Zero warnings - strict StyleCop and SonarAnalyzer compliance
  • 48 passing tests - comprehensive test coverage with FluentAssertions
  • Multi-platform CI/CD - automated testing on Ubuntu, Windows, and macOS
  • .NET 10.0 support

Download

# Single package with all features included
dotnet add package TestSuite.TUnit.AutoFixture --version 1.0.1

📦 View on NuGet 📝 Full Release Notes 🏷️ GitHub Release

Latest Release

Version 1.0.0 - Initial Stable Release (2026-01-21)

What's New in 1.0.0

  • [AutoData] attribute for automatic test data generation
  • [InlineAutoData] for combining explicit and auto-generated parameters
  • [Frozen] attribute with 5 matching strategies (ExactType, ImplementedInterfaces, DirectBaseType, BaseType, MemberOfFamily)
  • NSubstitute integration with [AutoNSubstituteData] and [InlineAutoNSubstituteData]
  • xUnit-compatible naming - seamless migration from xUnit.AutoFixture
  • Zero warnings - strict StyleCop and SonarAnalyzer compliance
  • 48 passing tests - comprehensive test coverage with FluentAssertions
  • Multi-platform CI/CD - automated testing on Ubuntu, Windows, and macOS
  • .NET 10.0 support

Quick Start

Installation

# Single package with all features (AutoFixture + NSubstitute)
dotnet add package TestSuite.TUnit.AutoFixture

Basic Usage

using TestSuite.TUnit.AutoFixture;
using TUnit.Core;

public class MyTests
{
    [Test]
    [AutoData]
    public void Test_WithAutoGeneratedData(string text, int number, Person person)
    {
        // All parameters are automatically generated by AutoFixture
        text.Should().NotBeNull();
        number.Should().NotBe(0);
        person.Should().NotBeNull();
    }

    [Test]
    [InlineAutoData("explicit value", 42)]
    public void Test_CombiningExplicitAndAutoData(
        string explicitText,
        int explicitNumber,
        Person autoPerson)
    {
        // First two parameters use explicit values
        // Third parameter is auto-generated
        explicitText.Should().Be("explicit value");
        explicitNumber.Should().Be(42);
        autoPerson.Should().NotBeNull();
    }

    [Test]
    [AutoData]
    public void Test_FrozenParameters(
        [Frozen] IService service,
        Consumer consumer)
    {
        // The same IService instance is injected into Consumer
        consumer.Service.Should().BeSameAs(service);
    }
}

With NSubstitute

using TestSuite.TUnit.AutoFixture;
using TUnit.Core;
using NSubstitute;

public class MyMockTests
{
    [Test]
    [AutoNSubstituteData]
    public void Test_WithAutoMocking(
        [Frozen] IRepository repository,
        Service sut)
    {
        // IRepository is automatically mocked
        repository.GetData().Returns("test data");

        var result = sut.ProcessData();

        result.Should().Be("test data");
        repository.Received(1).GetData();
    }
}

Using FixtureFactory Directly

You can use the FixtureFactory to create configured IFixture instances directly in your code:

using AutoFixture;
using TestSuite.TUnit.AutoFixture;

public class MyTests
{
    [Test]
    public void Test_DirectFixtureUsage()
    {
        // Create a configured fixture instance
        var fixture = FixtureFactory.Create();

        // Generate test data
        var person = fixture.Create<Person>();
        var numbers = fixture.CreateMany<int>(5);

        // All AutoRegister customizations are automatically applied
        person.Should().NotBeNull();
        numbers.Should().HaveCount(5);
    }
}

For NSubstitute-enabled fixtures:

using AutoFixture;
using TestSuite.TUnit.AutoFixture;

public class MyTests
{
    [Test]
    public void Test_DirectNSubstituteFixtureUsage()
    {
        // Create a fixture with NSubstitute auto-mocking
        var fixture = FixtureFactory.CreateWithNSubstitute();

        // Generate mocked interfaces and configured objects
        var service = fixture.Create<IService>();
        var consumer = fixture.Freeze<Consumer>();

        service.GetData().Returns("test");
        consumer.GetServiceData().Should().Be("test");
    }
}

The FixtureFactory provides:

  • Centralized Configuration - All customizations in one place
  • AutoRegister Support - Automatically applies [AutoRegister] marked customizations
  • Immutable Collections - Built-in support for ImmutableArray, ImmutableList, etc. (.NET 5.0+)
  • Consistent Behavior - Same configuration across all test attributes

Documentation

Why TestSuite.TUnit.AutoFixture?

xUnit Compatibility

If you're migrating from xUnit, you can use the exact same attribute names:

xUnit.AutoFixture TestSuite.TUnit.AutoFixture Status
[AutoData] [AutoData] ✅ Identical
[InlineAutoData] [InlineAutoData] ✅ Identical
[Frozen] [Frozen] ✅ Identical
Matching enum Matching enum ✅ Identical

No need to learn new attribute names or refactor your tests!

Simpler Architecture

Aspect Official AutoFixture.TUnit TestSuite.TUnit.AutoFixture
Core Classes 20+ internal classes 6-8 core classes
Primary Attribute AutoDataSourceAttribute AutoDataAttribute
Inline Attribute AutoArgumentsAttribute InlineAutoDataAttribute
Learning Curve Steeper Gentler
Extensibility Multiple extension points Focused, essential points

Feature Parity

All core AutoFixture features are supported:

  • ✅ Auto-generation of primitives, complex types, collections, enums
  • ✅ Frozen parameters with 5 matching strategies (ExactType, ImplementedInterfaces, DirectBaseType, BaseType, MemberOfFamily)
  • ✅ Inline data combining explicit and auto-generated values
  • ✅ NSubstitute auto-mocking integration
  • ✅ Customization attributes
  • ✅ Complex dependency graphs

Migration Guide

From xUnit.AutoFixture

// Before (xUnit)
using AutoFixture.Xunit2;
using Xunit;

public class MyTests
{
    [Theory]
    [AutoData]
    public void Test(string param) { }
}
// After (TUnit)
using TestSuite.TUnit.AutoFixture;
using TUnit.Core;

public class MyTests
{
    [Test]  // Changed from [Theory]
    [AutoData]
    public void Test(string param) { }
}

The only change needed is:

  1. Replace [Theory] with [Test]
  2. Update using statements

All [AutoData], [InlineAutoData], and [Frozen] attributes work identically!

From Official AutoFixture.TUnit

// Before (Official AutoFixture.TUnit)
using AutoFixture.TUnit;

[Test]
[AutoDataSource]  // Different name
public void Test(string param) { }
// After (TestSuite.TUnit.AutoFixture)
using TestSuite.TUnit.AutoFixture;

[Test]
[AutoData]  // xUnit-compatible name
public void Test(string param) { }

Examples

See the test project for comprehensive examples:

Requirements

  • .NET 10.0 or later
  • TUnit 1.10.0 or later
  • AutoFixture 4.18.1 or later

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Building from Source

git clone https://github.com/kimlundjohansen/TUnit.AutoFixture.git
cd TUnit.AutoFixture
dotnet restore
dotnet build
dotnet test

All tests must pass with zero warnings before submitting a PR.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Related Projects


Note: This is an independent implementation focused on xUnit compatibility and simplicity. For the official AutoFixture organization implementation, see AutoFixture.TUnit.