Skip to content

Updated to show test coverage results in browser after pipeline run. #5

Updated to show test coverage results in browser after pipeline run.

Updated to show test coverage results in browser after pipeline run. #5

name: Build and test API on push or PR
#asdf
#controls when the workflow is triggered. Works on push or pull request to main branch
on:
push:
branches:
- main
pull_request:
branches:
- main
# specify the permissions for the workflow
permissions:
contents: read #needed to read the repository content
checks: write #needed to post check run results
pull-requests: write #needed to post PR comments for test results
# workflow is made of one or more jobs that can be run sequentially or in parallel
jobs:
run-tests:
name: Run Tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: "8.x"
- name: Restore dependencies
run: dotnet restore
- name: Run tests with coverage
run: |
mkdir -p test-results
mkdir -p coverage
dotnet test --logger "trx;LogFileName=test-results.trx" --results-directory test-results --collect:"XPlat Code Coverage" --results-directory coverage
continue-on-error: true
- name: Display code coverage summary
run: |
echo "Code Coverage Results:"
find coverage -name "coverage.cobertura.xml" -exec echo "Coverage file: {}" \;
# Install reportgenerator tool to create summary
dotnet tool install -g dotnet-reportgenerator-globaltool
if [ -f coverage/*/coverage.cobertura.xml ]; then
reportgenerator -reports:coverage/*/coverage.cobertura.xml -targetdir:coverage/report -reporttypes:TextSummary
cat coverage/report/Summary.txt
else
echo "No coverage files found"
fi
- name: Publish Test Results
uses: dorny/test-reporter@v1
if: always()
with:
name: .NET Tests
path: test-results/*.trx
reporter: dotnet-trx
- name: Code Coverage Report
uses: irongut/CodeCoverageSummary@v1.3.0
if: always()
with:
filename: coverage/*/coverage.cobertura.xml
badge: true
fail_below_min: false
format: markdown
hide_branch_rate: false
hide_complexity: true
indicators: true
output: both
thresholds: "60 80"
- name: Add Coverage PR Comment
uses: marocchino/sticky-pull-request-comment@v2
if: github.event_name == 'pull_request'
with:
recreate: true
path: code-coverage-results.md
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
with:
files: coverage/*/coverage.cobertura.xml
fail_ci_if_error: true