AutoPoco, create meaningful test data based on your domain objects. (.NET)
Tags: dotnet, csharp, testing
Published: 2020-11-05 10:33:45

---
title: AutoPoco, create meaningful test data based on your domain objects. (.NET)
published: true
date: 2011-03-19 07:00:00 UTC
tags: dotnet,csharp,testing
canonical_url: 
---

_Creating test data for your applications is usually a cumbersome and not too inspiring exercise, to say the least. Lately I discovered a nice tool called_ [_AutoPoco_](http://autopoco.codeplex.com/) _which is created by_ [_Rob Ashton_](http://codeofrob.com/)_. AutoPoco enables you to generate meaningful test data based on your domain objects (poco’s). AutoPoco is able to generate data for individual unit tests, or you can use AutoPoco to generate large amounts of test data to populate your database._

In this article I’ll give a short introduction to AutoPoco and explain how to customize AutoPoco to enable the generation of test data which is meaningful to your project and not just some randomly generated values.

I also created a [sample app](https://github.com/robinvanderknaap/AutoPoco.Datasources) hosted on github, which contains some basic usage stuff and some custom datasources I wrote. A live version of the sample app is located [here](http://autopocodatasources.skaele.nl/)

**Notice:** Source code in this article is based on the latest version of AutoPoco residing in the trunk. The trunk contains some breaking changes with the official v1.0 release and because of that the source code used in this article will not work with the v1 release. The trunk is located [here](http://autopoco.codeplex.com/SourceControl/list/changesets). You can also use the autopoco.dll which is included in the sample app, if you don’t feel like compiling the dll yourself. I will update this article and the sample app when Rob decides to release the next version of AutoPoco.

### How does it work?

Basically AutoPoco examines your domain objects using reflection and assigns datasources to each property/field of your domain object. The assignment can be done automagically (convention over configuration), or you can assign datasources yourself. When generating test data each datasource creates meaningful test data to populate the assigned property. Here’s a quick example:

First, create a domain object for which test data will be generated:

{% gist https://gist.github.com/robinvanderknaap/a062d953680b5e27787b3b25f9dda6fe %}

Setup an AutoPoco factory and assign datasources to the properties of the domain object:

{% gist https://gist.github.com/robinvanderknaap/b298040e2a48964695dd5cee277ad11c %}

Create an AutoPoco session:

var pocoSession = pocoFactory.CreateSession();

Tell AutoPoco to generate 10 users:

var users = pocoSession.List<User>(10).Get();

And here’s the result:

![](https://cdn-images-1.medium.com/max/357/0*87bRvSb35ifonwz-.png)

AutoPoco is also able to set fixed values for certain properties and allows for invocation of methods on the domain objects. More information on installing and configuring AutoPoco can be found at [http://autopoco.codeplex.com/documentation](http://autopoco.codeplex.com/documentation)

### Roll your own datasources

AutoPoco has some built-in datasources, like Email, Firstname, Lastname etc. In a real world scenario the built-in datasources won’t get you far. Luckily it’s easy to create your own datasources for AutoPoco.  
The easiest way to create a custom datasource is to create a class that inherits from the DatasourceBase class:

{% gist https://gist.github.com/robinvanderknaap/814a785995519e12c44e23e2a16c0c77 %}

As you can see, there’s only one method you have to implement, the Next method. The DatasourceBase class uses generics to determine the returntype, in this case a string, but this can just as easy be any other object. As an example I will create a datasource which provides company names:

{% gist https://gist.github.com/robinvanderknaap/23102d04877f60f2b73dc98113e2ae75 %}

Once created this datasource can be used in the same way as the built-in datasources. Extending our previous example, the configuration of AutoPoco would look like this using the company datasource:

{% gist https://gist.github.com/robinvanderknaap/5ba40a9ced0147f9dbcee0419792d12f %}

Which results in:

![](https://cdn-images-1.medium.com/max/479/0*G2OExjft1PQIBnco.png)

### Concluding

It’s very easy to generate test data with AutoPoco, and what I like most, you can give meaning to your test data by creating your own datasources.

I created a [little project](https://github.com/robinvanderknaap/AutoPoco.Datasources) on github to host some of the custom datasources I wrote. The project includes an asp.net mvc example using some of the built-in and all of the custom datasources I use to generate data. The output of the example project can be found [here](http://autopocodatasources.skaele.nl/). Feel free to use the datasources anyway you like. Included datasources are:

- EnumSource
- BooleanSource
- CitySource
- CompanySource
- DateTimeSource
- DecimalSource
- DoubleSource
- DutchCitySource
- DutchMiddlenameSource
- DutchMobileSource
- DutchPostalSource
- DutchTelephoneSource
- ExtendedEmailAddressSource
- FloatSource
- IntegerSource
- PostalSource
- StreetSource
- TimeSpanSource
- UrlSource

Copyright © 2010 Webpirates. All source code used in this article is licensed under the Lesser General Public License.