Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Excluding unit tests from the release version of your project

Tags:

How do you usually go about separating your codebase and associated unit tests? I know people who create a separate project for unit tests, which I personally find confusing and difficult to maintain. On the other hand, if you mix up code and its tests in a single project, you end up with binaries related to your unit test framework (be it NUnit, MbUnit or whatever else) and your own binaries side by side.

This is fine for debugging, but once I build a release version, I really do not want my code to reference the unit testing framework any more.

One solution I found is to enclose all your unit tests within #if DEBUG -- #endif directives: when no code references an unit testing assembly, the compiler is clever enough to omit the reference in the compiled code.

Are there any other (possibly more comfortable) options to achieve a similar goal?

like image 460
petr k. Avatar asked Sep 17 '08 10:09

petr k.


1 Answers

I definitely advocate separating your tests out to a separate project. It's the only way to go in my opinion.

Yes, as Gary says, it also forces you to test behavior through public methods rather than playing about with the innards of your classes

like image 116
Galwegian Avatar answered Oct 05 '22 12:10

Galwegian