Unit Testing – Should Tests Use Project or Same Namespace?

cproject-structuretddunit testingvisual studio

Background

I am working on a project with C# .NET, and I've just added a new unit test project to my solution in Visual Studio. The way that I always have been doing this is:

  1. Create a new unit test project.
  2. Have that project include a reference to the project under test.
  3. Just include (using) the project.

I guess the other way you could do it would be…

  1. Create a new unit test project.
  2. Have that project include a reference to the project under test.
  3. Make the unit test project share a namespace with the project under test.

Question

Is there an accepted way of doing this for projects in the .NET world, or, is this just an opinion and there is nothing more to it?

Best Answer

Your unit tests are in a separate project and serve a separate function from your main code, so putting them into a separate namespace makes the most sense to me.

If you're considering putting them into the same namespace just to save the using line, then don't. Less code is good, clearer code is better.