Follow me: Jack Histon's Twitter Share on LinkedIn Share on Google+ RSS Feed

Author avatar

Welcome. I am Jack Histon. My career would not be what it is today without dedication and hard work from software bloggers. My purpose is to give back to that online community.

Automating The Arrange Phase With AutoFixture

Thursday, 02 November 2017

Tweet about this on Twitter Share on Facebook Share on LinkedIn Share on Google+ Pin on Pinterest Share on Reddit Share on StumbleUpon

In a previous article, we refactored a unit test in two ways. The builder and fixture patterns encapsulated an object's creation, which simplified the arrange phase of a unit test.

These two patterns produced a lot of boiler-plate code. It would be nice to never have to create an arrange phase in the first place.

AutoFixture tries to produce unit tests with no arrange phase. As the AutoFixture GitHub page describes:

AutoFixture is an open source library for .NET designed to minimize the 'Arrange' phase

Reducing the arrange phase of a unit test is a powerful idea. It allows the unit test to become more readable, and concentrates the unit test on what is being tested.

How Can AutoFixture Help?

The purpose of a unit test's arrange phase is to generate auxiliary classes that are used by the system under test. So primarily, AutoFixture is concerned with the generation of primitive and complex types:


var fixture = new Fixture();

// Creating strings
var str = fixture.Create<string>();

Console.WriteLine(str);

// Output: dbbfdba4-4542-45ea-a15f-d26437af25a2

We can see in the example that the main class to use with AutoFixture is the Fixture class itself. We use the fixture to create a primitive type of string. By default, this produces a GUID cast to a string.


var fixture = new Fixture();

// Creating numbers
var num = fixture.Create<int>();

Console.WriteLine(num);

// Output: 133

This example shows the generation of an int primitive.

These are very simple cases of needing to generate random test data, but what about more complex types?


public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }

    public override string ToString() => $"Id: {Id}, Name: {Name}";
}

var fixture = new Fixture();

// Creating complex types
var person = fixture.Create<Person>();

Console.WriteLine(person);

// Output: Id: 108, Name: Name1b3bd547-ae34-4a28-8f75-0e807f073413

The power of AutoFixture can be seen here. Using reflection, we have automatically generated a dummy object of type Person.

AutoFixture can create lists of data:


// Manipulating lists
var list = new List();
fixture.AddManyTo(list);

Console.WriteLine(string.Join($"PERSON: {Environment.NewLine}", list));

// Output:  Id: 159, Name: Name591a8bf9-1479-4506-b584-c1d187cc4284PERSON:
//          Id: 239, Name: Nameefad8d54-9ed1-4e8f-b72e-5adaa2573c44PERSON:
//          Id: 207, Name: Name0613681d-19d2-4112-bc90-167714525015

You can specify the length, as well as how to randomise the data.

Using AutoFixture With xUnit

Out of the box, AutoFixture provides an easy way to integrate itself with xUnit:


[Theory, AutoData]
public void ShouldDoSomethingWithAPerson(Person person)
{
    ...
}

This will autogenerate the variable person of type Person. Rather than having an arrange phase, we have shortened the unit test to only needing an act and assert phase.

Summary

Arranging a unit test can be improved with popular design patterns, but AutoFixture removes the need for arrangement entirely. Adding AutoFixture to your toolbox will make unit tests easier to understand, and more importantly, more maintainable.

AutoFixture can be used in seeding data, performance analysis, user interface demonstrations, test driven development, and much more.

Share with a friend

Please share this blog post so others can learn from it as well.

Tweet about this on Twitter Share on Facebook Share on LinkedIn Share on Google+ Pin on Pinterest Share on Reddit Share on StumbleUpon

Recent Posts

Archives



© 2017 - Jack Histon - Blog