diff --git a/README.md b/README.md index c91697447..89720fbeb 100644 --- a/README.md +++ b/README.md @@ -4,15 +4,13 @@ Coverlet is a cross platform code coverage library for .NET Core, with support f ## Installation -Available on [NuGet](https://www.nuget.org/packages/coverlet.msbuild/) +**Global Tool**: -Visual Studio: - -```powershell -PM> Install-Package coverlet.msbuild +```bash +dotnet tool install --global coverlet.console ``` -.NET Core CLI: +**Package Reference**: ```bash dotnet add package coverlet.msbuild @@ -20,7 +18,7 @@ dotnet add package coverlet.msbuild ## How It Works -Coverlet integrates with the MSBuild system and that allows it to go through the following process: +Coverlet generates code coverage information by going through the following process: ### Before Tests Run @@ -37,9 +35,151 @@ _Note: The assembly you'd like to get coverage for must be different from the as ## Usage -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. +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. + +### Global Tool + +To see a list of options, run: + +```bash +coverlet --help +``` + +The current options are (output of `coverlet --help`): + +```bash +Cross platform .NET Core code coverage tool 1.0.0.0 + +Usage: coverlet [arguments] [options] + +Arguments: + Path to the test assembly. + +Options: + -h|--help Show help information + -v|--version Show version information + -t|--target Path to the test runner application. + -a|--targetargs Arguments to be passed to the test runner. + -o|--output Output of the generated coverage report + -f|--format Format of the generated coverage report. + --threshold Exits with error if the coverage % is below value. + --threshold-type Coverage type to apply the threshold to. + --exclude Filter expressions to exclude specific modules and types. + --exclude-by-file Glob patterns specifying source files to exclude. +``` + +#### Code Coverage + +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. + +The following example shows how to use the familiar `dotnet test` toolchain: + +```bash +coverlet /path/to/test-assembly.dll --target "dotnet" --targetargs "test /path/to/test-project --no-build" +``` + +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. + +_Note: The `--no-build` flag is specified so that the `/path/to/test-assembly.dll` isn't rebuilt_ + +#### Coverage Output + +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`: + +```bash +coverlet --target --targetargs --format opencover +``` + +Supported Formats: + +* json (default) +* lcov +* opencover +* cobertura + +The `--format` option can be specified multiple times to output multiple formats in a single run: + +```bash +coverlet --target --targetargs --format opencover --format lcov +``` + +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. + +```bash +coverlet --target --targetargs --output "/custom/path/result.json" +``` + +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. + +```bash +coverlet --target --targetargs --output "/custom/directory/" -f json -f lcov +``` + +#### Threshold + +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. + +```bash +coverlet --target --targetargs --threshold 80 +``` + +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: + +```bash +coverlet --target --targetargs --threshold 80 --threshold-type line +``` + +You can specify the `--threshold-type` option multiple times. Valid values include `line`, `branch` and `method`. + +```bash +coverlet --target --targetargs --threshold 80 --threshold-type line --threshold-type method +``` + +#### Excluding From Coverage + +##### Attributes + +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. + +##### Source Files + +You can also ignore specific source files from code coverage using the `--exclude-by-file` option + - Can be specified multiple times + - Use absolute or relative paths (relative to the project directory) + - Use file path or directory path with globbing (e.g `dir1/*.cs`) + +```bash +coverlet --target --targetargs --exclude-by-file "../dir1/class1.cs" +``` + +##### Filters + +Coverlet gives the ability to have fine grained control over what gets excluded using "filter expressions". + +Syntax: `--exclude '[Assembly-Filter]Type-Filter'` + +Wildcards +- `*` => matches zero or more characters +- `?` => the prefixed character is optional + +Examples + - `--exclude "[*]*"` => Excludes all types in all assemblies (nothing is instrumented) + - `--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`) + - `--exclude "[*]Coverlet.Core.Instrumentation.*"` => Excludes all types belonging to `Coverlet.Core.Instrumentation` namespace in any assembly + - `--exclude "[coverlet.*.tests?]*"` => Excludes all types in any assembly starting with `coverlet.` and ending with `.test` or `.tests` (the `?` makes the `s` optional) + - `--exclude "[coverlet.*]*" --exclude "[*]Coverlet.Core*"` => Excludes assemblies matching `coverlet.*` and excludes all types belonging to the `Coverlet.Core` namespace in any assembly + +```bash +coverlet --target --targetargs --exclude "[coverlet.*]Coverlet.Core.Coverage" +``` + +You can specify the `--exclude` option multiple times to allow for multiple filter expressions. + +### MSBuild + +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. -### Code Coverage +#### Code Coverage Enabling code coverage is as simple as setting the `CollectCoverage` property to `true` @@ -49,7 +189,7 @@ dotnet test /p:CollectCoverage=true 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. -### Coverage Output +#### Coverage Output 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: @@ -78,7 +218,7 @@ To specify a directory where all results will be written to (especially if using dotnet test /p:CollectCoverage=true /p:CoverletOutput='./results/' ``` -### Threshold +#### Threshold 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. @@ -94,9 +234,9 @@ dotnet test /p:CollectCoverage=true /p:Threshold=80 /p:ThresholdType=line You can specify multiple values for `ThresholdType` by separating them with commas. Valid values include `line`, `branch` and `method`. -### Excluding From Coverage +#### Excluding From Coverage -#### Attributes +##### Attributes 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. @@ -110,7 +250,7 @@ You can also ignore specific source files from code coverage using the `ExcludeB dotnet test /p:CollectCoverage=true /p:ExcludeByFile=\"../dir1/class1.cs,../dir2/*.cs,../dir3/**/*.cs,\" ``` -#### Filters +##### Filters Coverlet gives the ability to have fine grained control over what gets excluded using "filter expressions". Syntax: `/p:Exclude=[Assembly-Filter]Type-Filter`