You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Coverlet覆盖率报告中排除AspNetCoreGeneratedDocument类

问题

代码覆盖率统计时,Solution.Web项目Views文件夹下的每个视图都会生成类似AspNetCoreGeneratedDocument.Views_Users__AddOrUpdateUser、AspNetCoreGeneratedDocument.Views_Users__GetUser的自动生成类,类归属为AspNetCoreGeneratedDocument。现有.runsettings配置如下,但无法排除这些类,需要实现类似dotCover的过滤效果:

<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="XPlat code coverage">
                <Configuration>
                    <!-- Globbing filter -->
                    <ExcludeByFile>**/Solution.Web/Views/*.cshtml</ExcludeByFile>
                    <IncludeTestAssembly>false</IncludeTestAssembly>
                    <DeterministicReport>false</DeterministicReport>
                </Configuration>
            </DataCollector>
        </DataCollectors>
    </DataCollectionRunSettings>
</RunSettings>
解决方案

由于Razor视图编译后生成的类不会直接对应.cshtml文件的过滤规则,需要针对类名/命名空间或生成类的特性进行精准排除:

方式1:按类名前缀排除

利用ExcludeByCode规则匹配类名前缀,直接排除所有AspNetCoreGeneratedDocument开头的类:

<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="XPlat code coverage">
                <Configuration>
                    <!-- 排除Views目录下的cshtml源文件 -->
                    <ExcludeByFile>**/Solution.Web/Views/*.cshtml</ExcludeByFile>
                    <!-- 排除自动生成的AspNetCoreGeneratedDocument类 -->
                    <ExcludeByCode>^AspNetCoreGeneratedDocument.*$</ExcludeByCode>
                    <IncludeTestAssembly>false</IncludeTestAssembly>
                    <DeterministicReport>false</DeterministicReport>
                </Configuration>
            </DataCollector>
        </DataCollectors>
    </DataCollectionRunSettings>
</RunSettings>

方式2:按程序集+类名组合排除

如果需要更精准的范围,可结合程序集和类名进行过滤:

<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="XPlat code coverage">
                <Configuration>
                    <ExcludeByFile>**/Solution.Web/Views/*.cshtml</ExcludeByFile>
                    <!-- 仅排除Solution.Web程序集中的AspNetCoreGeneratedDocument类 -->
                    <Exclude>[Solution.Web]AspNetCoreGeneratedDocument*</Exclude>
                    <IncludeTestAssembly>false</IncludeTestAssembly>
                    <DeterministicReport>false</DeterministicReport>
                </Configuration>
            </DataCollector>
        </DataCollectors>
    </DataCollectionRunSettings>
</RunSettings>

说明

  • ExcludeByCode使用正则表达式匹配类的完全限定名,^AspNetCoreGeneratedDocument.*$会匹配所有以该前缀开头的生成类
  • <Exclude>标签支持通配符,[Solution.Web]AspNetCoreGeneratedDocument*精准限定了程序集和类名范围,避免误排除其他程序集的同类命名

内容的提问来源于stack exchange,提问作者KA-Yasso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 20:01:06