我有一个方法,它将结构列表写入文件并返回列表中的结构计数
[ExcludeFromCodeCoverage]
public static int WriteFileFromTransactionsList(List<Transactions> transList = new List<Transactions>(), string outputFile)
{
FileSystemCommands.WriteFileFromStringList(
transList.Select(t => string.Format("ROC: {0};
ClaimNumber: {1}; TranType: {2}; ItemClaimant: {3}{4}",
t.ROC, t.ClaimNumber, t.TransCode, t.Coverage, t.Claimant)).ToList(),
string.Format("{0}", outputFile));
return transList.Count();
}
我添加了[ExclucdeFromCodeCoverage]标记,因为FileSystemCommands.WriteFileFromStringList方法是在FileSystemCommands单元测试中进行单元测试的,此方法唯一没有经过测试的方法是返回transList的计数,如果是传递null transList;
我也在使用.runsettings文件
<?xml version="1.0" encoding="utf-8"?>
<!-- File name extension must be .runsettings -->
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Configuration>
<CodeCoverage>
<!--
Additional paths to search for .pdb (symbol) files. Symbols must be found for modules to be instrumented.
If .pdb files are in the same folder as the .dll or .exe files, they are automatically found. Otherwise, specify them here.
Note that searching for symbols increases code coverage runtime. So keep this small and local.
-->
<!--
<SymbolSearchPaths>
<Path>C:\Users\User\Documents\Visual Studio 2012\Projects\ProjectX\bin\Debug</Path>
<Path>\\mybuildshare\builds\ProjectX</Path>
</SymbolSearchPaths>
-->
<!--
About include/exclude lists:
Empty "Include" clauses imply all; empty "Exclude" clauses imply none.
Each element in the list is a regular expression (ECMAScript syntax). See http://msdn.microsoft.com/library/2k3te2cs.aspx.
An item must first match at least one entry in the include list to be included.
Included items must then not match any entries in the exclude list to remain included.
-->
<!-- Match assembly file paths: -->
<ModulePaths>
<Include>
<ModulePath>.*\.exe$</ModulePath>
</Include>
<Exclude>
<ModulePath>.*\.dll$</ModulePath>
</Exclude>
</ModulePaths>
<!-- Match attributes on any code element: -->
<Attributes>
<Exclude>
<Attribute>^System\.Diagnostics\.CodeAnalysis\.ExcludeFromCodeCoverageAttribute$</Attribute>
</Exclude>
</Attributes>
<!-- Match the company name property in the assembly: -->
<CompanyNames>
<Exclude>
<CompanyName>.*microsoft.*</CompanyName>
<CompanyName>.*fluentassertions*</CompanyName>
</Exclude>
</CompanyNames>
<!-- We recommend you do not change the following values: -->
<UseVerifiableInstrumentation>True</UseVerifiableInstrumentation>
<AllowLowIntegrityProcesses>True</AllowLowIntegrityProcesses>
<CollectFromChildProcesses>True</CollectFromChildProcesses>
<CollectAspDotNet>False</CollectAspDotNet>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
然而,当我从visual studio运行代码覆盖率分析时(我正在使用测试框架NUnit 3),我得到一个报告,说他的方法100%未涵盖,并突出显示代码的transList.Select()部分
任何人都可以看到为什么代码覆盖率分析会包含这个,因为它在一个应该从代码覆盖范围中排除的方法中?
此外,我在其他具有Exclude from code coverage标记的方法中也注意到了这一点。当我使用泛型时,它主要会消失。