在 Visual Studio 中使用属性从覆盖率分析中省略代码

发布于 2024-08-02 12:37:08 字数 309 浏览 8 评论 0 原文

我有一些类,由于某种原因,不能或不需要进行单元测试。我想从我的覆盖范围指标中排除这些类别,以便我可以更好地了解我真正关心的类别的覆盖范围。现在我必须在事后排除结果。我想做的是使用一个属性将这些类标记为已排除,以便它们一开始就不包含在内。 有没有办法用一个属性来装饰一个类,自动将其从覆盖率分析中排除? VS 覆盖率分析或 nCover 都可以。

FWIW,这些是我可以通过检查确保代码正确的类。大多数情况下,它们是我引入的现有框架类的包装类,以便能够模拟框架类。由于包装器被嘲笑,所以它们不会被测试。没关系,因为它们所做的只是包装我关心的框架类的方法。

I have some classes that, for one reason or another, cannot be or need not be unit tested. I'd like to exclude these classes from my coverage metrics so that I can get a better feel for the coverage on the classes I actually care about. Right now I have to exclude the results after the fact. What I would like to do is use an attribute to mark those classes as excluded so that they aren't included to begin with. Is there any way to decorate a class with an attribute that will automatically exclude it from coverage analysis? Either VS coverage analysis or nCover will work.

FWIW, these are classes that I can assure myself by inspection that the code is correct. Mostly they are wrapper classes around existing framework classes that I've introduced in order to be able to mock the framework class out. Since the wrapper's get mocked out they don't get tested. That's ok because all they do is wrap the framework class' methods that I care about.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜味超标? 2024-08-09 12:37:08

从 VS2010 开始,我们有 ExcludeFromCodeCoverageAttribute 。评论者指出这在 NCover 以及 DotCover + NUnit 中都有效。用法示例:

[ExcludeFromCodeCoverage]
public class myUntestableClass
{ }

另请参阅此链接。他们建议使用 VSInstr 作为命令行工具,它有 /EXCLUDE 选项(它不太方便)。

Starting with VS2010 we have ExcludeFromCodeCoverageAttribute. Commenters have noted this to work in NCover as well as DotCover + NUnit. Example usage:

[ExcludeFromCodeCoverage]
public class myUntestableClass
{ }

Also see this link. They suggest using VSInstr as command line tool, it have /EXCLUDE options (it's not as handy).

如果没结果 2024-08-09 12:37:08

我发现 有关几个诊断属性的一些信息DebuggerNonUserCodeAttributeDebuggerHiddenAttribute 表示使用这些属性将导致 VS 中的覆盖率分析器将这些属性排除在其结果之外。我已经用 DebuggerNonUserCodeAttribute 尝试过,它似乎有效。对于我正在考虑的大多数课程,我可能可以忍受这一点,尽管我不喜欢无法进入这些课程的副作用。对于包装类来说,这不应该是问题,但对于本质上难以测试并且我需要调试器访问的类来说,最终可能会出现问题。

不过,我仍在寻找替代方案。

I've found some information on a couple of Diagnostics attributes DebuggerNonUserCodeAttribute and DebuggerHiddenAttribute that indicates that using these attributes will cause the coverage analyzer in VS to leave these out of its results. I've tried it with the DebuggerNonUserCodeAttribute and it seems to work. I can probably live with this for most of the classes that I'm thinking of, though I don't like the side effect of not being able to step into these classes. That shouldn't be a problem for the wrapper classes, but it may end up being so with classes that are inherently hard to test and I need debugger access to.

I'm still looking for alternatives, though.

欢你一世 2024-08-09 12:37:08

使用 Nover,您可以创建一个属性,然后告诉 NCover 忽略该属性。

在我们的项目中,我们定义了这个属性(没有命名空间,所以很容易使用):

public class CoverageExcludeAttribute : Attribute { }

我们使用 NAnt,所以我们有一个如下所示的目标:

<target name="unittests" description="run nunit tests" >        
    <ncover
        ....
        excludeAttributes="CoverageExcludeAttribute"
    />
</target>

NCover FAQ描述了这种方法。我们的解决方案就是基于此。

或者,您可以使用 NCoverExplorer 的排除功能从最终报告中排除命名空间和程序集。这只是从报告中删除数据,但最终结果是相同的。

我们使用这两种技术。

With NCover you can create an attribute and then tell NCover to ignore that attribute.

In our projects, we have defined this attribute (no namespace, so it is easy to use):

public class CoverageExcludeAttribute : Attribute { }

We use NAnt, so we have a target that looks like this:

<target name="unittests" description="run nunit tests" >        
    <ncover
        ....
        excludeAttributes="CoverageExcludeAttribute"
    />
</target>

Question 9 in the NCover FAQ describes this method. We based our solution on this.

Alternatively, you can use the exclude feature of the NCoverExplorer to exclude namespaces and assemblies from the final report. This merely removes the data from the report, but the end result is the same.

We use both techniques.

巴黎盛开的樱花 2024-08-09 12:37:08

这对我有用!

This work for me! ????

use in .csproj: sonar keys

 <ItemGroup>
    <SonarQubeSetting Include="sonar.issue.ignore.allfile">
        <Value>ExcludeFromCodeCoverage</Value>
    </SonarQubeSetting>
</ItemGroup>

or

 <ItemGroup>
    <SonarQubeSetting Include="sonar.coverage.exclusions">
        <Value>**/FileClassToExclude.cs</Value>
    </SonarQubeSetting>
</ItemGroup>

And then use in your class file .cs by ExcludeFromCodeCoverage from microsoft

[ExcludeFromCodeCoverage]
public class ExcludeMeFromSonarCoverage
{
    public ExcludeMeFromSonarCoverage()
    {
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文