简体   繁体   中英

How can I exclude lambda functions in properties from code coverage?

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.

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

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

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;

    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM