Unit Testing Internal Classes

So I recently got my Scrum Master Certification and as a result I have felt an obligation to begin practicing what I now preach. So part of this “transition” in my developer life has been to start forcing myself to just commit to doing test driven development (TDD) for everything I develop. Its been a transition that I knew I would eventually have to make but have been very reluctant because its so different to what I’m used to. Now that I have started doing it, I feel quite stupid I didn’t do it earlier, it just makes so much sense on so many different levels.

Anyway the point of this post is not to gush about how great TDD is, what I wanted to do was just document one of the learnings I had recently where I had a class that I had made internal. Obviously the unit tests all sit in a different project in a different assembly and I needed the unit tests to get access to my class. I found this post where someone had put in a great answer but for whatever reason it wasn’t chosen as the top answer so I thought I may just let everyone know that what you want to do is use the following code:

using System.Runtime.CompilerServices;


Place this in the “AssemblyInfo.cs” file under the “Properties” folder in your project. Replace the “UnitTestsAssemblyName” with the name of the assembly where your unit tests are stored and after a re-compile of your assembly you’ll have access to all the internal classes.

One of the arguments on the post was that in TDD the tests really should be testing user-centric features and those features really should be public. I personally disagree to this statement for the fact that the user-centric feature really has no bearing on the structure of your classes and assemblies so long as the feature is implemented, as requested by the user. Just because a class doesn’t directly implement a user-centric feature doesn’t mean you don’t want to perform thorough testing on it to make sure that any changes to this class don’t impact the stability of the entire project.

In the current example I’m working on, I have a helper class that performs some XML de-serialisation tasks. I don’t feel that there is a need for these classes to be viewed by anything outside of my assembly but if I need to change the checks that are done during XML de-serialization, I want to have some level of assurance that I have the basic checks and balances in-place that will quickly pick-up any obvious mistakes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s