NBuilder 2.1 Available

Posted on April 19, 2009. Filed under: .NET, Automated Testing, C#, Functional Testing, NBuilder, Unit Testing |

A new version of NBuilder is available.

This is a complete re-write of the original code but still has the same fluent interface with only a few slight differences.

The code base is now very robust and there are loads of new features, including hierarchy generation and persistence support.

Download the source and the binary from the NBuilder google code page

Read Full Post | Make a Comment ( 1 so far )

Introducing NBuilder

Posted on February 1, 2009. Filed under: .NET, Automated Testing, C#, Functional Testing, NBuilder, Unit Testing |

UPDATE: 06 June 2009 – This post is based on a proof of concept and as such the syntax has changed slightly. Please see the project page for up to date documentation:

When I’m writing tests I frequently need to create some quick test data either for using as the result of a mock object or perhaps for inserting into a database and then either running an automated functional test or an automated browser / acceptance test with it. I want to be able to create it in my test or in the setup for a test and I want to be able to do it very quickly.

The goal

I want to be able to say something like this:

1. Create a list of 100 categories

2. Create a list of 100 products, and give them all between 1 and 5 categories each.


Consider the following simple model:


What I usually find myself doing is having a class such as ProductBuilder and giving it static methods such as:


Perhaps also with a few ‘WithAbc(xyz)’ methods to set certain properties. However as you can imagine you soon end up with loads of builder classes and loads of methods.

I decided there had to be a better way. I had a quick search on the web but I couldn’t find anything that looked like it would do the job. I wanted something with a very fluent interface where I could almost write the code by saying something like Create 100 Products, each with a tax type of VAT, and each in some categories. I couldn’t find anything like this, so I decided to create a library myself.


I started off with a class Builder<T>Then I started by coming up with a generic way to create a list:


I thought that the properties should be set to something by default using some reflection and decided I wasn’t really too bothered about what the values of most of the properties were so i decided to to set every string to its name, and append its one-based index to it. So for example the first object’s Title would be set to Title1, the second Title2 etc, etc. All the integers and decimals etc would be set to 1,2,3,4; 1.0, 2.0, 3.0, 4.0 etc, etc. So using this you can easily make a list of products each with their basic properties populated.

However, what if you want some of them – the first 10 to have a different title? I added this where syntax to cope with that:


Ok cool – but what if you also want those same ten to have a specific value for the QuantityInStock property:


How about you want to give them a random quantity, between 1 and 2000? This is where the Generate class comes in:


How about if you want the next ten after it to have different properties still:


What about giving a random 50 the quantity of 1? (Perhaps you could test some stock alerting functionality with this for instance)


The ‘Pick’ Class

Ok you get the idea. That’s the easy kind of thing out of the way. How about relations? What if you want to add categories to your products? What if you want to add different categories and a different number of categories to each product?

I came up with the ‘Pick’ class to do this.


I think that reads pretty well, if I was coming to read someone’s test set up, I could see straight away from that what they were doing.

Now do what you want with them – return them in a mocked object or if you’re doing a functional/integration or acceptance test you could use your repository / data layer to save them to the database:


Test data inserted and it only took a few lines of simple easy to read code. Job done, now you can get on and write your tests.

Adding NBuilder to your functional test


Download the binary

Download NBuilder Binary from Google Code

Features I haven’t mentioned here

  1. The method HaveDoneToThem(Action<T>). As the name suggests this allows you to call a method on each object in the same way as you use Have(). You could use this to add a value object to each of the list elements.
  2. There is also a CreateNew() method for creating a single object.

Please bear in mind…

Please note that this is only one step up from a proof of concept at the moment so if you try it out please bear in mind:

  1. While it has been unit tested, and everything I have described here does work, it has only been tested using this syntax. For example I have no idea what would happen if you did a Have() before having done a WhereTheFirst() or WhereRandom(). You might get an exception.
  2. When the initial object generation happens, it supports most simple data types int, string, long, DateTime etc, but it doesn’t support unsigned types yet. (No particular reason other than I just haven’t bothered to add them in yet)
  3. The Generate class only support ints and decimals. And it doesn’t support decimals all that well either! You can only say generate numbers between two integers at the moment.

As I use this tool in real projects over the next few weeks I will add to it, make it more robust and do another posting and probably get the source code up very soon too.

Comments and suggestions

I’d be interested to hear any comments, suggestions or if anyone knows if there is already something out there that does what NBuilder does.

p.s. – Sorry – I couldn’t think of anything more imaginative than NBuilder!

Read Full Post | Make a Comment ( 6 so far )

Liked it here?
Why not try sites on the blogroll...