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 owns UseTech Design, a Michigan development company focused on .NET and Microsoft technologies. Ondrej is a Microsoft MVP in Visual Studio and Development Technologies and an active contributor to the Michigan software development community. He works across many industries -- finance, healthcare, manufacturing, and logistics -- and has expertise with large data sets, algorithm design, distributed architecture, and software development practices.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube