Jus*_*tin 18 .net unit-testing code-coverage visual-studio vs-unit-testing-framework
在为包含多个项目的解决方案运行代码覆盖时,我注意到Code Coverage还包括我的测试程序集.
我发现了一篇文章,建议使用.runsettings的以下模板:
<?xml version="1.0" encoding="utf-8"?>
<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>
<ModulePaths>
<Include>
<!-- Do not specify any includes. This will attempt to include all binaries -->
</Include>
<Exclude>
<!-- Exclude modules that aren't to be processed, by their name / path -->
<ModulePath>.*Tests\.dll$</ModulePath> <!-- I put it here -->
</Exclude>
</ModulePaths>
<UseVerifiableInstrumentation>True</UseVerifiableInstrumentation>
<AllowLowIntegrityProcesses>True</AllowLowIntegrityProcesses>
<CollectFromChildProcesses>True</CollectFromChildProcesses>
<CollectAspDotNet>False</CollectAspDotNet>
<!--
Additional paths to search for symbol files. Symbols must be found for modules to be instrumented.
If symbols are alongside the binaries, they are automatically picked up. Otherwise specify the here.
Note that searching for symbols increases code coverage runtime. So keep this small and local.
<SymbolSearchPaths>
<Path>C:\Users\User\Documents\Visual Studio 11\Projects\ProjectX\bin\Debug</Path>
<Path>\\mybuildshare\builds\ProjectX</Path>
</SymbolSearchPaths>
-->
<Functions>
<Exclude>
<Function>^std::.*</Function>
<Function>^ATL::.*</Function>
<Function>.*::__GetTestMethodInfo.*</Function>
<Function>^Microsoft::VisualStudio::CppCodeCoverageFramework::.*</Function>
<Function>^Microsoft::VisualStudio::CppUnitTestFramework::.*</Function>
<Function>.*::YOU_CAN_ONLY_DESIGNATE_ONE_.*</Function>
</Exclude>
</Functions>
<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>
<Sources>
<Exclude>
<Source>.*\\atlmfc\\.*</Source>
<Source>.*\\vctools\\.*</Source>
<Source>.*\\public\\sdk\\.*</Source>
<Source>.*\\microsoft sdks\\.*</Source>
<Source>.*\\vc\\include\\.*</Source>
</Exclude>
</Sources>
<CompanyNames>
<Exclude>
<CompanyName>.*microsoft.*</CompanyName>
</Exclude>
</CompanyNames>
<PublicKeyTokens>
<Exclude>
<PublicKeyToken>^B77A5C561934E089$</PublicKeyToken>
<PublicKeyToken>^B03F5F7F11D50A3A$</PublicKeyToken>
<PublicKeyToken>^31BF3856AD364E35$</PublicKeyToken>
<PublicKeyToken>^89845DCD8080CC91$</PublicKeyToken>
<PublicKeyToken>^71E9BCE111E9429C$</PublicKeyToken>
<PublicKeyToken>^8F50407C4E9E73B6$</PublicKeyToken>
<PublicKeyToken>^E361AF139669C375$</PublicKeyToken>
</Exclude>
</PublicKeyTokens>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
Run Code Online (Sandbox Code Playgroud)
现在的问题是,只要我在TEST\Test设置菜单中检查我的.runsettings文件,我就会看到Code Coverage只分析我解决方案中的许多程序集中的一个.
即使我删除了该行
<ModulePath>.*Tests\.dll$</ModulePath> <!-- I put it here -->
Run Code Online (Sandbox Code Playgroud)
只分析了一个装配体.我想避免将所有程序集添加到Include列表中,我只需要排除所有测试程序集.
为什么.runsettings使代码覆盖只看到我的VS解决方案中的一个程序集?
Hul*_*lah 23
问题是这个时期.出于某种原因,RegEx对此感到窒息.您可以通过以下方式绕过它:
<ModulePaths>
<Include>
<ModulePath>.*MyCompany\.Namespace\.Project\.dll$</ModulePath>
</Include>
<Exclude>
<ModulePath>.*ThirdParty\.Namespace\.Project\.dll$</ModulePath>
</Exclude>
</ModulePaths>
Run Code Online (Sandbox Code Playgroud)
此外,空包含有效并暗示包含所有模块.微软提供的示例说明:
<!--
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.
-->
Run Code Online (Sandbox Code Playgroud)
sfu*_*qua 13
在相关的说明中,我遇到了这篇文章,因为我有点想清楚包含和排除路径的正则表达式性质.对于我的WPF应用程序,我想排除Caliburn.Micro上的覆盖率分析.所以我写了
<ModulePath>Caliburn.Micro.dll</ModulePath>
Run Code Online (Sandbox Code Playgroud)
显然,这段时间让我感到困惑.这个问题不会受到这个问题的影响,但我敢打赌,我不是唯一一个忽视这个简单事实的人.对于任何其他读者,请注意*不是通配符 - 它是正则表达式"任意数量"的运算符.你不想要*.Caliburn,而是.*Caliburn这样这个简单的陈述解决了我的问题:
<ModulePath>.*Caliburn.*</ModulePath>
Run Code Online (Sandbox Code Playgroud)
因为它正在寻找路径而不仅仅是模块名称,所以需要模块前面的.*来忽略它 - 也就是说,你想在任何给定的文件路径上忽略它.
因为我在其他任何地方都找不到这个答案,这只是让我花了一段时间才弄清楚,这ModulePath是完整的路径,您可能正在路径中的其他地方匹配您的模式。
例如,如果您有一个 projectFoo和一个 project Foo.Tests,并且它们被构建到自己的目录中,那么您最终会得到Foo.Tests\bin\Release\Foo.dlland Foo.Tests\bin\Release\Foo.Tests.dll。这是测试程序集将引用的 dll,因此这是使用的路径。Foo\bin\Release\Foo.dll测试程序集不直接引用。
如果您尝试排除.*tests.*,它将匹配两个路径并且不产生覆盖。
为了仅排除文件名中带有“test”的程序集,而忽略其路径,我使用了
<Exclude>
<ModulePath>.*\\[^\\]*test[^\\]*\.dll</ModulePath>
</Exclude>
Run Code Online (Sandbox Code Playgroud)