Click here to Skip to main content
Click here to Skip to main content

Creating mock data for software testing

, 30 Dec 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Need some mock data to test your app? MockData is a free library

Introduction

I'am developing MockData library as a .Net's tool to automatically generate in-memory different "fake data" format as Company Name or Person Name. I wrote this post to share my idea and to get feedback from other developers about how enhance features, I've still planned more features as financial data class generator, localization, export the data in SQL Insert Script file or CSV files. I hope you will join me.  

Background

If you are developing an application, you will want to make sure you are testing it under conditions that closely simulate a production environment. In production, you will have an army of users banging away at your app and filling your database with data, which puts stress on your code. I you are hand-entering data into a test environment one record at time using the UI, you are never going to build up the volume and variety of data that your app accumulate in a few days in production. Worse, the data you enter will be biased towards you are own usage patterns and won't match real-world usage, leaving important bugs undiscovered. Some application frameworks, like as Ruby on Rails, have great data mocking libraries. But not everyone is a programmer, has time to learn a new framework, or is at liberty to adopt a new platform.

Solution  

This post introduces a solution for this topic.  MockData allows you to quickly and easily generate large amounts of randomly generated test data based on your own specs which you can then load directly into your classes for test environment. 

Using the code 

I suppose we have the following class: 

public class Person
{
   public int ID { get; set; }
   public String FirstName { get; set; }
   public String LastName { get; set; }
   public String Address { get; set; }
   public String City { get; set; }
   public String ZipCode { get; set; }
   public String Country { get; set; }
   public bool Status { get; set; }
   public String Details { get; set; }
}

Using MockData we can write the following class :

public static class DummyData
{
    public static IList<Person> GetDataPerson(int number)
    {
        int personIdIncrement = 1;
        return Enumerable.Range(0,number).Select(o =>  new Person
	{
	   ID = personIdIncrement++ ,
           FirstName = MockData.Person.FirstName(),				
           LastName = MockData.Person.Surname(),
	   Address = MockData.Address.StreetAddress(),
	   City = MockData.Address.City(),
	   ZipCode = MockData.Address.ZipCode(),
	   Country = MockData.Address.DefaultCountry(),
	   Status = new Random().Next(0, 2) == 1 ? true : false,
	   Details = MockData.Lorem.Paragraph(1)
	   }).ToList();
	}
    }
}

where number is the number of record to generate in the list. We can write :

   List<Person> list = DummyData.GetDataPerson(300);

that returns a list with 300 records of the type Person. At the moment MockData has the following static classes with related methods: 

  • Address
    • City
    • Country
    • DefaultCountry
    • State
    • StateAbbr
    • ZipCode
  • Company
    • BS
    • Name
    • CatchPhrases
  • Internet
    • Email
    • FreeEmail
    • UserName
    • DomainName
    • DomainWord
  • Lorem
    • Words
    • GetFirstWord
    • Sentence
    • Paragraph
    • Paragraphs
  • Person
    • FirstName
    • Surname
    • FullName
  • Product
    • Department
    • ListDepartment
    • ProductName
  • Utils
    • Boolean
    • RandomDate 

Points of Interest

When your test database is filled with realistic looking data, you'll be more engaged as a tester. When you demonstrate new features to others, they'll understand them faster. Real data is varied and will contain characters that may not play nice with your code, such as apostrophes, or unicode characters from other languages. Testing with realistic data will make your app more robust because you'll catch errors that are likely to occur in production before release day. I am still developing the tool, with more features as financial data class generator, localization, export the data in SQL Insert Script file or CSV file. 

Next Step

I am exploring enterprise design patterns in N-Layered application looking at Unit Test Design Development and Integration Tests. In my experience when our classes deal with data, we need to use fake data in our unit test. A unit test should do exactly as the name implies and test 'One Unit'. If I am testing a controller I forget all about the data access. I should be removing all of the calls to database context in my mind and replacing them with a black box method call as if those operations were unknown to me. For example in a MVC application we can use the repository pattern. I have a repository, say ProductRepository : IProductRepository, which will perform all of my product database operations. If I were to test my controllers would I want the tests to test my repository, my database access, and the controller logic itself? Of course not! There are many 'units' in this pipeline. What you want to do is create a fake repository with fake data which implements IProductRepository to allow you to test the controller logic in isolation. If I wanted to test the ProductRepository logic, how would I do that? My opinion is that the easiest way is to use a fake datasource. This will allow me to make sure that when I'm trying to get a user by ID, it actually gets the user by ID and so forth. But this is the topic of my next article.

History

I've just released a beta version 0.0.6 and published it on NuGet. I hope you will join me with your feedback and your suggests  

To install Mock Data Generator, run the following command in the Package Manager Console

PM> Install-Package MockData

  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Antonio Ripa
Architect
Switzerland Switzerland
I really like coding. And i really like improve my knowledge about design pattern and new architecture. I started at 13 just thirty one years ago ( self taught ) when I bought my first Sinclair ZX Spectrum. Nowadays I am a Senior Software ( Agile ) Engineer with more than 18 years of experience in Enterprise Application design and development, both back-end and front-end.
I am particularly interested in Enterprise Design Pattern, Domain Driven Design, Test Driven Design and Scrum Methodology, looking for new patterns in software development
I do not want to keep my learnings to myself, so I had joined code project, and adding few more learnings which may benefit many other software engineers and developers working in this lovely industry. I'm not going to dictate any of the points, but all the practices listed here contributed a lot in my software development career, so if you think they make some sense for you then try to adopt few. If you have any +/- comments, kindly feel free to write me back
Group type: Organisation

4 members

Follow on   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 [modified] PinmemberLoic URIEN3-Jan-14 12:36 
GeneralRe: My vote of 5 PinmemberAntonio Ripa3-Jan-14 12:50 
GeneralMy vote of 5 Pinmemberfredatcodeproject31-Dec-13 11:53 
GeneralMy vote of 1 Pinmemberzfu30-Dec-13 8:04 
SuggestionRe: My vote of 1 PinmvpMika Wendelius30-Dec-13 11:59 
GeneralRe: My vote of 1 Pinmemberzfu30-Dec-13 12:02 
GeneralRe: My vote of 1 PinmvpMika Wendelius30-Dec-13 12:07 
AnswerRe: My vote of 1 PinmemberAntonio Ripa30-Dec-13 12:30 
GeneralRe: My vote of 1 PinmemberAntonio Ripa30-Dec-13 23:52 
GeneralMy vote of 5 Pinmemberp4koo28-Dec-13 16:28 
GeneralRe: My vote of 5 PinmemberAntonio Ripa29-Dec-13 3:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150301.1 | Last Updated 30 Dec 2013
Article Copyright 2013 by Antonio Ripa
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid