SuperAPI AI Directory

dotnet-testing-autofixture-bogus-integration

Testing & quality

View repository

Updated 10-16 06:22

About this skill

AutoFixture and Bogus Integration Application Guide

Core Concepts

Why Is Integration Needed?

Aspect AutoFixture Bogus Integration Solution
Data Realism Low (GUID-format strings) High (realistic Email/Phone) High
Object Relationship Handling Automatic Manual Automatic
Circular Reference Handling Built-in None Integrated
Configuration Complexity Low Medium Medium
Applicable Scenarios Unit Testing Integration Testing/Prototyping Both

Integration Effect: user.Email From "Email1a2b3c4d" to "john.doe@example.com", while other properties are still automatically populated by AutoFixture.

Package Installation

<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="AutoFixture.Xunit2" Version="4.18.1" />
<PackageReference Include="Bogus" Version="35.6.5" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="AwesomeAssertions" Version="9.4.0" />

Integration Methods Overview

Integration Method Applicable Scenarios Complexity
Property-Level SpecimenBuilder Use Bogus for specific properties Low
Type-Level SpecimenBuilder Use Bogus for an entire type Medium
Hybrid Generator (HybridGenerator) Unified API integration Medium
Integrated Factory (IntegratedFactory) Complete test scenario construction High
Custom AutoData Attribute xUnit integration Low

Core Integration Techniques

ISpecimenBuilder Integration

Through the ISpecimenBuilder interface, implement property-level and type-level integration:

  • Property Level: Determine based on the property name (such as Email、Phone、FirstName), and use Bogus to generate the corresponding realistic data
  • Type Level: Register a Bogus Faker generator for an entire type (such as User、Address)

Commonly Used SpecimenBuilder

SpecimenBuilder Generated Data Type Bogus API
EmailSpecimenBuilder Email Address f.Internet.Email()
PhoneSpecimenBuilder Phone Number f.Phone.PhoneNumber()
NameSpecimenBuilder Person Name f.Name.FirstName()
AddressSpecimenBuilder Address f.Address.FullAddress()

Extension Methods

  • fixture.WithBogus() — Register all Bogus SpecimenBuilder
  • fixture.WithOmitOnRecursion() — Handle circular references
  • fixture.WithSeed(seed) — Set random seed
  • fixture.WithRepeatCount(count) — Set the default count for CreateMany

For complete content, please refer to references/core-integration-techniques.md

Circular Reference Handling

When an object has circular references (such as User → Company → Employees(User)), use OmitOnRecursionBehavior to resolve:

var fixture = new Fixture();
fixture.Behaviors.OfType<ThrowingRecursionBehavior>()
    .ToList()
    .ForEach(b => fixture.Behaviors.Remove(b));
fixture.Behaviors.Add(new OmitOnRecursionBehavior());

Effect: Avoid StackOverflowException; properties with circular references are set to null or an empty collection.

Custom AutoData Attribute

public class BogusAutoDataAttribute : AutoDataAttribute
{
    public BogusAutoDataAttribute()
        : base(() => new Fixture().WithBogus())
    {
    }
}

// 使用方式
[Theory]
[BogusAutoData]
public void 使用整合資料測試(User user, Address address)
{
    user.Email.Should().Contain("@");
    address.City.Should().NotBeNullOrEmpty();
}

Hybrid Generator and Test Data Factory

HybridTestDataGenerator

A unified test data generation API, implementing ITestDataGenerator Interface:

  • Generate<T>() — Generate a single object
  • Generate<T>(int count) — Generate a collection of the specified quantity
  • Generate<T>(Action<T> configure) — Perform custom configuration after generating objects

IntegratedTestDataFactory

Complete scenario construction factory, supporting advanced features:

  • CreateFresh<T>() — Generate a brand-new object each time
  • CreateMany<T>(count) — Batch creation
  • GetCached<T>() — Caching mechanism, the same type is generated only once
  • CreateTestScenario() — Create a complete test scenario containing Company, Users, and Orders, with associations automatically established

TestBase base class

Unify the initialization and Seed management of Fixture, Generator, and Factory, providing Create<T>()、CreateMany<T>()、Create<T>(configure) and other convenience methods.

For complete code examples, please refer to references/hybrid-generator-and-factory.md

Seed Management and Reproducibility

Important Limitations

Because AutoFixture and Bogus have different random number management mechanisms:

  • Seed ensures stability of test behavior
  • Seed ensures consistency of data format
  • Cannot guarantee that all property values are exactly the same

Recommended Practices

  • General scenarios: use IntegratedTestDataFactory(seed: 12345) to ensure stability
  • Fully reproducible: use a single tool, such as Faker<User>().UseSeed(12345)
  • No reproducibility needed: do not set a Seed, generate different random data each time

Frequently Asked Questions

Q1: When should the integrated solution be used, and when should a single tool be used?

  • Use the integrated solution: test scenarios that require realistic data (Email, Phone) and have complex object structures
  • Use pure AutoFixture: unit tests that only need anonymous data population
  • Use pure Bogus: integration tests that need to be fully reproducible and have strict data formats

Q2: What is the SpecimenBuilder matching priority order?

AutoFixture matches according to the order of the Customizations collection; Builders added earlier take priority. Using fixture.Customizations.Insert(0, builder) ensures the highest priority.

Q3: How do I add Bogus support for a new entity type?

  1. Create the corresponding ISpecimenBuilder implementation
  2. In the Create() method, determine the property name or type
  3. Use Bogus Faker to generate the corresponding realistic data
  4. In the WithBogus() extension method, register it

Best Practices

Recommended Practices

  1. Always handle circular references first — fixture.WithOmitOnRecursion().WithBogus()
  2. Create dedicated SpecimenBuilders for commonly used entities
  3. Use Seed to ensure test stability
  4. Create a test base class to unify data generation logic
  5. Use caching appropriately to improve performance

Things to Avoid

  1. Over-engineering; keep it simple and practical
  2. Expecting the integrated environment to be fully reproducible
  3. Ignoring circular reference handling
  4. Recreating the Fixture in every test

Output Format

  • Generate ISpecimenBuilder implementation class files (such as EmailSpecimenBuilder.cs)
  • Generate Fixture extension method class files (FixtureBogusExtensions.cs)
  • Generate integration test data factories or hybrid generator classes
  • Generate BogusAutoDataAttribute for use with xUnit Theory tests
  • Use with OmitOnRecursionBehavior to handle circular references

Reference Resources

Original Article

Official Documentation

Related Skills