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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  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.

Jonathan Danylko user avatar by
Jonathan Danylko
·
Oct. 26, 16 · Tutorial
Like (5)
Save
Tweet
Share
5.23K Views

Join the DZone community and get the full member experience.

Join For Free

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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Factors When Selecting a Database
  • OpenID Connect Flows
  • Using the PostgreSQL Pager With MariaDB Xpand
  • Debugging Threads and Asynchronous Code

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: