Skip to content

Commit d4890a6

Browse files
authored
Merge pull request #150 from tonerdo/doc-update
Update documentation to include Global tool
2 parents 83a8058 + 9c7d35d commit d4890a6

File tree

1 file changed

+154
-14
lines changed

1 file changed

+154
-14
lines changed

README.md

Lines changed: 154 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,21 @@ Coverlet is a cross platform code coverage library for .NET Core, with support f
44

55
## Installation
66

7-
Available on [NuGet](https://www.nuget.org/packages/coverlet.msbuild/)
7+
**Global Tool**:
88

9-
Visual Studio:
10-
11-
```powershell
12-
PM> Install-Package coverlet.msbuild
9+
```bash
10+
dotnet tool install --global coverlet.console
1311
```
1412

15-
.NET Core CLI:
13+
**Package Reference**:
1614

1715
```bash
1816
dotnet add package coverlet.msbuild
1917
```
2018

2119
## How It Works
2220

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:
2422

2523
### Before Tests Run
2624

@@ -37,9 +35,151 @@ _Note: The assembly you'd like to get coverage for must be different from the as
3735

3836
## Usage
3937

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`):
49+
50+
```bash
51+
Cross platform .NET Core code coverage tool 1.0.0.0
52+
53+
Usage: coverlet [arguments] [options]
54+
55+
Arguments:
56+
<ASSEMBLY> Path to the test assembly.
57+
58+
Options:
59+
-h|--help Show help information
60+
-v|--version Show version information
61+
-t|--target Path to the test runner application.
62+
-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:
76+
77+
```bash
78+
coverlet /path/to/test-assembly.dll --target "dotnet" --targetargs "test /path/to/test-project --no-build"
79+
```
80+
81+
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`:
88+
89+
```bash
90+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --format opencover
91+
```
92+
93+
Supported Formats:
94+
95+
* json (default)
96+
* lcov
97+
* opencover
98+
* cobertura
99+
100+
The `--format` option can be specified multiple times to output multiple formats in a single run:
101+
102+
```bash
103+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --format opencover --format lcov
104+
```
105+
106+
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.
107+
108+
```bash
109+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --output "/custom/path/result.json"
110+
```
111+
112+
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.
113+
114+
```bash
115+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --output "/custom/directory/" -f json -f lcov
116+
```
117+
118+
#### Threshold
119+
120+
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.
121+
122+
```bash
123+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --threshold 80
124+
```
125+
126+
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`.
133+
134+
```bash
135+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --threshold 80 --threshold-type line --threshold-type method
136+
```
137+
138+
#### Excluding From Coverage
139+
140+
##### Attributes
141+
142+
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`)
150+
151+
```bash
152+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --exclude-by-file "../dir1/class1.cs"
153+
```
154+
155+
##### Filters
156+
157+
Coverlet gives the ability to have fine grained control over what gets excluded using "filter expressions".
158+
159+
Syntax: `--exclude '[Assembly-Filter]Type-Filter'`
160+
161+
Wildcards
162+
- `*` => matches zero or more characters
163+
- `?` => the prefixed character is optional
164+
165+
Examples
166+
- `--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
171+
172+
```bash
173+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --exclude "[coverlet.*]Coverlet.Core.Coverage"
174+
```
175+
176+
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.
41181
42-
### Code Coverage
182+
#### Code Coverage
43183
44184
Enabling code coverage is as simple as setting the `CollectCoverage` property to `true`
45185
@@ -49,7 +189,7 @@ dotnet test /p:CollectCoverage=true
49189
50190
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.
51191
52-
### Coverage Output
192+
#### Coverage Output
53193
54194
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:
55195
@@ -78,7 +218,7 @@ To specify a directory where all results will be written to (especially if using
78218
dotnet test /p:CollectCoverage=true /p:CoverletOutput='./results/'
79219
```
80220
81-
### Threshold
221+
#### Threshold
82222
83223
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.
84224
@@ -94,9 +234,9 @@ dotnet test /p:CollectCoverage=true /p:Threshold=80 /p:ThresholdType=line
94234
95235
You can specify multiple values for `ThresholdType` by separating them with commas. Valid values include `line`, `branch` and `method`.
96236
97-
### Excluding From Coverage
237+
#### Excluding From Coverage
98238
99-
#### Attributes
239+
##### Attributes
100240
101241
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.
102242
@@ -110,7 +250,7 @@ You can also ignore specific source files from code coverage using the `ExcludeB
110250
dotnet test /p:CollectCoverage=true /p:ExcludeByFile=\"../dir1/class1.cs,../dir2/*.cs,../dir3/**/*.cs,\"
111251
```
112252
113-
#### Filters
253+
##### Filters
114254
Coverlet gives the ability to have fine grained control over what gets excluded using "filter expressions".
115255
116256
Syntax: `/p:Exclude=[Assembly-Filter]Type-Filter`

0 commit comments

Comments
 (0)