4

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%.

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

Thank you for your time.

Steve Rukuts
  • 9,167
  • 3
  • 50
  • 72

4 Answers4

1

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

astellin
  • 433
  • 3
  • 13
  • 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. – Steve Rukuts Sep 06 '10 at 08:43
  • 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. – Steve Rukuts Sep 06 '10 at 08:48
1

Add the [ExcludeFromCoverage] attribute to the class instead of the property. Everything within the class will be excluded, including the lambdas.

Brian Erickson
  • 945
  • 8
  • 18
  • Unfortunately, that means the code that I want to be checked for coverage will not be checked. That'll probably work for some people though, nice idea. – Steve Rukuts May 12 '11 at 09:36
  • I have the exact same problem. I really wanted to get 100% coverage so I can monitor new uncovered code. I ended up putting the offending code into its own class. I know that's lame, but it worked. Another option I was considering was to create named methods instead of lambdas. – Brian Erickson May 12 '11 at 15:26
0

Idk what about C#, but in Java this can be done by using inner class

class SuperTest {

    @ExcludeFromJacocoGeneratedReport
    public Object test() {
        return Optional.of(1)
                .map(FunctionWrapper.mapFunction)
                .orElseThrow();
    }

    @ExcludeFromJacocoGeneratedReport
    public static class FunctionWrapper {

        static Function<Object, String> mapFunction = String::valueOf;

    }
}
Elya
  • 1
  • 1
0

Probably a little late to the party however it should work with below

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

I was struggling with very similar issue linking here if it helps others.

touchofevil
  • 595
  • 4
  • 21