views:

307

answers:

3

I would like to know how to implement unit testing in an existing (quite large) application using visual studio 2008 (.net 2.0).

I understand that developing unit tests for the existing/legacy code is not realistic but I would like to have tests for code moving forward.

I have found plenty of examples on how to write tests for code but nothing on how to set it up from scratch on an existing project, and how to integrate it into the development cycle.

+1  A: 

I highly recommend reading this book: Working Effectively with Legacy Code if you want to make unit test for the existing code. It's also a good book on best-practices for unit tests in general.

It is possible to do unit testing on existing projects, but you will have to make some adjustments here and there to make the code testable. Too much dependencies is often the problem.

EDIT (after your comment) If you really want to embed unit testing into your development cycle then you should go for TDD (Test Driven Development). The aim here is to write your unit tests first, so you have a good understanding of what your classes will do. Of course these tests will fail, but the target is to get them working one by one. Do a google on TDD, there's plenty of information out there.

Gerrie Schenck
Yeah I know its a little futile implementing it at this stage, but we are getting the opportunity to start again as a greenfield project.. becuase of architectural changes, I want to implement unit testing from the start and thought Id get a head start now.. (hence the question)
CodeKiwi
huh? I dont understand
CodeKiwi
Gerrie, Yes TDD is something we are investigating at the moment, part of the reason I want to put into practice a some unit testing now,
CodeKiwi
@CodeKiwi: Feathers is the author of the book I mentioned. Really, if you want to try out unit testing on existing code, then go and buy the book.
Gerrie Schenck
Oh ok, I have added it to my Amazon "to buy" list, thanks.
CodeKiwi
+4  A: 

Simple approach:

  • Pick one of unit test frameworks (Nunit, MbUnit, Xunit, VS unit test framework, ...)
  • Add project for unit tests in your solution (for example UnitTests).
  • Start writing tests.

For easier organization, create at least one namespace for each project in solution, for example UnitTest.Project1, UnitTests.Project2, ... Depending on size of projects, add more levels to namespace.

This way test project is part of solution. If you use some continuous integration tool, then tests can be automatically executed on each commit to source code repository.

Edit:
Regarding comment on MS Unit Test Framework problem with creating unit tests, there is know problem: "Create unit test" wizard does not work. It looks that there are unsupported types of projects that prevent unit test wizard from working correctly. I just tried with one solution that have one F# and several C# projects. I added unit testing project and tried to add tests. Test wizard had problems until I unloaded F# project. Then everything worked fine.

zendar
This may be a stupid question but I thought unit testing was "baked" into the VS 2008 IDE, so what do you need the unit testing frameworks for?
CodeKiwi
You are right. I forgot about that one because I am using NUnit just because it was best pick 4 years ago when I started unit testing.
zendar
I have attempted to just add unit tests by using the "create unit test" context option on when right clicking a class but was always confronted with an error (cant remember it at the moment) however I suspect it relates to visual source safe intervening.
CodeKiwi
Don't forget a mocking framework! Moq comes to mind... http://code.google.com/p/moq/
Randolpho
+3  A: 

One easy way to get tests going with existing code is to have a policy of writing a test when a bug is found.

i.e

  • Find bug
  • Write test which recplicates bug
  • Fix
qui
just out of interest, assuming you have single purpose method (or close to single purpose) how many tests do you usually have per method (ball park),
CodeKiwi
Make one test for some "average" or "standard" set of parameters and be sure to make tests for all known boundary conditions (null parameters, max/min values, ...).
zendar
+1 and write tests for any new code that goes in, not just for bugs. In time it adds up.
SnOrfus