Visual Studio Code Coverage: Value does not fall within the expected range

While attempting to improve my code coverage in Visual Studio 2014, I suddenly found myself getting the error “Value does not fall within the expected range” when running unit tests with code coverage enabled. Without code coverage, the tests ran fine. With code coverage, that error would appear twice in the test output.

I googled the error for awhile and mostly came up empty. I eventually tracked down this post where a user was getting the same message but related to website profiling. It did help me track down the issue though.

The solution

I looked in the configuration manager. It turns out that while most of the projects in my solution were set to the “Any CPU” platform, I had one Wix installer project set to “x86”, presumably because it’s building an x86-compatible installer. If this project is disabled, code coverage works. If re-enabled, I get the error. For now, I’ve just been disabling the program when I want to do code coverage.

Test utilities

I put some MSTest-based unit testing utilities up on github.  So far I have FileAssert and DirectoryAssert classes that compare the contents of, as you would expect, files and directories. I do a lot of code generation and so it’s useful to be able to compare files and directories easily.  I did some extra work so that when files and directories are different, the message that comes back is helpful and identifies the lines and character positions that differ.  This is something you don’t get when simply comparing strings.