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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Introduction to Salesforce Batch Apex [Video]
  • TDD Typescript NestJS API Layers with Jest Part 1: Controller Unit Test
  • 5 Principles of Production Readiness
  • Alexa Skill With Local DynamoDB

Trending

  • Data Quality: A Novel Perspective for 2025
  • Why Database Migrations Take Months and How to Speed Them Up
  • How Can Developers Drive Innovation by Combining IoT and AI?
  • AI-Driven Root Cause Analysis in SRE: Enhancing Incident Resolution
  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 user avatar
Jonathan Danylko
·
Oct. 26, 16 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
6.5K 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.

Related

  • Introduction to Salesforce Batch Apex [Video]
  • TDD Typescript NestJS API Layers with Jest Part 1: Controller Unit Test
  • 5 Principles of Production Readiness
  • Alexa Skill With Local DynamoDB

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: