From 5212cbe39a65d3d5db1140a020acf6d5b5eabde2 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Tue, 11 Jun 2024 10:32:27 -0700 Subject: [PATCH] Add Code coverage report (#79) Add code coverage report to pipeline. Fixes #42. Design is basically: 1. Capture code coverage files in `test.runsettings` to avoid embedding settings / logic in the pipeline YAML and make it possible to generate coverage locally the same as CI - Currently uses a simple assembly include limited to `Moq.Analyzers.dll`. Ideally we could say "all project references". Added a TODO for that, as well as to discuss with the code coverage team 3. Add `CleanCoverageReport` and `GenerateCoverageReport` targets in test MSBuild slice to handle merging the reports across test projects 4. Use the MSBuild task for `dotnet-reportgenerator` to merge code coverage reports into: - A markdown summary for GitHub - An HTML report with line-coverage info - A merged Cobertura XML report for tool processing 5. Update GitHub Actions YAML so that: - Markdown summary is added to the summary of the run - Other coverage reports are added as a build artifact I didn't add any functionality to fail below a certain threshold, as tools generally use a static threshold, or do odd things to smuggle state like creating a branch in the repo or storing information in a gist, so leaving that for later. --- .github/workflows/main.yml | 27 +++++++++++++++++++-------- build/targets/tests/Packages.props | 1 + build/targets/tests/Tests.props | 1 + build/targets/tests/Tests.targets | 23 +++++++++++++++++++++++ build/targets/tests/test.runsettings | 28 ++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 build/targets/tests/test.runsettings diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 63deb4cd..699f7e16 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -56,9 +56,17 @@ jobs: run: dotnet build --no-restore --configuration Release /bl:./artifacts/logs/release/build.release.binlog - name: Test - run: dotnet test --no-build --configuration Release + run: dotnet test --no-build --configuration Release --settings ./build/targets/tests/test.runsettings - - name: Publish Test Report + - name: Upload *.received.* files + uses: actions/upload-artifact@v4 + if: failure() + with: + name: verify-test-results + path: | + **/*.received.* + + - name: Upload Test Report uses: actions/upload-artifact@v4 if: success() || failure() with: @@ -66,13 +74,16 @@ jobs: path: "artifacts/TestResults/**/*.trx" if-no-files-found: error - - name: Upload Test Results - uses: actions/upload-artifact@v2 - if: failure() + - name: Upload Code Coverage Report + uses: actions/upload-artifact@v4 + if: success() || failure() with: - name: verify-test-results - path: | - **/*.received.* + name: .NET Code Coverage Reports (${{ matrix.os }}) + path: "artifacts/TestResults/coverage/**" + + - name: Publish coverage summary + run: cat artifacts/TestResults/coverage/SummaryGithub.md >> $GITHUB_STEP_SUMMARY + shell: bash - name: Upload binlogs uses: actions/upload-artifact@v4 diff --git a/build/targets/tests/Packages.props b/build/targets/tests/Packages.props index d6d77013..fc5e9f1d 100644 --- a/build/targets/tests/Packages.props +++ b/build/targets/tests/Packages.props @@ -1,5 +1,6 @@ + diff --git a/build/targets/tests/Tests.props b/build/targets/tests/Tests.props index 80693a59..9a32918b 100644 --- a/build/targets/tests/Tests.props +++ b/build/targets/tests/Tests.props @@ -1,5 +1,6 @@ + diff --git a/build/targets/tests/Tests.targets b/build/targets/tests/Tests.targets index b3078335..49bc6c6e 100644 --- a/build/targets/tests/Tests.targets +++ b/build/targets/tests/Tests.targets @@ -3,5 +3,28 @@ trx%3bLogFileName=$(MSBuildProjectName).trx $(ArtifactsTestResultsPath)/$(TargetFramework) + + <_TestCoverageGlob>$(ArtifactsTestResultsPath)/**/*.cobertura.xml + <_TestCoverageReportDirectory>$(ArtifactsTestResultsPath)/coverage + + + + + + + <_CoverageFiles Include="$(_TestCoverageGlob)" /> + + + + <_CoverageFiles Remove="@(_CoverageFiles)" /> + + + + + + <_CoverageFiles Include="$(_TestCoverageGlob)" /> + + + diff --git a/build/targets/tests/test.runsettings b/build/targets/tests/test.runsettings new file mode 100644 index 00000000..13545c08 --- /dev/null +++ b/build/targets/tests/test.runsettings @@ -0,0 +1,28 @@ + + + + + + + + cobertura + False + + False + False + + + + .*Moq\.Analyzers\.dll$ + + + + + + + + \ No newline at end of file