views:

60

answers:

1

I am trying to gather some good code coverage stats for my C# application, but I would like to ignore the generated SubSonic classes as there is no real point in gathering code coverage statistics for these functions.

I have edited the ActiveRecord.tt file to include the [ExcludeFromCodeCoverage] attribute on every method and property, and this has removed most of the irrelevant results in my code coverage window. However, despite having the code coverage exclusion attribute, lambda expressions within properties are being marked as not covered. As an example:

[ExcludeFromCodeCoverage]
int GetValue
{
    get
    {
        this.SomeMethod(); // this is excluded
        return this.Find(x => x.Value == this._Value); // this.Find is excluded
                                                       // but x => x.Value == ...
                                                       // is not.
    }
}

This only occurs in properties. Lambda expressions are ignored within methods, as expected.

Now, to me, this is rather annoying, but not necessarily fatal, as I can just ignore these expressions as I browse my code coverage list. However, our client has demanded that our code coverage level be above 95%. It is unlikely that I will be able to make them understand that this statistic is largely useless.

I could automatically generate unit tests along with the ActiveRecord.tt file, but this is hardly ideal.

Thank you for your time.

+1  A: 

Though i'm not sure what tools you're using for unit-testing, you could try to move the attribute on top of the get method declaration:

int GetValue
{
    [ExcludeFromCodeCoverage]
    get
    {
        this.SomeMethod();
        return this.Find(x => x.Value == this._Value);
    }
}

This, because on compile time, the GetValue property is converted into a method, with signature get_GetValue : int

Excel20
While this is a great idea, it doesn't work. It does compile, however, but my code coverage percentage does not change and the lambda expression is still marked as uncovered.
Raskolnikov
By the way, I am using the unit testing tools that ship with Visual Studio 2010, but I'm not sure if that affects anything. My hypothesis is that because these functions aren't literally part of the property that I define, they aren't being seen as part of the property. So maybe what I'm looking for is a way to apply attributes to lambda functions. I'll only have to apply them in about 5 places to make this work.
Raskolnikov