Saturday, February 10, 2007

Unit Testing Internal Types

Problem: .NET classes of 'internal' visibility to be unit tested (in this case, using NUnit).

Complication: These classes are part of a 'provider' model (pluggable components instantiated via a Factory Method implementation, based on configuration settings). Access to the functionality to be tested would be via a Facade class, which looks up configuration and instantiates the relevant type using Reflection.

Known Solutions:
  • Use of Friend assemblies - however, to maintain security (ensuring only our Unit Test assembly invokes the internal types) we would need to strong name our assembly under test. This has a cascading effect, as all its references then need to be strong named. Not an option, when you consider that we might have a reference on an external vendor's component somewhere down the dependency chain.
  • Writing the Unit Tests in the same assembly - which clutters up the codebase goodstyle.
  • Write Reflection based wrappers (a la VSTS) - which the Professional edition of VS2005 does not have.

Yet another solution?

I'm sure there is a downside to the following approach, which I'm going to find out about the hard way - but till then:

The complication states that the required values need to be pulled from configuration. What price placing the default configuration file in the execution path during the method marked with [TestFixtureSetUp], to be overwritten at the beginning of the individual [Test] method by a separate configuration file containing the relevant settings, methinks... all this is very much guesswork, depending on NUnit allowing the configuration file to be overwritten.

I'm thinking a feature in NUnit like the VSTS [DeploymentItem] attribute would have been very helpful in this scenario...

Will blog about whether this works... if it works!

No comments: