<p>How do you usually go about <strong>separating your codebase and associated unit tests</strong>? 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.</p>

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

<p>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.</p>

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

[{'answer_id': 81790, 'author': 'Galwegian', 'author_id': 3201, 'author_profile': 'https://Stackoverflow.com/users/3201', 'pm_score': 6, 'selected': True, 'text': '<p>I definitely advocate separating your tests out to a separate project. It\'s the only way to go in my opinion.</p>\n\n<p>Yes, as <a href="https://stackoverflow.com/users/6369/garry-shutler">Gary</a> says, it also forces you to test behavior through public methods rather than playing about with the innards of your classes</p>\n'}, {'answer_id': 81794, 'author': 'Chris Canal', 'author_id': 5802, 'author_profile': 'https://Stackoverflow.com/users/5802', 'pm_score': 1, 'selected': False, 'text': "<p>I've always keep my unit tests in a seperate project so it compiles to it's own assembly.</p>\n"}, {'answer_id': 81814, 'author': 'Anthony', 'author_id': 5599, 'author_profile': 'https://Stackoverflow.com/users/5599', 'pm_score': 1, 'selected': False, 'text': '<p>For each project there is a corresponding .Test project that contains tests on it.</p>\n\n<p>E.g. for the assembly called, say "Acme.BillingSystem.Utils", there would be a test assembly called "Acme.BillingSystem.Utils.Test".</p>\n\n<p>Exclude it from the shipping version of your product by not shipping that dll.</p>\n'}, {'answer_id': 81854, 'author': 'Rasmus Faber', 'author_id': 5542, 'author_profile': 'https://Stackoverflow.com/users/5542', 'pm_score': 2, 'selected': False, 'text': '<p>I would recommend a separate project for unit tests (and yet more projects for integration tests, functional tests etc.). I have tried mixing code and tests in the same project and found it much less maintainable than separating them into separate projects.</p>\n\n<p>Maintaining parallel namespaces and using a sensible naming convention for tests (eg. MyClass and MyClassTest) will help you keeping the codebase maintainable.</p>\n'}, {'answer_id': 81880, 'author': 'Rikalous', 'author_id': 4271, 'author_profile': 'https://Stackoverflow.com/users/4271', 'pm_score': 3, 'selected': False, 'text': '<p>The .Net framework after v2 has a useful feature where you can mark an assembly with the InternalsVisibleTo attribute that allows the assembly to be accessed by another.<br>\nA sort of assembly tunnelling feature.</p>\n'}, {'answer_id': 81881, 'author': 'Morten Christiansen', 'author_id': 4055, 'author_profile': 'https://Stackoverflow.com/users/4055', 'pm_score': 4, 'selected': False, 'text': '<p>As the others point out, a seperate test project (for each normal project) is a good way to do it. I usually mirror the namespaces and create a test class for each normal class with \'test\' appended to the name. This is supported directly in the IDE if you have Visual Studio Team System which can automatically generate test classes and methods in another project.</p>\n\n<p>One thing to remember if you want to test classes and methods with the \'internal\' accessor is to add the following line to the AssemblyInfo.cs file for each project to be tested:</p>\n\n<pre><code>[assembly: InternalsVisibleTo("UnitTestProjectName")]\n</code></pre>\n'}, {'answer_id': 81922, 'author': 'Michael Meadows', 'author_id': 7643, 'author_profile': 'https://Stackoverflow.com/users/7643', 'pm_score': 0, 'selected': False, 'text': '<p>I definitely agree with everyone else that you should separate the tests from your production code.  If you insist on not, however, you should define a conditional comiplation constant called TEST, and wrap all of your unit test classes with a </p>\n\n<pre><code>#if TEST\n#endif\n</code></pre>\n\n<p>first to ensure that the test code does not compile in a production scenario.  Once that is done, you should either be able to exclude the test dlls from your production deployment, or even better (but higher maintenance), create an NAnt or MSBuild for production that compiles without the references to the test dlls.</p>\n'}, {'answer_id': 81937, 'author': 'tsilb', 'author_id': 11112, 'author_profile': 'https://Stackoverflow.com/users/11112', 'pm_score': 2, 'selected': False, 'text': "<p>As long as your tests are in a seperate project, the tests can reference the codebase, but the codebase never has to reference the tests. I have to ask, what's confusing about maintaining two projects?  You can keep them in the same solution for organization.</p>\n\n<p>The complicated part, of course, is when the business has 55 projects in the solution and 60% of them are tests. Count yourself lucky.</p>\n"}, {'answer_id': 82126, 'author': 'Hamish Smith', 'author_id': 15572, 'author_profile': 'https://Stackoverflow.com/users/15572', 'pm_score': 0, 'selected': False, 'text': "<p>I always create a separate Acme.Stuff.Test project that is compiled separately.</p>\n\n<p>The converse argument is: Why do you want to take the tests out? Why not deliver the test? If you deliver the tests along with a test runner you have some level of acceptance test and self test delivered with the product. </p>\n\n<p>I've heard this argument a few times and thought about it but I personally still keep tests in a separate project.</p>\n"}, {'answer_id': 82196, 'author': 'Niklas Winde', 'author_id': 9077, 'author_profile': 'https://Stackoverflow.com/users/9077', 'pm_score': 2, 'selected': False, 'text': "<p>I put the tests in a separate project but in the same solution. Granted, in big solutions there might be a lot of projects but the solution explorer is good enough on separating them and if you give everything reasonable names I don't really think it's an issue.</p>\n"}, {'answer_id': 121090, 'author': 'user21114', 'author_id': 21114, 'author_profile': 'https://Stackoverflow.com/users/21114', 'pm_score': 3, 'selected': False, 'text': '<p>Yet another alternative to using compiler directives within a file or creating a separate project is merely to create additional .cs files in your project.</p>\n\n<p>With some magic in the project file itself, you can dictate that:</p>\n\n<ol>\n<li>nunit.framework DLLs are only referenced in a debug build, and</li>\n<li>your test files are only included in debug builds</li>\n</ol>\n\n<p>Example .csproj excerpt:</p>\n\n<pre><code>&lt;Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5"&gt;\n\n   ...\n\n   &lt;Reference Include="nunit.framework" Condition=" \'$(Configuration)\'==\'Debug\' "&gt;\n      &lt;SpecificVersion&gt;False&lt;/SpecificVersion&gt;\n      &lt;HintPath&gt;..\\..\\debug\\nunit.framework.dll&lt;/HintPath&gt;\n   &lt;/Reference&gt;\n\n   ...\n\n   &lt;Compile Include="Test\\ClassTest.cs" Condition=" \'$(Configuration)\'==\'Debug\' " /&gt;\n\n   ...\n&lt;/Project&gt;\n</code></pre>\n'}, {'answer_id': 744230, 'author': 'Kenneth Cochran', 'author_id': 71200, 'author_profile': 'https://Stackoverflow.com/users/71200', 'pm_score': 2, 'selected': False, 'text': '<p>One thing yet to be considered is versions of VisualStudio prior to 2005 did not allow EXE assembly projects to be referenced from other projects. So if you are working on a legacy project in VS.NET your options would be:</p>\n\n<ul>\n<li>Put unit tests in the same project and use conditional compilation to exclude them from release builds.</li>\n<li>Move everything to dll assemblies so your exe is just an entry point.</li>\n<li>Circumvent the IDE by hacking the project file in a text editor.</li>\n</ul>\n\n<p>Of the three conditional compilation is the least error prone.</p>\n'}, {'answer_id': 1098713, 'author': 'mbowles', 'author_id': 1112, 'author_profile': 'https://Stackoverflow.com/users/1112', 'pm_score': 1, 'selected': False, 'text': '<p>If the <strong>#if(DEBUG)</strong> tag allows for a clean "release" version, why would you need a separate project for tests.  The nunit LibarryA/B example (yeah, I know its a example) does this.  Currently wrestling with the scenario.  Had been using a separate project, but this seems to possibly allow for some productivity improvements.  Still hummin and hawin.</p>\n'}]
['https://Stackoverflow.com/questions/81784', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/15497/']