Using ContractInvariantMethodAttribute causes missed code coverage of auto property

I am using VS 2013 Community Edition with ReSharper 9.1.1 and dotCover 3.1.1.  I am also using Microsoft Code Contracts 1.7.11202.10 (6/23/15).

In my current project, I have a struct with an int auto property named "Index" with a public getter and private setter:

public int Index { get; private set; }

In the same struct, I have a Code Contracts object invariant method that is marked with the ContractInvariantMethodAttribute.  It has two lines that use the Contract.Invariant method to test that the auto property is within an allowable range:

private void ObjectInvariant()
     Contract.Invariant(Index >= MinIndex);
     Contract.Invariant(Index <= MaxIndex);

As soon as I added the invariant method to the struct, dotCover started reporting that the auto property "Index" is not covered.  If I comment out the two lines in the object invariant method that reference "Index" dotCover shows the auto property as grayed out in the coverage analysis.

I have tried excluding the ContractInvariantMethodAttribute from coverage but that does not solve the problem.  Additionally, I have tried cleaning the Solution and rebuilding it but the error keeps occurring.

Of note, if I add a backing field with a getter only, instead of using an auto property, the coverage is normal.  While this fixes the coverage issue, I prefer to use the auto property when possible (at least for the time being).

Any help will be appreciated.

Thank you.

Mike Cowan

1 comment
Maria Serebryansky
Comment actions Permalink

Hello Michael,

Coverage analysis for auto-properties is not supported. Sorry.

Please follow the ticket in our issue tracker to be notified when this feature is implemented:

Kind regards,


Please sign in to leave a comment.