Your first test

Use Fixture.Create to build anonymous arrange data in a simple unit test.

This page walks through a small unit test where AutoFixture replaces hand-written arrange values. You use new Fixture() directly inside the test method.

Example scenario

You are testing LineTotalCalculator.Calculate, which returns quantity × unitPrice. The test should verify the line total matches that product. You need a quantity, a unit price, and a calculator instance for the arrange step, but the exact values do not matter.

Example

var fixture = new Fixture();
var quantity = fixture.Create<int>();
var unitPrice = fixture.Create<decimal>();
var sut = fixture.Create<LineTotalCalculator>();

var total = sut.Calculate(quantity, unitPrice);

Assert.Equal(quantity * unitPrice, total);

How it works

  • new Fixture() — creates a data generator local to this test
  • Create<int>() and Create<decimal>() — anonymous operands (quantity and unit price)
  • Create<LineTotalCalculator>() — builds the calculator (no manual new when the type has a simple constructor)
  • sut — the system under test (SUT); the instance under test
  • Assert.Equal(quantity * unitPrice, total) — expected value comes from the operands you arranged, not from re-running production code

Use new Fixture() when a single test needs one-off anonymous data. When many tests share the same customizations, consider a fixture factory or xUnit.net 3 AutoData.

Example types

The test above uses this type:

public class LineTotalCalculator
{
    public decimal Calculate(int quantity, decimal unitPrice) =>
        quantity * unitPrice;
}

Next steps