Geeks With Blogs


Microsoft Store

Support This Site

AddThis Social Bookmark Button

Locations of visitors to this page

Subscribers to this feed

TwitterCounter for @sdorman

Creative Commons License

Scott Dorman Microsoft MVP, Software Architect, Developer, Author

This question is for all of the TDD and unit test folks, so I’m hoping someone comes up with an answer.

I’ve been writing unit tests and analyzing code coverage for one of my libraries for about a week now and I’m starting to see the end, at least for this particular library. However, I’ve run in to a problem trying to ensure as close to 100% code coverage as possible. This exists in several methods in a few different classes, but I’ll take the simplest one for illustration purposes.

I have cod that looks like this:

   1: public static ProcessorArchitecture GetProcessorArchitecture()
   2: {
   3:     SYSTEM_INFO sysInfo = new SYSTEM_INFO();
   5:     if (Environment.OSVersion.Version.Major > 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1))
   6:     {
   7:         SafeNativeMethods.GetNativeSystemInfo(out sysInfo);
   8:     }
   9:     else
  10:     {
  11:         SafeNativeMethods.GetSystemInfo(out sysInfo);
  12:     }
  14:     ProcessorArchitecture architecture = ProcessorArchitecture.Unknown;
  15:     switch (sysInfo.uProcessorInfo.processorArchitecture.wProcessorArchitecture)
  16:     {
  17:         case Constants.PROCESSOR_ARCHITECTURE_AMD64:
  18:             architecture = ProcessorArchitecture.X64;
  19:             break;
  21:         case Constants.PROCESSOR_ARCHITECTURE_IA64:
  22:             architecture = ProcessorArchitecture.Itanium;
  23:             break;
  25:         case Constants.PROCESSOR_ARCHITECTURE_INTEL:
  26:             architecture = ProcessorArchitecture.X86;
  27:             break;
  28:     }
  30:     return architecture;
  31: }

My unit test for this particular method is relatively simple as well:

   1: [TestMethod]
   2: public void GetProcessorArchitecture()
   3: {
   4:     ProcessorArchitecture expected = ProcessorArchitecture.X86;
   5:     ProcessorArchitecture actual = ExtendedEnvironment.GetProcessorArchitecture();
   6:     Assert.IsTrue(actual == expected);
   7: }

The problem here is that not all of this method is able to covered. Specifically, the else clause and the first two cases in the switch. Short of physically changing hardware or operating systems, how do I actually unit test those conditions? I don’t think creating a mock object will help in this case, as the object I would need to mock is the System.Environment object and then somehow be able to alter the processor architecture field of the SYSTEM_INFO struct that is returned from GetSystemInfo().

I realize this is a somewhat trivial example, but I think it does clearly demonstrate the impossibility of achieving 100% code coverage through unit tests.

DotNetKicks Image
Posted on Saturday, January 17, 2009 10:35 PM .NET (General) | Back to top

Copyright © Scott Dorman | Powered by: