简体   繁体   English

如何从代码覆盖率中排除属性中的 lambda 函数?

[英]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.我正在尝试为我的 C# 应用程序收集一些好的代码覆盖率统计信息,但我想忽略生成的 SubSonic 类,因为收集这些函数的代码覆盖率统计信息没有实际意义。

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.我已经编辑了 ActiveRecord.tt 文件以在每个方法和属性上包含 [ExcludeFromCodeCoverage] 属性,这已经删除了我的代码覆盖率 window 中的大部分不相关结果。但是,尽管具有代码覆盖率排除属性,属性中的 lambda 表达式被标记为未涵盖。 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. Lambda 表达式在方法中被忽略,如预期的那样。

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%.但是,我们的客户要求我们的代码覆盖率要达到95%以上。

I could automatically generate unit tests along with the ActiveRecord.tt file, but this is hardly ideal.我可以自动生成单元测试以及 ActiveRecord.tt 文件,但这并不理想。

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: 虽然我不确定您用于单元测试的工具,但是您可以尝试将属性移到get方法声明的顶部:

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 这是因为在编译时,GetValue属性被转换为带有签名get_GetValue : int的方法get_GetValue : int

Add the [ExcludeFromCoverage] attribute to the class instead of the property. [ExcludeFromCoverage]属性添加到类而不是属性。 Everything within the class will be excluded, including the lambdas. 该类中的所有内容(包括lambda)都将被排除在外。

Idk what about C#, but in Java this can be done by using inner class我想知道 C# 怎么样,但是在 Java 中,这可以通过使用内部类来完成

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.如果它对其他人有帮助,我正在努力解决与此处链接非常相似的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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