Code Focused

Are You Faking? Dynamically Generate Test Data at Run Time

Quickly populate your applications with fake names, addresses, phone numbers and much more with freely available libraries.

One of the recurring problems in showing off a demo of a great new application you're building is not having any good data to populate your fields. John Smith living at 123 Oak St. is fine for a single record, but it gets repetitive when dealing with multiple records. Also, it distracts from the UI and brings focus to the data, instead. Fortunately, there are some libraries that can solve this problem for you by allowing you to generate all sorts of test data at run time.

The most popular and versatile I've found is Faker.Net, available via the NuGet Package Manager or the project's GitHub page. Faker.Net is an open source C# port of the Ruby Faker gem and makes it extremely easy to generate test data.

Follow along by either downloading the provided sample application, or by creating a new Windows Forms application. If you choose to create a new application, you'll need to get the Faker.Net assembly. The easiest way to get the assembly is with NuGet. In case you aren't familiar with it, you can open NuGet from inside Visual Studio by going to Project | Manage NuGet Packages. Once opened, search for Faker.Net as shown in Figure 1 and then install the Faker.Net library. In the sample, I also dropped a DataGridView onto the main form and named it CustomersGrid.

[Click on image for larger view.] Figure 1. Using NuGet to find the Faker.Net library.

Populating the Data Grid with Fake Data
When starting the sample application, you'll see a data grid containing some names and other personal information of fictitious people, as shown in Figure 2. The data behind each row is stored in an instance of a Customer class:

Public Class Customer
  Public Property FirstName As String
  Public Property LastName As String
  Public Property Company As String
  Public Property Address As String
  Public Property City As String
  Public Property State As String
  Public Property ZipCode As String
  Public Property PhoneNumber As String
  Public Property EmailAddress As String
[Click on image for larger view.] Figure 2. A sample application showing a data grid populated by dynamically generated fake data.

End Class The Customer class is then used by the FakeCustomerDataRepository, defined in Listing 1. The purpose of the repository is to act as the layer between the application and the data. Instead of pulling data out of a database, however, upon instantiation it creates a list of customers and fills it with fake customer data.

Listing 1. The FakeCustomerDataRepository.vb class acts as a fake data source and will eventually be replaced by a class that gets data from the real data source.
Public Class FakeCustomerDataRepository
  Private _customers As List(Of Customer)

  Public Sub New()
    _customers = New List(Of Customer)()

    For i As Integer = 0 To 10
      _customers.Add(New Customer() With {
        .FirstName = Faker.Name.First(),
        .LastName = Faker.Name.Last(),
        .Company = Faker.Company.Name(),
        .Address = Faker.Address.StreetAddress(),
        .City = Faker.Address.City(),
        .State = Faker.Address.UsStateAbbr(),
        .ZipCode = Faker.Address.ZipCode(),
        .PhoneNumber = Faker.Phone.Number(),
        .EmailAddress = Faker.Internet.Email()
      })
    Next
  End Sub

  Public Function GetCustomers() As List(Of Customer)
    Return _customers
  End Function
End Class

Notice the fake data is created by making calls into a few of the many methods that Faker.Net provides. The methods themselves can be called statically (without an instance of a Faker object), and are grouped together logically into classes based on the type of data they generate. There are quite a few options that allow you to generate test data in a variety of formats, of which I'm only using a handful. Rather than including an exhaustive listing of every method available, I've listed the classes into which the various methods are grouped in Table 1. As you drill down into the classes, IntelliSense within Visual Studio will show the available methods, all of which are named well and generally self-explanatory.

Feature Faker.Net Faker
Address U.S. and U.K. Formats U.S. and U.K. Formats (no U.S. states)
Company Names and Catchphrases Names and Catchphrases
Internet E-mails, Domains, IP addresses Domain, E-mail, URL
Lorem Ipsum Sentence or Paragraph Sentences
Name First, Last, Suffix, Prefix, Full First and Last (can specify gender)
Phone Various Formats Local or International
Dates - Birthdates (random in range)

Table 1. Faker.Net and Faker, two test-data generation libraries available from within NuGet.

The last step is to wire up the grid to show the data when the form loads. I do this by setting the grid's DataSource property to the list of fake data from the FakeCustomerDataRepository class:

Public Class CustomersForm
  Private ReadOnly _repository As FakeCustomerDataRepository = 
    New FakeCustomerDataRepository()

  Private Sub CustomersForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    CustomersGrid.DataSource = _repository.GetCustomers()
  End Sub
End Class

Other Libraries
In the event Faker.Net doesn't meet your specific needs, there are alternatives that have some other options for generating data. One such alternative is Faker, a similar library that behaves in almost the same way, but with some different options for test-data generation (see Table 1 for a comparison of the two libraries).

As you can see, using a library such as Faker.Net to generate test data can provide you with many benefits. Not only does it make it easier to visualize how your interfaces will look when populated with data, it also prevents production data from accidentally making its way into your prototypes.

About the Author

Ondrej Balas is the owner of UseTech Design, a small development company based in Troy, Mich., that focuses primarily on the .NET Framework and other Microsoft technologies. Like many other developers, he began writing code at a young age and hasn't stopped. Most of the work he does today is in big data, algorithm design and software architecture.

comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.