---
title: "Anonymous test data with AutoFixture"
description: "Often times we have unit or integration tests that rely on some input data. The easiest solution is just to take some hard-coded values and move on with life. This has some major downsides: \n\nGiving specific values in a test carries meaning, but we are often times not interested in that. We just need to pass the object around to fulfill the API. Also the simplest solution to fulfill your test is literally checking against those values. \n\nHere is an elegant solution to that problem: `AutoFixture`. I will show you what it can do, especially in combination with `xUnit`."
updated: 2022-11-19
tags: ["Unit Test", "Testing", "C#", ".NET"]
canonical: "https://steven-giesel.com/blogPost/0a7d1bff-6c97-4d98-97cf-d72cd2b3888e/anonymous-test-data-with-autofixture"
---

# Anonymous test data with AutoFixture

Often times we have unit or integration tests that rely on some input data. The easiest solution is just to take some hard-coded values and move on with life. This has some major downsides: 

Giving specific values in a test carries meaning, but we are often times not interested in that. We just need to pass the object around to fulfill the API. Also, the simplest solution to fulfill your test is literally checking against those values. 

Here is an elegant solution to that problem: `AutoFixture`. I will show you what it can do, especially in combination with `xUnit`.

[**AutoFixture**](https://github.com/AutoFixture/AutoFixture):
> Write maintainable unit tests, faster.
> AutoFixture makes it easier for developers to do Test-Driven Development by automating non-relevant Test Fixture Setup, allowing the Test Developer to focus on the essentials of each test case.

You can check on the given website how to setup a test, which uses **AutoFixture**. I want to show you something else. The magic and power of data-driven tests in combination with **AutoFixture**. The neat thing is, that for all the major testing frameworks, there is an extension, which makes it super trivial to write those tests. My all-time favorite testing framework is **xUnit**. So to get the magic, we have to install this nuget package: [*AutoFixture.Xunit*](https://www.nuget.org/packages/AutoFixture.Xunit). Or just simply:

```no-class
dotnet add package AutoFixture.Xunit
```

## The first test
For the whole magic, we can use the `AutoData` attribute like this:
```csharp
[Theory]
[AutoData]
public void TestOne(string name)
{
    var sut = new MyFooService();

    var result = sut.MyCall(name);

    Assert.NotNull(result);
}
```

As you can see, we never specified `name` but still it is working. And here we can see what the output of `name` is:

![name](https://linkdotnetblog.azureedge.net/blog/20221119_XUnitAutoFixture/XUnitExecution.webp)

With all of the modern testing framework, we can write data-driven tests. But this can mean that on one row a date is important whereas on another it isn't anymore. And also here the package got you covered. In the fashion of `InlineData`, which is native to **xUnit**, the **AutoFixture** package introduces **AutoInlineData** where we can set 0 to all parameters at will.

```csharp
[Theory]
[InlineAutoData]
[InlineAutoData(31)]
[InlineAutoData(31, "Steven")]
public void TestTwo(int age, string name)
{
    var sut = new MyFooService();

    var result = sut.MyCall(name, age);

    Assert.NotNull(result);
}
```

![name2](https://linkdotnetblog.azureedge.net/blog/20221119_XUnitAutoFixture/XUnitExecution2.webp)

With **AutoFixture** in general you could also create automatically complete classes if you want:

```csharp
[Theory]
[InlineAutoData]
public void TestThree(string name, MyFooService sut)
{
    var result = sut.MyCall(name);
    
    Assert.NotNull(result);
}
```
## Conclusion
With **AutoFixture** and the power of **xUnit** (or **nUnit** / **MSTest**) you can have tests, which are more robust, simple, and the stuff which should carry meaning, does it!

## Resources
* Source code to this blog post: [here](https://github.com/linkdotnet/BlogExamples/tree/main/AutoFixtureXUnit)
* All my sample code is hosted in this repository: [here](https://github.com/linkdotnet/BlogExamples)
