.NET Core Generating Test Data

I am doing work for a healthcare client and because of the highly sensitive nature of their data I wanted to find a way to generate test data that would be pretty close to the real world. Here is how I did it I hope you find it helpful:

The first step is to get a good data generator that works with .NET Core, after doing some research I was very happy to find Bogus (https://github.com/bchavez/Bogus)

  • In my application I need to create records of patient code blue events (cardiac arrest). To do so we just define a rule for each field that we want data generated for. As an example we are generating a random alphanumeric string of up to 30 characters for the DeviceId field.Notice that we have a start time and an end time fields. Naturally we want an EndTime > StartTime in any given record bogus allows us to do this easily by providing us a way to access the auto-generated start time and by allowing us to use that value as the lower boundary for our date.
 var codeRecordFaker = new Faker()
                    .RuleFor(o => o.DeviceId,
                             f => f.System.Random.AlphaNumeric(30).ToString())
                    .RuleFor(o => o.Comment, f => f.Lorem.Sentence(2))
                    .RuleFor(o => o.StartTime, f => f.Date.Past())
                    .RuleFor(o => o.EndTime,
                            (f, o) => f.Date.Between(o.StartTime.DateTime,
                    .RuleFor(o => o.DeviceDescription,
                             f => f.System.Random.AlphaNumeric(100));


  • There are other rules that I added that I didn’t include in the code above to keep it as concise as possible (let me know if you want the full code and I will be happy to post it, I just didn’t want to fight with the wordpress code formatting limitations). For example some of the fields have a set of predefined values that the user selects from (as opposed from just using a random value). In order to randomly select a value from a set of predefined values in bogus we first populate an array with the predefined values (in this case I am getting those values from the database) and we then use the PickRando() method to randomly select one of those values (it can also be used with an array of objects it doesn’t necessarily have to be primitive values).


 //Load the values predefined in the db for the multiple choice fields.
 //We will later on pick a random value for each
 //test data record that we create.
 var outcomes = context.Answer.Where(a => a.QuestionId == outcomeQuestion.Id).Select(r => r.Id).ToArray();

//Randomly selects one of the predefined patient outcome values.
.RuleFor(o => o.Outcome, f => f.PickRandom(outcomes));


  • Some of the fields in the application can have one or more values. In order to randomize not only the value of the fields but also the amount of values of those fields I just randomized the number of records to generate anywhere from 0 to 5 values for those fields an example of that is the Onset Monitor fields (ignore the model specific properties):
 //Generate a random number of Onset Monitors.
 for (var i = 0; i < f.Random.Number(5); i++)
    answers.Add(new AnswerRecord()
       QuestionId = onsetMonitorsQuestion.Id,
       Question = onsetMonitorsQuestion,
       AnswerId = f.PickRandom(onsetMonitors)


  • One last note about the Bogus library. Initially I used the Person object to attempt to generate First and Last Names. However, I noticed that the generated names were identical in the many fields that I was using them for each record (each record has name entries for Doctor, Nurse, ICU Nurse, etc). The person object is just that an object that’s generated once but it’s fields don’t change within the scope of a record generation event. If you run into this scenario use the Name.FirstName() and Name.LastName() methods instead….One nice feature of those methods is that they let you pass by parameter the gender so that the generated name reflects it which is kind of neat.


  • Now we take the data that we generated and add it to our db using Entity Framework:
var codeRecords = codeRecordFaker.Generate(50);



  • Finally we add a call to our seed method in Startup.cs
 // This method gets called by the runtime.
//Use this method to configure the HTTP request pipeline.
 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    if (env.IsDevelopment())

       using (var serviceScope = app.ApplicationServices.GetRequiredService().CreateScope())

1 thought on “.NET Core Generating Test Data”

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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