Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Test Data in Code

Tags:

c#

I am writing an exporting function to take data from a database table and export to text. Before I plug the code into the rest of the app I would like to test the export based on random data created in c#. I have found many examples in SO about creating test data in a database but none that was created directly in code. Does anybody have an example or know of a link to one?

Thanks

like image 936
Dan R. Avatar asked Feb 22 '10 19:02

Dan R.


People also ask

How do you create test data?

How to Create Test Data? Test data can be created: Manually: This is the most common way and mostly created in an excel sheet, where testers need to consider test Scenarios and test conditions and use the combinations correctly. Word files, Text Files, XML files can also be used to create test data.

What is test data in programming?

Test data is data that is used to test whether or not a program is functioning correctly. The test data is input, the program is processed and the output is confirmed. Whenever possible, test data should cover a range of possible and impossible inputs , each designed to prove a program works or to highlight any flaws.


1 Answers

You can use AutoFixture to generate pseudo-random data directly from C# code.

It is a convention-based and extensible library that uses reflection to populate objects with data.

It can be used as simply as this:

Fixture fixture = new Fixture();
MyClass mc = fixture.CreateAnonymous<MyClass>();

After the second line of code, the mc instance will be populated with all appropiate constructor parameters, and all writable properties will have been assigned.

It supports nested hierachies, non-default constructors, mapping from interfaces to concrete types and many other things.

You can customize it (almost) to your heart's content as well.

like image 180
Mark Seemann Avatar answered Oct 16 '22 16:10

Mark Seemann