Friday, November 18, 2011

SharpDevelop – Code Coverage Issue Involving VerificationException

I recently encountered a hard-to-debug issue with SharpDevelop that caused one of my unit tests to fail only when code coverage is enabled. The test failed with the following exception:

System.Security.VerificationException : Operation could destabilize the runtime.

The method being tested uses Microsoft.VisualBasic.TextFieldParser to parse CSV. I dug around and determined that the exception seems to be thrown within TextFieldParser’s constructor.

The Solution


The fix is easy: just add a PartCover exclusion. In SharpDevelop (4.1), this can be done by going to the Test project’s properties, clicking on ‘Code Coverage’, and adding the following to the Exclusion text box:

[Microsoft*]*

A Bit of Explanation


SharpDevelop’s preinstalled code coverage tool uses PartCover. It seems that VerificationException is thrown when PartCover attempts to track code coverage in an assembly decorated with System.Security.AllowPartiallyTrustedCallersAttribute. Apparently, the assembly where TextFieldParser is defined (Microsoft.VisualBasic.dll) is decorated with this attribute.

Resources


I learned about PartCover exceptions and default exceptions from this introduction to PartCover.

PartCover’s maintainer asked a question on StackOverflow that pointed me in the right direction when I was doing some research on why this problem occurs.