Unit testing non-public types and members for .NET projects

Unit testing is usually used for testing public types and members. However, there are cases when you might need to test types or members that are not public. These could be internal classes or private helper methods, for instance. Whether that is proper unit testing or beyond its scope is not a discussion that I want to get into here. However, in this post, I will show how you can unit test non-public types and members from .NET assemblies.

When faced with the need for testing non-public types and members you can use several approaches:

  • change the accessibility to public; you can do that perhaps only for debug builds and keep the intended accessibility in release builds by using conditional compilation.
  • provide public members of a class that invoke private ones;
  • use reflection.

The first solutions involve changing the API only for the sake of the testing. The last solution avoids that but requires more work. To help with that, the Visual Studio unit testing framework provides some helper types that enable you to focus on the actual testing and be less concerned about the reflection details.

Unit-testing internals

Let us assume we have the following internal class in a project called demolib:

Both the InternalClass class and its Add method are internal. Therefore they are not accessible from another assembly, so the following code cannot work:

This can be fixed, however, with a single line of code, added to the assembly that is being tested (not the testing assembly). You can put it into the AssemblyInfo.cs file:

And with that, the above test can be compiled and executed successfully.

However, things are a bit different if the assembly that is being tested is a strong-named signed assembly. In this case the following error is triggered on the InternalsVisibleTo attribute:

Friend assembly reference ‘demotest’ is invalid. Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations.

To solve this, you must:

  • sign the testing assembly too, and
  • specify the public key from the file that was used to sign the testing assembly.

To fetch the public key, you must run the following commands (this examples assume the key file used to sign the testing assembly is called demokey.snk):

The result is as follows:

With this information, you must change the InternalsVisibleTo declaration as follows:

Unit-testing privates

Let’s change the InternalClass above so that instead of being internal it is public but its members are protected and private. The solution shown earlier no longer works and we are back to the same error with members not being accessible due to their protection level.

The Visual Studio unit testing framework provides a class called PrivateObject that helps invoking non-public non-static members through reflection.

The unit testing of the PrivateClass using the PrivateObject helper would look as follows:

However, PrivateObject cannot be used for invoking static members. Another helper, called PrivateType, is provided and must be used for this purpose.

Suppose the PrivateClass type should change as shown below:

In this case, testing the Add() method must be done as follows:

Additional readings

For more information see the following articles:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.