Populating test data using C# for realistic unit and integration testing
Is there any framework for .NET to populate test data? Yes here’s one I made earlier — Faker C# for generating fake data such as names, addresses, emails and the classic lorem ipsum placeholder text.
.NET Test-driven development Behavior-driven development
Faker C# is a port of the Ruby gem of the same name, which itself is a port of Perl’s Data::Faker library. It allows you to easily generate fake data: names, addresses, phone numbers, emails and lorem ipsum placeholder text.
Useful for populating seed data for local development and within unit tests. I’ve also used it to good effect within BDD-style tests when driving a web browser (e.g. Given a user account
creates a user with a Faker generated name, username and email address).
Getting started
Pull the code via git:
git clone git://github.com/slashdotdash/faker-cs.git
Or just grab the pre-built assembly Download »
Dependencies
- Requires the .NET framework 3.5 or later
Usage
-
Add a reference to
Faker.dll
in you project within Visual Studio. -
Start using the Faker methods to generate your random test data.
Names, phone numbers and emails
var name = Faker.Name.FullName(); // "Alene Hayes"
Faker.Internet.Email(name); // "alene_hayes@hartmann.co.uk"
Faker.Internet.UserName(name); // "alene.hayes"
Faker.Internet.Email(); // "morris@friesen.us"
Faker.Internet.FreeEmail(); // "houston_purdy@yahoo.com"
Faker.Internet.DomainName(); // "larkinhirthe.com"
Faker.Phone.Number(); // "(033)216-0058 x0344"
Addresses
Faker.Address.StreetAddress(); // "52613 Turcotte Lock"
Faker.Address.SecondaryAddress(); // "Suite 656"
Faker.Address.City(); // "South Wavaside"
Faker.Address.UkCounty(); // "West Glamorgan"
Faker.Address.UkPostCode().ToUpper(); // "BQ7 3AM"
Faker.Address.UsState(); // "Tennessee"
Faker.Address.ZipCode(); // "66363-7828"
Lorem Ipsum sentences and paragraphs
Faker.Lorem.Sentence(); // "Voluptatem repudiandae necessitatibus assumenda dolor illo maiores in."
Faker.Lorem.Paragraph(); /* "Rerum dolor cumque cum animi consequatur praesentium. Enim quia quia modi est ut. Dolores qui debitis qui perspiciatis autem quas. Expedita distinctio earum aut. Delectus assumenda rerum quibusdam harum iusto." */
Buzzword bingo
Last, but not least, you can generate company names, catchphrases and bs!
Faker.Company.Name(); // "Dickens Group"
Faker.Company.CatchPhrase(); // "User-centric neutral internet solution"
Faker.Company.BS(); // "transition proactive solutions"
Faker also supports i18n thanks to Patrick Drechsler who added the multi-lingual support and German translations.