Post

Introduction to AutoFixture

AutoFixture is a .NET library designed to simplify the process of creating test data in unit tests. It automatically generates object instances with random data, helping you focus more on writing meaningful tests instead of manually creating and configuring test objects.

Advantages of AutoFixture

  • Simplicity: Automatically generates test data, saving time on setting up test objects manually.
  • Customization: Supports customization for objects, allowing you to override default behaviors as needed.
  • Integration: Easily integrates with popular testing frameworks like xUnit, NUnit, and Moq.

Examples of Usage

1. Install the nuget package

To add AutoFixture to your project, install the NuGet package. You can do this in Visual Studio:
Right-click on your project -> Manage NuGet Packages… -> Browse
Search for AutoFixture, select it, and click Install.
In some cases, you may want to install AutoFixture.AutoMoq and AutoFixture.Xunit2 as well.

2. Basic Example

Once installed, you can start using Fixture to automatically create instances of your objects.

1
2
3
4
5
6
7
8
9
[Fact]
public void ShouldCreateCustomer()
{
    var fixture = new Fixture();
    var customer = fixture.Create<Customer>();

    Assert.NotNull(customer);
    Assert.IsType<Customer>(customer);
}

In this case, AutoFixture automatically populates the Customer object with random data.

You can customize some fields inside your object, letting AutoFixture decide about others:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Fact]
public void ShouldCreateCustomer()
{
    var fixture = new Fixture();
    var customer = fixture.Build<Customer>()
        .With(c => c.Name, "John Doe") //Fixed Name
        .Witout(c => c.Orders) // Null Orders
        .Create();

    Assert.NotNull(customer);
    Assert.IsType<Customer>(customer);
    Assert.Equal("John Doe", customer.Name);
    Assert.Null(customer.Orders);
}

3. Customizing Object Creation

You can also customize object creation for all instances of the same type using the .Customize method.

1
2
3
4
5
6
7
8
9
10
11
12
13
[Fact]
public void ShouldCreateCustomerWithSpecificName()
{
    var fixture = new Fixture();

    // Fixed Name for all created by fixture Customer's
    fixture.Customize<Customer>(c => c.With(x => x.Name, "John Doe"));

    var customer = fixture.Create<Customer>();

    Assert.Equal("John Doe", customer.Name);
}

This ensures that the Name property of the Customer is set to “John Doe”, while other properties are still randomly generated.

4. Using AutoFixture with Moq

You can combine AutoFixture with Moq to create mock objects automatically.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Fact]
public void ShouldMockRepository()
{
    var fixture = new Fixture();
    var mockRepo = fixture.Create<Mock<IRepository<Customer>>>();

    var customer = fixture.Create<Customer>();
    mockRepo.Setup(r => r.Add(customer));

    mockRepo.Object.Add(customer);

    mockRepo.Verify(r => r.Add(customer), Times.Once);
}

5. Using AutoFixture with xUnit

AutoFixture can be used directly with xUnit via its attributes, making test writing even simpler.

1
2
3
4
5
[Theory, AutoData]
public void ShouldCreateCustomerWithAutoData(Customer customer)
{
    Assert.NotNull(customer);
}
1
2
3
4
5
[Theory, AutoData]
public void ShouldCreateCustomerWithAutoData(IFixture fixture)
{
    var customer = fixture.Create<Customer>();
}

With the [AutoData] attribute, AutoFixture automatically generates the param object and passes it to the test method.

This post is licensed under CC BY 4.0 by the author.