5

I am setting up some unit tests for a C# project I'm working on, and I opted to use Visual Studio's built-in unit test project. The problem is that I have been giving most of my classes in the project the default, internal access level. Now they can't be reached by my unit test project because it is a different assembly.

It would be trivial to just make all my classes in the project public so that the unit test project can access them, but isn't it idiomatic to keep classes which are only used internally by a project internal?

Matt Kline
  • 10,149
  • 7
  • 50
  • 87
  • 2
    Take a look here http://stackoverflow.com/questions/7580710/is-it-considered-bad-practice-to-use-internalsvisibleto-for-unit-test-code – SWeko Jan 13 '14 at 16:16

2 Answers2

4

you can make your internals visible to the test project - see here for more info

NDJ
  • 5,189
  • 1
  • 18
  • 27
0

There is built-in support for that, it's called private accessors. Read about here for example: http://msdn.microsoft.com/en-us/library/bb385974%28v=vs.100%29.aspx

By the way, please consider whether you really want to unit test the internals of a project. According to some schools, you shouldn't do that, but I understand that it can be useful.

fejesjoco
  • 11,763
  • 3
  • 35
  • 65
  • That article seems seriously outdated - it was last updated for VS 2005, the menu entry it mentions doesn't seem to exist in VS2012, and http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute%28v=vs.110%29.aspx seems like a more straightforward approach. – Matt Kline Jan 13 '14 at 16:22
  • I already updated my link to VS2010. But you never mentioned your VS version. You're right, it seems they are in fact deprecated. The other solution will work fine. – fejesjoco Jan 13 '14 at 16:24