I received this in an email a while ago, but have been too busy to blog about it until now. The email:
I've recently started TDD, mostly after reading your blog. I downloaded
NUnit and have begun running a few simple tests. My question is, do you
normally include test classes and more importantly, the NUnit DLL in
your projects? I didn't think it was a good idea, not that I have any
argument for why, so I would write my class and then create a whole new
project for the test files and finally run my tests from new DLL that
VS.NET would create. Should I not be concerned about inlcuding test code
or the the reference to the NUnit DLL in my projects?
As far as I can tell, there are 4 ways to setup NUnit tests in Visual Studio .NET. And they are:
- In the same class – the advantages to this setup are you have full access to everything, including private methods and variables. However, since you have to make the test methods public, it could be a security issue. I don’t generally do this except for learning.
- Separate class, same project – here you make a new class within the same project (or folder). Thus you can access all internal methods without worry. There could still be security issues, but it is somewhat less dangerous this way. I sometimes use this for smaller projects if requirements allow.
- All tests in 1 separate project – here all of an application’s unit tests are in a separate project. This has the advantage of keeping all unit tests in one place, so it’s easy to fire up NUnit and run all the application’s tests. But I tend to need a lot of directories to organize my tests, and for larger applications the unit test project becomes huge. I use this for all but the smallest and largest projects.
- All tests for a project go in a separate project – here you have one test project per real project. For example, the BusinessServices project has a BusinessServicesTest project. This keeps the unit test projects manageable, but it takes more effort to run all the application’s unit tests at once. I generally use this for large applications.
For options 1 and 2, perhaps not deploying the NUnit dll is good for security reasons, but anyone can still call those public test methods. I’m not sure of the security implications, since I generally don’t do either of these or if I do, it’s a small local app. For options 3 and 4, it doesn’t really matter if you include the NUnit dll in the project since you won’t deploy the unit test project(s).
Which of these methods do you use and why? Are there other methods out there you are using? Am I smoking crack? Leave a comment letting me know!