Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to write unit test code?

I thought it will be a common question so I searched for a while but couldn't find it.

I am about to start a new project (C#, .net 3.5) and I was thinking about where I should I write the unit test code. I can create a unit test project and write all code there, or I can write the unit test code with the "class under test" itself.

What do you recommend and why? Things to consider before choosing an approach (caveats?)?

EDIT: About writing unit-test code with "code under test": Removing the test code from production assembly isn't difficult I guess. Thats what conditional compilation is for. Right?

Just throwing this point because answers are rejecting the second option just because production assemblies would be fatty.

like image 782
Hemant Avatar asked Sep 23 '09 08:09

Hemant


2 Answers

Separate project, same solution. Use InternalsVisibleTo if you want to access internals from the test code.

Separating out test from production code:

  • makes it more obvious what's what
  • means you don't need dependencies on test frameworks in your production project
  • keeps your deployed code leaner
  • avoids including test data files in your deployment assembly

Keeping the code in the same solution:

  • keeps the test cycle quick
  • makes it easy to hop between production and test code
like image 53
Jon Skeet Avatar answered Oct 13 '22 12:10

Jon Skeet


I always create a separate project in where I write my TestFixtures. I do not want to litter my domain model (or whatever) with Test classes.
I do not want to distribute my tests to customers or users of my application, so therefore I put them in a separate project (which also leads to a separate assembly).

If you have the rare case that you want to test internal methods, you can use InternalsVisibleTo. If you have the very rare case that you want to test private methods, you can use this technique, as explained by Davy Brion.

like image 36
Frederik Gheysels Avatar answered Oct 13 '22 12:10

Frederik Gheysels