Reputation: 12834
I have a unit test project that mirrors the directory and namespace structure of my application, except that the unit test project wraps everything in a Test
namespace. So a test for this class:
MyModule.MyNamespace.MySubNamespace.MyClass
would be defined in this class:
Test.MyModule.MyNamespace.MySubNamespace.MyClass
I'm running into problems in the unit test project when I try to create instances of objects defined in the regular (non-unit test) project if an identically-named class also exists in the unit test project. Using the example above, if I create a variable of type MyClass
in my unit test project using its fully qualified type name:
MyModule.MyNamespace.MySubNamespace.MyClass myobject = new ....
the compiler assumes I'm referring to the MyClass
object defined within the Test
namespace, and doesn't treat the type name as a fully qualified name.
The easy solution is to restructure my unit testing namespaces, but I'm curious if there's a way around this. I need some way to "force" the compiler to treat the type name as fully qualified - a way to "break out" of the Test
namespace. Is there any way to do this?
If not, is there a better convention to clearly namespace my unit tests?
Upvotes: 2
Views: 111
Reputation: 12834
After some more searching, I came across the global
keyword, which tells the compiler to assume the type name is fully qualified:
global::MyModule.MyNamespace.MySubNamespace.MyClass
Here's the reference for the global
keyword on MSDN.
Upvotes: 5
Reputation: 1530
Try using an alias for the namespace. eg
using implementation = MyModule.MyNamespace.MySubNamespace;
then in the test
implementation::MyClass myClass = new...
Here's the docs about it http://msdn.microsoft.com/en-gb/library/c3ay4x3d.aspx
Upvotes: 1
Reputation: 196
I usually have something that looks like this:
MyCompany.MyProduct.MyModuleTests.MyNamespace.MySubNamespace.MyClassTest
This usually makes it pretty clear to people and also plays well with TFS builds that will try to detect unit tests in all assemblies with "Test" in the name.
If you're really attached to your naming convention, you can use an alias for a namespace at the top of the test file:
using MyRealStuff = MyModule.MyNamespace.MySubNamespace;
And used in code:
MyRealStuff.MyClass myobject = new ...
Upvotes: 0