DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Introduction to Salesforce Batch Apex [Video]
  • TDD Typescript NestJS API Layers with Jest Part 1: Controller Unit Test
  • 5 Principles of Production Readiness
  • Share Delta Lake Tables Between Databricks and Snowflake

Trending

  • How You Clear Your HTML5 Canvas Matters
  • How to Keep AI Agents From Going Rogue Using Docker Sandbox
  • Part 2: Securing and Scaling Goose-to-Java Agent Traffic With agentgateway
  • Rebuilding Elasticsearch Under Load: Unsafe Watermarks and a Sampling Blind Spot

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. Using Faker and NBuilder To Generate Massive Data for Your Unit Tests

Using Faker and NBuilder To Generate Massive Data for Your Unit Tests

Check out how you can perform unit testing using two different tools for generating lots of mock data. See how these tools work separately and together.

By 
Jonathan Danylko
·
Oct. 26, 16 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
6.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

One of the hardest things in coding is naming things.

When writing unit tests, you need some test data. Sometimes even names of companies or people simulating production-style data (complete with commas and apostrophes).

Back in July of last year, I mentioned the fastest way to mock a database for unit testing, but everyone doesn't need that much depth of unit testing.

In smaller cases of unit testing, wouldn't it be nice to generate a list of 15 records with various types of values for a specific data type?

That's where NBuilder and Faker.Net enter the picture.

Getting Started

For your unit tests, download NBuilder or Faker from NuGet.

For NBuilder:

install-package NBuilder

For Faker:

install-package Faker.Net

Once you've added those libraries, you can get started immediately.

Faker.Net

Faker.Net is a library from Ben Smith and Ollie Riches that creates a random set of dummy data using static methods.

var companies = new List<string>();
var count = 100;
for (var i = 0; i < count; i++)
{
    companies.Add(Company.Name());
}

If you need 100 company names, call the static Company.Name() to create a fake company name.

Faker also allows you to create addresses, phone numbers, internet addresses, lorum ipsum text, and yes, even a name generator.

NBuilder

NBuilder is a way to create data using simple fluent syntax.

Each method provides an easy way to build your dummy data.

Following the same example from above using NBuilder instead, the code would look something like this:

var companyList = Builder<Company>
    .CreateListOfSize(100)
    .TheFirst(20).With(e => e.Title = "My Company Name")
    .TheNext(40).With(e=> e.Title = "Company Name that is too long.")
    .Build();

Now that you created your data, you can move this code into a setup method in your unit tests and test that particular data.

Some of the features in NBuilder include building a hierarchy of objects, "operable" extensions, and excellent configurability.

Which One To Use?

I know what you're thinking. Which one is better for unit testing?

Honestly, I think it's best to use both.

Use Faker for the data while using NBuilder to build the structure.

For example:

var companyList = Builder<Company>
    .CreateListOfSize(100)
    .TheFirst(50).With(e => e.Title = Faker.Company.Name())
    .TheNext(50).With(e=> e.Title = Faker.Name.FullName())
    .Build();

I decided to throw the Name.FullName() in there for unit testing a name instead of a company.

Conclusion

It's hard to come up with companies, names, and text to help you unit test, but Faker and NBuilder assists with all tasks including structure and, of course, naming things.

Is there another library out there to help with populating data for unit tests? Post your comments below.

unit test Data (computing) MASSIVE (software)

Published at DZone with permission of Jonathan Danylko. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Introduction to Salesforce Batch Apex [Video]
  • TDD Typescript NestJS API Layers with Jest Part 1: Controller Unit Test
  • 5 Principles of Production Readiness
  • Share Delta Lake Tables Between Databricks and Snowflake

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook