You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -4,23 +4,21 @@ Coverlet is a cross platform code coverage library for .NET Core, with support f
4
4
5
5
## Installation
6
6
7
-
Available on [NuGet](https://www.nuget.org/packages/coverlet.msbuild/)
7
+
**Global Tool**:
8
8
9
-
Visual Studio:
10
-
11
-
```powershell
12
-
PM> Install-Package coverlet.msbuild
9
+
```bash
10
+
dotnet tool install --global coverlet.console
13
11
```
14
12
15
-
.NET Core CLI:
13
+
**Package Reference**:
16
14
17
15
```bash
18
16
dotnet add package coverlet.msbuild
19
17
```
20
18
21
19
## How It Works
22
20
23
-
Coverlet integrates with the MSBuild system and that allows it to go through the following process:
21
+
Coverlet generates code coverage information by going through the following process:
24
22
25
23
### Before Tests Run
26
24
@@ -37,9 +35,151 @@ _Note: The assembly you'd like to get coverage for must be different from the as
37
35
38
36
## Usage
39
37
40
-
Coverlet doesn't require any additional setup other than including the NuGet package in the unit test project. It integrates with the `dotnet test` infrastructure built into the .NET Core CLI and when enabled, will automatically generate coverage results after tests are run.
38
+
Coverlet can be used either as a .NET Core global tool that can be invoked from a terminal or as a NuGet package that integrates with the MSBuild system of your test project.
39
+
40
+
### Global Tool
41
+
42
+
To see a list of options, run:
43
+
44
+
```bash
45
+
coverlet --help
46
+
```
47
+
48
+
The current options are (output of `coverlet --help`):
-a|--targetargs Arguments to be passed to the test runner.
63
+
-o|--output Output of the generated coverage report
64
+
-f|--format Format of the generated coverage report.
65
+
--threshold Exits with error if the coverage % is below value.
66
+
--threshold-type Coverage type to apply the threshold to.
67
+
--exclude Filter expressions to exclude specific modules and types.
68
+
--exclude-by-file Glob patterns specifying source files to exclude.
69
+
```
70
+
71
+
#### Code Coverage
72
+
73
+
The `coverlet` tool is invoked by specifying the path to the assembly that contains the unit tests. You also need to specify the test runner and the arguments to pass to the test runner using the `--target` and `--targetargs` options respectively. The invocation of the test runner with the supplied arguments **must not** involve a recompilation of the unit test assembly or no coverage data will be generated.
74
+
75
+
The following example shows how to use the familiar `dotnet test` toolchain:
After the above command is run, a `coverage.json` file containing the results will be generated in the directory the `coverlet`command was run. A summary of the results will also be displayed in the terminal.
82
+
83
+
_Note: The `--no-build` flag is specified so that the `/path/to/test-assembly.dll` isn't rebuilt_
84
+
85
+
#### Coverage Output
86
+
87
+
Coverlet can generate coverage results in multiple formats, which is specified using the `--format` or `-f` options. For example, the following command emits coverage results in the `opencover` format instead of `json`:
By default, Coverlet will output the coverage results file(s) in the current working directory. The `--output` or `-o` options can be used to override this behaviour.
The above command will write the results to the supplied path, if no file extension is specified it'll use the standard extension of the selected output format. To specify a directory instead, simply append a `/` to the end of the value.
Coverlet allows you to specify a coverage threshold below which it returns a non-zero exit code. This allows you to enforce a minimum coverage percent on all changes to your project.
The above command will automatically fail the build if the line, branch or method coverage of _any_ of the instrumented modules falls below 80%. You can specify what type of coverage to apply the threshold value to using the `--threshold-type` option. For example to apply the threshold check to only **line** coverage:
127
+
128
+
```bash
129
+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --threshold 80 --threshold-type line
130
+
```
131
+
132
+
You can specify the `--threshold-type` option multiple times. Valid values include `line`, `branch` and `method`.
You can ignore a method or an entire class from code coverage by creating and applying the `ExcludeFromCodeCoverage` attribute present in the `System.Diagnostics.CodeAnalysis` namespace.
143
+
144
+
##### Source Files
145
+
146
+
You can also ignore specific source files from code coverage using the `--exclude-by-file` option
147
+
- Can be specified multiple times
148
+
- Use absolute or relative paths (relative to the project directory)
149
+
- Use file path or directory path with globbing (e.g `dir1/*.cs`)
- `--exclude "[*]*"` => Excludes all types in all assemblies (nothing is instrumented)
167
+
- `--exclude "[coverlet.*]Coverlet.Core.Coverage"` => Excludes the Coverage class in the `Coverlet.Core` namespace belonging to any assembly that matches `coverlet.*` (e.g `coverlet.core`)
168
+
- `--exclude "[*]Coverlet.Core.Instrumentation.*"` => Excludes all types belonging to `Coverlet.Core.Instrumentation` namespace in any assembly
169
+
- `--exclude "[coverlet.*.tests?]*"` => Excludes all types in any assembly starting with `coverlet.` and ending with `.test` or `.tests` (the `?` makes the `s` optional)
170
+
- `--exclude "[coverlet.*]*" --exclude "[*]Coverlet.Core*"` => Excludes assemblies matching `coverlet.*` and excludes all types belonging to the `Coverlet.Core` namespace in any assembly
You can specify the `--exclude` option multiple times to allow for multiple filter expressions.
177
+
178
+
### MSBuild
179
+
180
+
In this mode, Coverlet doesn't require any additional setup other than including the NuGet package in the unit test project. It integrates with the `dotnet test` infrastructure built into the .NET Core CLI and when enabled, will automatically generate coverage results after tests are run.
41
181
42
-
### Code Coverage
182
+
#### Code Coverage
43
183
44
184
Enabling code coverage is as simple as setting the `CollectCoverage` property to `true`
45
185
@@ -49,7 +189,7 @@ dotnet test /p:CollectCoverage=true
49
189
50
190
After the above command is run, a `coverage.json` file containing the results will be generated in the root directory of the test project. A summary of the results will also be displayed in the terminal.
51
191
52
-
### Coverage Output
192
+
#### Coverage Output
53
193
54
194
Coverlet can generate coverage results in multiple formats, which is specified using the `CoverletOutputFormat` property. For example, the following command emits coverage results in the `opencover` format:
55
195
@@ -78,7 +218,7 @@ To specify a directory where all results will be written to (especially if using
78
218
dotnet test /p:CollectCoverage=true /p:CoverletOutput='./results/'
79
219
```
80
220
81
-
### Threshold
221
+
#### Threshold
82
222
83
223
Coverlet allows you to specify a coverage threshold below which it fails the build. This allows you to enforce a minimum coverage percent on all changes to your project.
84
224
@@ -94,9 +234,9 @@ dotnet test /p:CollectCoverage=true /p:Threshold=80 /p:ThresholdType=line
94
234
95
235
You can specify multiple values for `ThresholdType` by separating them with commas. Valid values include `line`, `branch` and `method`.
96
236
97
-
### Excluding From Coverage
237
+
#### Excluding From Coverage
98
238
99
-
#### Attributes
239
+
##### Attributes
100
240
101
241
You can ignore a method or an entire class from code coverage by creating and applying the `ExcludeFromCodeCoverage` attribute present in the `System.Diagnostics.CodeAnalysis` namespace.
102
242
@@ -110,7 +250,7 @@ You can also ignore specific source files from code coverage using the `ExcludeB
110
250
dotnet test /p:CollectCoverage=true /p:ExcludeByFile=\"../dir1/class1.cs,../dir2/*.cs,../dir3/**/*.cs,\"
111
251
```
112
252
113
-
#### Filters
253
+
##### Filters
114
254
Coverlet gives the ability to have fine grained control over what gets excluded using "filter expressions".
0 commit comments