.net core 将类或方法从.net core中的代码覆盖范围中排除

.net core 将类或方法从.net core中的代码覆盖范围中排除,.net-core,.net Core,我知道我可以使用[ExcludeFromCodeCoverage]排除.Net framework 4中的代码覆盖率 有人知道我是否有办法从.dotnet核心中排除代码覆盖率吗?我终于找到了解决方案 首先,您需要使用.runsettings文件来配置代码覆盖率: 但问题是ExcludeFromCodeCoverageAttribute是密封的,所以我们不能使用它 我在这里发布了这个问题,看起来它将在下一个版本中得到解决 现在 我使用GeneratedCodeAttribute,如.runs

我知道我可以使用
[ExcludeFromCodeCoverage]
排除.Net framework 4中的代码覆盖率


有人知道我是否有办法从.dotnet核心中排除代码覆盖率吗?

我终于找到了解决方案

首先,您需要使用.runsettings文件来配置代码覆盖率:

但问题是ExcludeFromCodeCoverageAttribute是密封的,所以我们不能使用它

我在这里发布了这个问题,看起来它将在下一个版本中得到解决

现在 我使用GeneratedCodeAttribute,如.runsettings示例中所述。 您还可以使用自定义属性或任何其他排除规则,如下所示:


创建自己的ExcludeFromCodeOverage属性

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Interface)]
public class ExcludeFromCodeCoverageAttribute : Attribute
{
    public ExcludeFromCodeCoverageAttribute(string reason = null)
    {
        Reason = Reason;
    }

    public string Reason { get; set; }
}
然后将其添加到.runsettings文件并将其排除

...
   <Configuration>
      <CodeCoverage>
        .
        .
        .
        <Attributes>
          <Exclude>
            <Attribute>^YourCompany\.YourNameSpace\.sExcludeFromCodeCoverageAttribute$</Attribute>
          </Exclude>
        </Attributes>
        .
        .
        .
    </Configuration>
...
。。。
.
.
.

自.NET Core 2.0以来,您可以使用
ExcludeFromCodeCoverage
属性

using System.Diagnostics.CodeAnalysis;

namespace YourNamespace
{
    [ExcludeFromCodeCoverage]
    public class YourClass
    {
        ...
    }
}

我想快速搜索仍然存在。在此处找到:-我想我应该参考System.Diagnostics.ToolsId找到解决方案了吗?没有,仍然无法将System.Diagnostics.Tools nuget包添加到我的asp.net核心项目中。不幸的是,这对我的asp.net核心2.2 web api项目不起作用。我想排除从dbcontext构建的OnModelCreating()方法。但它不起作用。运行代码度量时,仍然会显示具有非常低的可维护性指数的方法。我希望该方法完全不显示,因为它是自动生成的。