首页 > 解决方案 > ExcludeFromCodeCoverage 不适用于 asp.net core 3.1 单元测试项目

问题描述

using MyAPI.ShippingAddress.Operations;
using MyAPI.ShippingAddress.Request;
using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics.CodeAnalysis;

namespace MyAPI
{
    public class DependencyServiceFactory
    {
        [ExcludeFromCodeCoverage]
        public void Add(int a, int b)
        {
            return a + b;
        }
    }
}

我创建了 asp.net core 3.1 MS 单元测试项目,一切都按预期工作,但 [ExcludeFromCodeCoverage] 不起作用。我需要从代码覆盖结果中删除此方法

有什么建议吗?

标签: c#unit-testingmstestasp.net-core-3.1

解决方案


添加 CodeCoverage.runsettings 文件并使用测试探索对其进行配置。CodeCoverage.runsettings 文件应包含“System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute”

        <Attributes>
          <Exclude>                
            <Attribute>^System\.Diagnostics\.DebuggerHiddenAttribute$</Attribute>
            <Attribute>^System\.Diagnostics\.DebuggerNonUserCodeAttribute$</Attribute>
            <Attribute>^System\.Runtime\.CompilerServices.CompilerGeneratedAttribute$</Attribute>
            <Attribute>^System\.CodeDom\.Compiler.GeneratedCodeAttribute$</Attribute>
            <Attribute>^System\.Diagnostics\.CodeAnalysis.ExcludeFromCodeCoverageAttribute$</Attribute>
          </Exclude>
        </Attributes>

推荐阅读