Generate Test Data for tests using AutoFixture

ExecuteAutomation
ExecuteAutomation
Published in
2 min readOct 4, 2021

While it comes to testing application, regardless of Unit test, Integration or UI test, Test Data plays key role. In some tests, test data setup become a very complex ceremony and sometimes becomes headache to perform test data setup.

In order to make the setup of test data a breeze, we are going to use the AutoFixture Library !

AutoFixture

AutoFixture makes it easier for developers/testers 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.

It does so by removing the need for hand-coding anonymous variables as part of a test’s Fixture Setup phase.

AutoFixture setup

AutoFixture is an open-source tool available as a nuget package and can be installed in project using Visual studio or any IDE of your choice with Nuget Package Manager.

AutoFixture is available for both XUnit and NUnit test runners, hence we get the utmost compatibility for our testing.

XUnit Test setup vs AutoFixture

If you have read my earlier posts on Data Driven testing with XUnit, we used something called as [InlineData], [MemberData] to pass data for test methods in XUnit as shown below

As you can see, both the above method has to create lot of ceremony to setup test data like these

[InlineData("admin", "password")]        
[InlineData("admin", "password2")]
[InlineData("admin", "password3")]
[InlineData("admin", "password4")]

But using AutoFixture, we can do this much easily as shown below

As you can see the UserName, Password and Email are set using the below lines

var userName = new Fixture().Create<string>();            
var password = new Fixture().Create<string>();
var email = new Fixture().Create<MailAddress>();

The Fixture().Create<string>() method will create a random guid based string as username, password but also create a random email address, since it uses the type MailAddress

AutoFixture to create Initial Object State

When writing tests, we may typically need to create some objects that represent the initial state of the test. Often, an API will force you to specify much more data than you really care about, so you frequently end up creating objects that has no influence on the test, simply to make the code compile.

AutoFixture can help by creating such Anonymous Variables for you.

As you can see the above code, we are using the builder pattern of AutoFixture to create anonymous data for RegisterUserModel class and also customise the value for type email with karthik@ea.com

var model = fixture.Build<RegisterUserModel>()                               .With(x => x.Email == "karthik@ea.com")                               .Create();

Video discussion

Here is the complete video discussion of this article in our ExecuteAutomation YouTube channel

Thanks for reading the post and hope you like the power of AutoFixture !

Thanks,

Karthik KK

--

--

ExecuteAutomation
ExecuteAutomation

ExecuteAutomation Ltd is a Software testing and its related information service company founded in 2020. Info available in YouTube and Udemy as video courses .