Skip to content

Commit 9f1cea8

Browse files
committed
Merge remote-tracking branch 'upstream/master' into BinOverBuild
2 parents f4c2d30 + 66a0119 commit 9f1cea8

22 files changed

+520
-418
lines changed

CONTRIBUTING.md

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,18 @@
22

33
Contributions are highly welcome, however, except for very small changes, kindly file an issue and let's have a discussion before you open a pull request.
44

5-
## Building The Project
5+
## Building the Project
66

77
Clone this repo:
88

9-
```bash
10-
git clone https://github.com/tonerdo/coverlet
11-
```
9+
git clone https://github.com/tonerdo/coverlet.git
10+
cd coverlet
1211

13-
Change directory to repo root:
12+
Building, testing, and packing use all the standard dotnet commands:
1413

15-
```bash
16-
cd coverlet
17-
```
18-
19-
Execute build script:
20-
21-
```bash
22-
dotnet msbuild build.proj
23-
```
24-
25-
This will result in the following:
26-
27-
* Restore all NuGet packages required for building
28-
* Build and publish all projects. Final binaries are placed into `<repo_root>\build\<Configuration>`
29-
* Build and run tests
30-
31-
These steps must be followed before you attempt to open the solution in an IDE (e.g. Visual Studio, Rider) for all projects to be loaded successfully.
14+
dotnet build
15+
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Include=[coverlet.*]*"
16+
dotnet pack
3217

3318
## Performance testing
3419

@@ -48,8 +33,8 @@ For more realistic testing it is recommended to try out any changes to the hit c
4833

4934
Change to the directory of the library and run the msbuild code coverage command:
5035

51-
dotnet msbuild /t:BuildAndTest /p:Coverage=true
52-
36+
dotnet test /p:Coverage=true
37+
5338
To run with a development version of coverlet call `dotnet run` instead of the installed coverlet version, e.g.:
5439

55-
dotnet msbuild /t:BuildAndTest /p:Coverage=true /p:CoverageExecutablePath="dotnet run -p C:\...\coverlet\src\coverlet.console\coverlet.console.csproj"
40+
dotnet test /p:Coverage=true /p:CoverageExecutablePath="dotnet run -p C:\...\coverlet\src\coverlet.console\coverlet.console.csproj"

Documentation/GlobalTool.md

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
# Coverlet as a Global Tool
2+
3+
To see a list of options, run:
4+
5+
```bash
6+
coverlet --help
7+
```
8+
9+
The current options are (output of `coverlet --help`):
10+
11+
```bash
12+
Cross platform .NET Core code coverage tool 1.0.0.0
13+
14+
Usage: coverlet [arguments] [options]
15+
16+
Arguments:
17+
<ASSEMBLY> Path to the test assembly.
18+
19+
Options:
20+
-h|--help Show help information
21+
-v|--version Show version information
22+
-t|--target Path to the test runner application.
23+
-a|--targetargs Arguments to be passed to the test runner.
24+
-o|--output Output of the generated coverage report
25+
-v|--verbosity Sets the verbosity level of the command. Allowed values are quiet, minimal, normal, detailed.
26+
-f|--format Format of the generated coverage report.
27+
--threshold Exits with error if the coverage % is below value.
28+
--threshold-type Coverage type to apply the threshold to.
29+
--threshold-stat Coverage statistic used to enforce the threshold value.
30+
--exclude Filter expressions to exclude specific modules and types.
31+
--include Filter expressions to include specific modules and types.
32+
--include-directory Include directories containing additional assemblies to be instrumented.
33+
--exclude-by-file Glob patterns specifying source files to exclude.
34+
--exclude-by-attribute Attributes to exclude from code coverage.
35+
--include-test-assembly Specifies whether to report code coverage of the test assembly.
36+
--single-hit Specifies whether to limit code coverage hit reporting to a single hit for each location.
37+
--merge-with Path to existing coverage result to merge.
38+
--use-source-link Specifies whether to use SourceLink URIs in place of file system paths.
39+
```
40+
41+
## Code Coverage
42+
43+
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.
44+
45+
The following example shows how to use the familiar `dotnet test` toolchain:
46+
47+
```bash
48+
coverlet /path/to/test-assembly.dll --target "dotnet" --targetargs "test /path/to/test-project --no-build"
49+
```
50+
51+
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.
52+
53+
_Note: The `--no-build` flag is specified so that the `/path/to/test-assembly.dll` isn't rebuilt_
54+
55+
## Coverage Output
56+
57+
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`:
58+
59+
```bash
60+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --format opencover
61+
```
62+
63+
Supported Formats:
64+
65+
* json (default)
66+
* lcov
67+
* opencover
68+
* cobertura
69+
* teamcity
70+
71+
The `--format` option can be specified multiple times to output multiple formats in a single run:
72+
73+
```bash
74+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --format opencover --format lcov
75+
```
76+
77+
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.
78+
79+
```bash
80+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --output "/custom/path/result.json"
81+
```
82+
83+
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.
84+
85+
```bash
86+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --output "/custom/directory/" -f json -f lcov
87+
```
88+
89+
### TeamCity Output
90+
91+
Coverlet can output basic code coverage statistics using [TeamCity service messages](https://confluence.jetbrains.com/display/TCD18/Build+Script+Interaction+with+TeamCity#BuildScriptInteractionwithTeamCity-ServiceMessages).
92+
93+
```bash
94+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --output teamcity
95+
```
96+
97+
The currently supported [TeamCity statistics](https://confluence.jetbrains.com/display/TCD18/Build+Script+Interaction+with+TeamCity#BuildScriptInteractionwithTeamCity-ServiceMessages) are:
98+
99+
| TeamCity Statistic Key | Description |
100+
| :--- | :--- |
101+
| CodeCoverageL | Line-level code coverage |
102+
| CodeCoverageR | Branch-level code coverage |
103+
| CodeCoverageM | Method-level code coverage |
104+
| CodeCoverageAbsLTotal | The total number of lines |
105+
| CodeCoverageAbsLCovered | The number of covered lines |
106+
| CodeCoverageAbsRTotal | The total number of branches |
107+
| CodeCoverageAbsRCovered | The number of covered branches |
108+
| CodeCoverageAbsMTotal | The total number of methods |
109+
| CodeCoverageAbsMCovered | The number of covered methods |
110+
111+
## Merging Results
112+
113+
With Coverlet you can combine the output of multiple coverage runs into a single result.
114+
115+
```bash
116+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --merge-with "/path/to/result.json" --format opencover
117+
```
118+
119+
The value given to `--merge-with` **must** be a path to Coverlet's own json result format.
120+
121+
## Threshold
122+
123+
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.
124+
125+
```bash
126+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --threshold 80
127+
```
128+
129+
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:
130+
131+
```bash
132+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --threshold 80 --threshold-type line
133+
```
134+
135+
You can specify the `--threshold-type` option multiple times. Valid values include `line`, `branch` and `method`.
136+
137+
```bash
138+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --threshold 80 --threshold-type line --threshold-type method
139+
```
140+
141+
By default, Coverlet will validate the threshold value against the coverage result of each module. The `--threshold-stat` option allows you to change this behaviour and can have any of the following values:
142+
143+
* Minimum (Default): Ensures the coverage result of each module isn't less than the threshold
144+
* Total: Ensures the total combined coverage result of all modules isn't less than the threshold
145+
* Average: Ensures the average coverage result of all modules isn't less than the threshold
146+
147+
The following command will compare the threshold value with the overall total coverage of all modules:
148+
149+
```bash
150+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --threshold 80 --threshold-type line --threshold-stat total
151+
```
152+
153+
## Excluding From Coverage
154+
155+
### Attributes
156+
157+
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.
158+
159+
You can also ignore additional attributes by using the `ExcludeByAttribute` property (short name or full name supported):
160+
161+
```bash
162+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --exclude-by-attribute "Obsolete,GeneratedCodeAttribute,CompilerGeneratedAttribute"
163+
```
164+
165+
### Source Files
166+
167+
You can also ignore specific source files from code coverage using the `--exclude-by-file` option
168+
- Can be specified multiple times
169+
- Use absolute or relative paths (relative to the project directory)
170+
- Use file path or directory path with globbing (e.g `dir1/*.cs`)
171+
172+
```bash
173+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --exclude-by-file "../dir1/class1.cs"
174+
```
175+
176+
### Filters
177+
178+
Coverlet gives the ability to have fine grained control over what gets excluded using "filter expressions".
179+
180+
Syntax: `--exclude '[Assembly-Filter]Type-Filter'`
181+
182+
Wildcards
183+
- `*` => matches zero or more characters
184+
- `?` => the prefixed character is optional
185+
186+
Examples
187+
- `--exclude "[*]*"` => Excludes all types in all assemblies (nothing is instrumented)
188+
- `--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`)
189+
- `--exclude "[*]Coverlet.Core.Instrumentation.*"` => Excludes all types belonging to `Coverlet.Core.Instrumentation` namespace in any assembly
190+
- `--exclude "[coverlet.*.tests?]*"` => Excludes all types in any assembly starting with `coverlet.` and ending with `.test` or `.tests` (the `?` makes the `s` optional)
191+
- `--exclude "[coverlet.*]*" --exclude "[*]Coverlet.Core*"` => Excludes assemblies matching `coverlet.*` and excludes all types belonging to the `Coverlet.Core` namespace in any assembly
192+
193+
```bash
194+
coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --exclude "[coverlet.*]Coverlet.Core.Coverage"
195+
```
196+
197+
Coverlet goes a step in the other direction by also letting you explicitly set what can be included using the `--include` option.
198+
199+
Examples
200+
- `--include "[*]*"` => Includes all types in all assemblies (everything is instrumented)
201+
- `--include "[coverlet.*]Coverlet.Core.Coverage"` => Includes the Coverage class in the `Coverlet.Core` namespace belonging to any assembly that matches `coverlet.*` (e.g `coverlet.core`)
202+
- `--include "[coverlet.*.tests?]*"` => Includes all types in any assembly starting with `coverlet.` and ending with `.test` or `.tests` (the `?` makes the `s` optional)
203+
204+
Both `--exclude` and `--include` options can be used together but `--exclude` takes precedence. You can specify the `--exclude` and `--include` options multiple times to allow for multiple filter expressions.
205+
206+
You can also include coverage of the test assembly itself by specifying the `--include-test-assembly` flag.

0 commit comments

Comments
 (0)