Generating Test Data In C#

Often times you may want to create a set of data for which you would use for testing purposes, and no one would like to enter those data manually into the database, or create instances of different object one after the other to seed to the database. We often feel the need that this kind of task should be automated, and we begin to search for tools or software which we can run to insert a bunch of meaningful test data into our database, or something that can look at your class definition and create instances of that class. This is where AngelaSmith comes to the rescue.

AngelaSmith is an OSS project hosted on GitHub which helps you create instances of objects on the fly, and you can install it via Nuget. It’s very smart such that it looks at the datatype and the property names defined in a class, and creates something meaningful. Now let’s see some example.

First install it via NuGet

install-package AngelaSmith

And you define a class, say a UserInfo class

public class UserInfo
public int Id { get; set; }
public string FirstName { get; private set; }
public string LastName { get; set; }
public int Age { get; set; }
public DateTime BirthDay { get; set; }
public string Email { get; set; }
public string Twitter {get; set; }

And you need just one instance UserInfo, just call Angie.FastMake<>()

var person = Angie.FastMake<UserInfo>();

And you get the following result:

Id: 25
FirstName: Scott
LastName: James
Age: 50
BirthDay: 01/01/0001 19:10:10
Twitter: @ARivera

You will notice that the library was smart enough to give meaningful names, email and a twitter handle. And If you want a list, call the FastList<>() method like this

var people = Angie.FastList<UserInfo>();

You can also configure the BirthDay to give only future dates like this

var persons = Angie.Configure<Person>().Fill(p => p.BirthDay).AsFutureDate().MakeList<UserInfo>();

or the Age to be within a range of 40 to 60

var persons = Angie.Configure<Person>().Fill(p => p.BirthDay).WithinRange(40, 60).MakeList<UserInfo>();

If you want to seed or insert these objects into your database, all you need to do is call the insert method on your database passing in the created objects.
I look forward to more features and enhancement to this library, and I hope you enjoy it.


One thought on “Generating Test Data In C#

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s