Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
By default, code coverage analyzes all solution assemblies that are loaded during unit tests. We recommend that you use this default behavior, because it works well most of the time. For more information, see Use code coverage to determine how much code is tested.
To exclude test code from the code coverage results and only include application code, add the ExcludeFromCodeCoverageAttribute attribute to your test class.
To include assemblies that aren't part of your solution, obtain the .pdb files for these assemblies and copy them into the same folder as the assembly .dll files.
Run settings file
The run settings file is the configuration file used by unit testing tools. Advanced code coverage settings are specified in a .runsettings file.
To customize code coverage, follow these steps:
Add a run settings file to your solution. In Solution Explorer, on the shortcut menu of your solution, choose Add > New Item, and select XML File. Save the file with a name such as CodeCoverage.runsettings.
If you don't see all the item templates, choose Show All Templates, and then choose the item template.
Add the content from the example file at the end of this article, and then customize it to your needs as described in the sections that follow.
Select a run settings file.
Symbol search paths
Code coverage requires symbol files (.pdb files) for assemblies. For assemblies built by your solution, symbol files are usually present alongside the binary files, and code coverage works automatically. In some cases, you might want to include referenced assemblies in your code coverage analysis. In such cases, the .pdb files might not be adjacent to the binaries, but you can specify the symbol search path in the .runsettings file.
<SymbolSearchPaths>
<Path>\\mybuildshare\builds\ProjectX</Path>
<!--More paths if required-->
</SymbolSearchPaths>
Note
Symbol resolution can take time, especially when using a remote file location with many assemblies. Therefore, consider copying .pdb files to the same local location as the binary (.dll and .exe) files.
Include or exclude assemblies and members
You can include or exclude assemblies or specific types and members from code coverage analysis. If the Include section is empty or omitted, then all assemblies that are loaded and have associated PDB files are included. If an assembly or member matches a clause in the Exclude section, then it is excluded from code coverage. The Exclude section takes precedence over the Include section: if an assembly is listed in both Include and Exclude, it will not be included in code coverage.
For example, the following XML excludes a single assembly by specifying its name:
<ModulePaths>
<Exclude>
<ModulePath>.*Fabrikam.Math.UnitTest.dll</ModulePath>
<!-- Add more ModulePath nodes here. -->
</Exclude>
</ModulePaths>
The following example specifies that only a single assembly should be included in code coverage:
<ModulePaths>
<Include>
<ModulePath>.*Fabrikam.Math.dll</ModulePath>
<!-- Add more ModulePath nodes here. -->
</Include>
</ModulePaths>
The following table shows the various ways that assemblies and members can be matched for inclusion in or exclusion from code coverage.
| XML element | What it matches |
|---|---|
| ModulePath | Matches assemblies specified by assembly name or file path. |
| CompanyName | Matches assemblies by the Company attribute. |
| PublicKeyToken | Matches signed assemblies by the public key token. |
| Source | Matches elements by the path name of the source file in which they're defined. |
| Attribute | Matches elements that have the specified attribute. Specify the full name of the attribute, for example <Attribute>^System\.Diagnostics\.DebuggerHiddenAttribute$</Attribute>.If you exclude the CompilerGeneratedAttribute attribute, code that uses language features such as async, await, yield return, and auto-implemented properties is excluded from code coverage analysis. To exclude truly generated code, only exclude the GeneratedCodeAttribute attribute. |
| Function | Matches procedures, functions, or methods by fully qualified name, including the parameter list. You can also match part of the name by using a regular expression. Examples: Fabrikam.Math.LocalMath.SquareRoot(double); (C#)Fabrikam::Math::LocalMath::SquareRoot(double) (C++) |
Include or exclude test assemblies
To include or exclude test assemblies from the coverage report, you can use the <IncludeTestAssembly> element in the <Configuration> section of your .runsettings file.
In this example, setting <IncludeTestAssembly> to False will exclude test assemblies from the code coverage report. If you want to include test assemblies, set it to True.
<?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>
<IncludeTestAssembly>False</IncludeTestAssembly>
...
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
Note
The default value of IncludeTestAssembly in VSTest is true, while it is false in Microsoft.Testing.Platform. This means that test projects are included by default. For more information, see Code Coverage configuration.
Regular expressions
Include and exclude nodes use regular expressions, which aren't the same as wildcards. All matches are case-insensitive. Some examples are:
.* matches a string of any characters
\. matches a dot "."
\( \) matches parentheses "( )"
\\ matches a file path delimiter "\"
^ matches the start of the string
$ matches the end of the string
The following XML shows how to include and exclude specific assemblies by using regular expressions:
<ModulePaths>
<Include>
<!-- Include all loaded .dll assemblies (but not .exe assemblies): -->
<ModulePath>.*\.dll$</ModulePath>
</Include>
<Exclude>
<!-- But exclude some assemblies: -->
<ModulePath>.*\\Fabrikam\.MyTests1\.dll$</ModulePath>
<!-- Exclude all file paths that contain "Temp": -->
<ModulePath>.*Temp.*</ModulePath>
</Exclude>
</ModulePaths>
The following XML shows how to include and exclude specific functions by using regular expressions:
<Functions>
<Include>
<!-- Include methods in the Fabrikam namespace: -->
<Function>^Fabrikam\..*</Function>
<!-- Include all methods named EqualTo: -->
<Function>.*\.EqualTo\(.*</Function>
</Include>
<Exclude>
<!-- Exclude methods in a class or namespace named UnitTest: -->
<Function>.*\.UnitTest\..*</Function>
</Exclude>
</Functions>
Warning
If there is an error in a regular expression, such as an unescaped or unmatched parenthesis, code coverage analysis won't run.
For more information about regular expressions, see Use regular expressions in Visual Studio.
Sample .runsettings file
Copy this code and edit it to suit your needs.