GenFu is a test and prototype data generation library for .NET apps. It understands different topics – such as "contact details" or "blog posts" and uses that understanding to populate commonly named properties using reflection and an internal database of values or randomly created data.
As a quick sample, I attempted to replace the Sample Data Generator in the ASP.NET5 Music Store app with GenFu. With the right GenFu configuration, it worked like magic and I was able to remove over 700 lines of code!
As part of that process, it became clear that our documentation related to configuring more complex scenarios was slightly lacking. We are working on creating official project documentation. In the mean time, this post can serve as the unofficial documentation for GenFu.
GenFu is available via NuGet and can be added to any .NET 4.5, 4.6, or aspnet5 project.
Let's say you have a simple Contact class as follows:
To generate a list of random people using the GenFu defaults, simply call the
This simple console app will output the following:
That was easy, and the data generally looks pretty realistic. The default is to generate 25 objects. If you want more (or less), you can use an overload of the
ListOf method to specify the number of objects you want. Okay, but what if the defaults aren't exactly what you wanted? That's where GenFu property filler configuration comes in.
Configuring Property Fillers
GenFu has a fluent API that lets you configure exactly how your object's properties should be filled.
Manually Overriding Property Fillers
Let's start with a very simple example. In the example above, the Id is populated with random values. That behaviour might be fine for you, but if you are using GenFu to generate random data to seed a database this will probably cause problems. In this case we would want the Id property to be always set to 0 so the database can automatically generate unique ids.
Now all the Ids are 0 and the objects would be safe to save to a database:
Another option is to use a method to fill a property. This can be a delegate or any other method that returns the correct type for the property you are configuring:
With that simple change, we now have sequential ids. Magic!
There is also an option that allows you to configure a property based on other properties of the object. For example, if you wanted to create an email address that matched the first name/last name you could do the following. Also, notice how you can chain together multiple property configurations.
This can be simplified greatly by using string interpolation in C#6.
Property Filler Extension Methods
In some cases, you might want to give GenFu hints about how to fill a property. For this there is a set of With* and As* extension methods available. For example, if you wanted an integer property to be filled with values within a particular range:
IntelliSense will show you the list of available extensions based on the type of the property you are configuring.
IntelliSense showing extensions for a String property
Extensions are available for String, DateTime, Integer, Short, Decimal, Float and Double types.
In some situations, you might want to fill a property with a random value from a given list of values. A simple example of this might be a boolean value where you want approximately 2/3rds of the values to be true and 1/3 to be false. You could accomplish this using the WithRandom extension as follows:
WithRandom method is also useful for wiring up object graphs. Imagine the following model classes:
We could use GenFu to generate 1,000 IncidentReports that were reported by 100 different Contacts as follows:
Wrapping it up
That covers the basics and you are now on your way to becoming a GenFu master. In a future post we will cover how to extend GenFu by writing your own re-usable property fillers. In the mean time, give GenFu a try and let us know what you think.