Add Unit Tests to an existing project

Having picked up several existing projects in the last few months I have been tasked with bug fixes and updates. None of these projects have any kind of testing carried out on them other than the boss sitting down and clicking buttons for an afternoon, if it doesn’t crash then it’s considered ready for release. To try and improve this situation I have taken it upon myself to add some unit tests to each project as I work on them, this way I can verify bug-fixes and reference tests that have been written in the bug-tracker we use. Over time this approach will hopefully lead to better regression testing and open the door to add more and more tests to the projects as time allows.

So basically this is the procedure I have used to add MbUnit tests to existing projects, I am certain it is not the only way to do it, and probably not the best of most efficient way either. It did, however, work for me:

Firstly install TestDriven.Net, Moq and latest MbUnit. TestDriven.Net does come with a version of MbUnit, but it isn’t the latest, so once TD.NET is installed install the newer version of MbUnit. TD.NET also requires a license if you are using it for commercial purposes to make sure you pay up if you need to.

So my original “non-testable” project looks like this

The next thing to do is to create an MbUnit Test project; File->New->Project. Select Test from either C# or VB depending on your preference and add it to the existing solution:

This will create an empty test project with all the references required to get it to run. However, as I explained in my last post, it is better to have references that are required for the build locally in the project and under Version Control. Following the tests from the previous posts I moved the MbUnit and Moq DLL files into a local folder and added them to the project (Moq is for a Mocking Framework that I will get onto in future posts). I also added the references that are required to the other projects within the solution so that I can actually access them for testing:

I now need to add a file to the existing project that I will be testing, this is called TestFriends.vb (as it is in a VB project), it simply contains the following line:

This allows the project FundManagerUnitTests access to internal methods of the assembly.

The next thing to be created is the Test Fixture, basically the file that will hold the tests that I will be running:

Now I need something to test, as a quick example I created the following simple method that takes 2 numbers and returns the sum:

The following test will check that the function returns 3 when adding 1 and 2, I could input the values as parameters and take advantage of that feature of MbUnit, but for speed I just hard-coded them:

Finally run the tests in MbUnit, this can be done either just by running the tests within Visual Studio using Test Driven.Net or as I show here using the MbUnit GUI:

If the tests pass we should see some green:

Now that I know that all the relevant bits are in place and working I can go ahead and write tests for the bug fixes and additional funcitonality that is needed in the app.