Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New article that details unit test code coverage using coverlet #18955

Merged
merged 17 commits into from
Jun 17, 2020
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: Tom Dykstra <tdykstra@microsoft.com>
  • Loading branch information
IEvangelist and tdykstra authored Jun 15, 2020
commit e7e9a796a363ec3c77cafe32d0678f2d92ce8fa2
4 changes: 2 additions & 2 deletions docs/core/testing/unit-testing-best-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ Writing tests for your code will naturally decouple your code, because it would

## Code coverage

A high code coverage percentage is often associated with a higher quality of code, however; the measurement itself *cannot* determine the quality of code. Setting an ambiguous code coverage percentage goal is counterintuitive. Imagine a complex project with thousands of conditional branches, and imagine that it requires 95% code coverage. Currently the project maintains 90% code coverage. The amount of time it takes to account for all of the edge cases in the remaining 5% could be a massive undertaking and the value proposition quickly diminishes.
A high code coverage percentage is often associated with a higher quality of code. However, the measurement itself *cannot* determine the quality of code. Setting an overly ambitious code coverage percentage goal can be counterproductive. Imagine a complex project with thousands of conditional branches, and imagine that you set a goal of 95% code coverage. Currently the project maintains 90% code coverage. The amount of time it takes to account for all of the edge cases in the remaining 5% could be a massive undertaking, and the value proposition quickly diminishes.

Using code coverage for what it truly represents is always best. It is not an indicator of success, nor does it imply a high code quality - instead it is simply the amount of code that is covered by unit tests. For more information, see [unit testing code coverage](unit-testing-code-coverage.md).
A high code coverage percentage is not an indicator of success, nor does it imply high code quality. It jusst represents the amount of code that is covered by unit tests. For more information, see [unit testing code coverage](unit-testing-code-coverage.md).

## Let's speak the same language
The term *mock* is unfortunately very misused when talking about testing. The following defines the most common types of *fakes* when writing unit tests:
Expand Down
10 changes: 5 additions & 5 deletions docs/core/testing/unit-testing-code-coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ ms.date: 06/15/2020

# Use code coverage for unit testing

Unit tests help to ensure functionality, and provide a means of verification for refactoring efforts. Code coverage is a measurement of the amount of code that is ran as part of a unit test run - either lines, branches, or methods. As an example, if you have a simple application with only two conditional branches of code (_branch a_, and _branch b_), a unit test that verifies conditional _branch a_ will report branch code coverage of 50%.
Unit tests help to ensure functionality, and provide a means of verification for refactoring efforts. Code coverage is a measurement of the amount of code that is run by unit tests - either lines, branches, or methods. As an example, if you have a simple application with only two conditional branches of code (_branch a_, and _branch b_), a unit test that verifies conditional _branch a_ will report branch code coverage of 50%.

This article discusses the usage of code coverage for unit testing with coverlet, and it is [open source project on GitHub](https://github.com/coverlet-coverage/coverlet). Coverlet is a cross platform code coverage framework for .NET. Additionally, [coverlet](https://dotnetfoundation.org/projects/coverlet) is part of the .NET foundation.
This article discusses the usage of code coverage for unit testing with coverlet. Coverlet is an [open source project on GitHub](https://github.com/coverlet-coverage/coverlet) that provides a cross platform code coverage framework for .NET. [Coverlet](https://dotnetfoundation.org/projects/coverlet) is part of the .NET foundation.

## Tooling

Expand All @@ -31,13 +31,13 @@ dotnet test --collect:"XPlat Code Coverage"
```

> [!IMPORTANT]
> The `"XPlat Code Coverage"` argument is a friendly name that corresponds to the data collectors from coverlet. This name is required, but is case insensitive.
> The `"XPlat Code Coverage"` argument is a friendly name that corresponds to the data collectors from coverlet. This name is required but is case insensitive.

As part of the `dotnet test` run, a resulting *TestResults/{test-run-guid}/coverage.cobertura.xml* file is output. The XML file contains the results.
IEvangelist marked this conversation as resolved.
Show resolved Hide resolved

#### MSBuild

As an alternative, you could use the MSBuild package. From the .NET Core CLI at the directory level of the *.csproj* file, run the following [`dotnet add package`](../tools/dotnet-add-package.md) command:
As an alternative, you could use the MSBuild package. From a command prompt in the project directory, run the following [`dotnet add package`](../tools/dotnet-add-package.md) command:

```dotnetcli
dotnet add package coverlet.msbuild
Expand All @@ -60,7 +60,7 @@ To use coverlet globally, it can be installed as a [.NET global tool](../tools/g
dotnet tool install --global coverlet.console
```

The [coverlet.console](https://www.nuget.org/packages/coverlet.console) NuGet package is added to the environment globally. Now, `coverlet` can be used.
The [coverlet.console](https://www.nuget.org/packages/coverlet.console) NuGet package is added to the environment globally. Now, `coverlet` can be used as a CLI command:

```console
coverlet C:\Source\Directory\bin\Debug\netcoreapp3.1\Test.dll --target "dotnet" --targetargs "test . --no-build"
IEvangelist marked this conversation as resolved.
Show resolved Hide resolved
Expand Down