Make your Unit Testing easier with AutoFixture

AutoFixture is a free open-source library and at the time of publishing this blog, it has over 31M downloads on NuGet. AutoFixture will help you to

  1. Increase Productivity
  2. Reduce test maintenance
  3. Improve test readability
  4. Less test code

You may have already familiar with the 3 phases of unit testing. Arrange, Act and Assert. AutoFixture can help us in the arrange phase and help us to create the anonymous test data. As per the documentaton.

… is a library for .NET designed to minimize the ‘Arrange’ phase of your unit tests in order to maximize maintainability. Its primary goal is to allow a developer to focus on what is being tested rather than how to set up the test scenario.

AutoFixture Supported Framework

One important thing before we proceed, AutoFixture is independent of the testing framework or test runner. However, you can find various packages like Autofixture.XUnit or for NUnit but these packages are just to add the deeper integration with a specific testing framework.

autofixture

To know more, please check the documentation.

Fixture Class

The Fixture class is provided in the basic AutoFixture NuGet package. We can use the fixture class inside your test method in the arrange phase. In arrange phase you can create the instance of fixture class to create anonymous test data. So or example we can ask the fixture instance to create the anonymous Strings, Number, Bytes, Enum, GUID’s, Custom or complex objects, Fake interface instances, collection.

        [Fact]
        public void UsingAutoFixureInstance()
        {
            //arrange
            var fixture = new Fixture();
            
            fixture.Create<string>();
            fixture.Create<int>();
            fixture.Create<byte>();
            fixture.Create<DateTime>();
            fixture.Create<Guid>();
            fixture.Create<MailAddress>();
            fixture.Create<Order>();
            
            //act

            //assert
        }

In the next article, we will cover more on how to create anonymous data with examples to illustrate how it can you to tackle complex problems and help you to write more manageable code with fewer efforts. So stay tuned!

1 thought on “Make your Unit Testing easier with AutoFixture”

  1. Pingback: How to create anonymous test data with AutoFixture? - Code Hotfix

Leave a Comment

Your email address will not be published. Required fields are marked *