Click here to Skip to main content
15,885,985 members
Articles / Programming Languages / C#
Tip/Trick

Creating mock data for software testing

Rate me:
Please Sign up or sign in to vote.
4.73/5 (11 votes)
30 Dec 2013CPOL4 min read 43.5K   328   13   13
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: 

C#
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 :

C#
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 :

C#
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 Product<vars>Repository : IProductRepository</vars><vars>, 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)


Written By
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
This is a Collaborative Group

10 members

Comments and Discussions

 
GeneralReally good stuff Pin
Sathiamurthi22-Jun-16 15:51
Sathiamurthi22-Jun-16 15:51 
QuestionSource code available? Pin
AdolfoPerez27-Apr-15 9:16
AdolfoPerez27-Apr-15 9:16 
Hi Antonio,

Are you sharing the source code? I wanted to see how you implemented the solution
and see what can be improved.

Thanks,
GeneralMy vote of 5 Pin
Loic URIEN3-Jan-14 11:36
Loic URIEN3-Jan-14 11:36 
GeneralRe: My vote of 5 Pin
Antonio Ripa3-Jan-14 11:50
professionalAntonio Ripa3-Jan-14 11:50 
GeneralMy vote of 5 Pin
fredatcodeproject31-Dec-13 10:53
professionalfredatcodeproject31-Dec-13 10:53 
GeneralMy vote of 1 Pin
zfu30-Dec-13 7:04
zfu30-Dec-13 7:04 
SuggestionRe: My vote of 1 Pin
Wendelius30-Dec-13 10:59
mentorWendelius30-Dec-13 10:59 
GeneralRe: My vote of 1 Pin
zfu30-Dec-13 11:02
zfu30-Dec-13 11:02 
GeneralRe: My vote of 1 Pin
Wendelius30-Dec-13 11:07
mentorWendelius30-Dec-13 11:07 
AnswerRe: My vote of 1 Pin
Antonio Ripa30-Dec-13 11:30
professionalAntonio Ripa30-Dec-13 11:30 
GeneralRe: My vote of 1 Pin
Antonio Ripa30-Dec-13 22:52
professionalAntonio Ripa30-Dec-13 22:52 
GeneralMy vote of 5 Pin
Pakosh28-Dec-13 15:28
Pakosh28-Dec-13 15:28 
GeneralRe: My vote of 5 Pin
Antonio Ripa29-Dec-13 2:07
professionalAntonio Ripa29-Dec-13 2:07 

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

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