Skip to content

Commit 1f8ebee

Browse files
aaronpowellstephentoubeiriktsarpalis
authored
Adding code coverage to tests in workflow (#75)
* Adding code coverage to tests in workflow This is using the pattern from the Aspire Community Toolkit which will publish coverage results to the GitHub Actions summary, as well as a comment on the PR (if the job was run by someone with permissions to write comments). Also added the GitHubActionsTestLogger so that it writes a nicer log out to the run. Fixes #11 * Feedback from code review * Slight change to structure * Bad indentation * Ensuring .NET SDK is installed * Disabling fail-fast so that jobs aren't terminated if one fails * Forgot to include coverlet.collector * Update Directory.Packages.props --------- Co-authored-by: Stephen Toub <[email protected]> Co-authored-by: Eirik Tsarpalis <[email protected]>
1 parent ed079b9 commit 1f8ebee

File tree

5 files changed

+251
-140
lines changed

5 files changed

+251
-140
lines changed

.github/workflows/ci.yml

+28-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ jobs:
1515
matrix:
1616
os: [ubuntu-latest, windows-latest, macos-latest]
1717
configuration: [Debug, Release]
18+
fail-fast: false
1819

1920
steps:
2021
- uses: actions/checkout@v4
2122
with:
2223
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
2324

2425
- name: Setup .NET
25-
uses: actions/setup-dotnet@v2
26+
uses: actions/setup-dotnet@v4
2627
with:
2728
dotnet-version: |
2829
9.0.x
@@ -48,4 +49,29 @@ jobs:
4849
run: dotnet build --configuration ${{ matrix.configuration }}
4950

5051
- name: Test
51-
run: dotnet test --filter '(Execution!=Manual)' --no-build --configuration ${{ matrix.configuration }}
52+
run: >-
53+
dotnet test
54+
--filter '(Execution!=Manual)'
55+
--no-build
56+
--configuration ${{ matrix.configuration }}
57+
--logger "console;verbosity=normal"
58+
--logger "trx"
59+
--logger "GitHubActions;summary.includePassedTests=true;summary.includeSkippedTests=true"
60+
--blame
61+
--blame-hang-timeout 7m
62+
--blame-crash
63+
--results-directory testresults
64+
--collect "XPlat Code Coverage" -- RunConfiguration.CollectSourceInformation=true
65+
66+
- name: Upload test results artifact
67+
if: always()
68+
uses: actions/upload-artifact@v4
69+
with:
70+
name: testresults-${{ matrix.os }}-${{ matrix.configuration }}
71+
path: testresults/**
72+
73+
publish-coverage:
74+
if: github.actor != 'dependabot[bot]'
75+
needs: build
76+
uses: ./.github/workflows/code-coverage.yml
77+
secrets: inherit

.github/workflows/code-coverage.yml

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Code Coverage
2+
3+
on:
4+
workflow_call:
5+
6+
jobs:
7+
publish-coverage:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v4
11+
- name: Setup .NET
12+
uses: actions/setup-dotnet@v4
13+
with:
14+
dotnet-version: |
15+
9.0.x
16+
8.0.x
17+
18+
- name: Download test results
19+
uses: actions/download-artifact@v4
20+
with:
21+
pattern: testresults-*
22+
23+
- name: Combine coverage reports
24+
uses: danielpalme/[email protected]
25+
with:
26+
reports: "**/*.cobertura.xml"
27+
targetdir: "${{ github.workspace }}/report"
28+
reporttypes: "HtmlSummary;Cobertura;MarkdownSummary;MarkdownSummaryGithub"
29+
verbosity: "Info"
30+
title: "Code Coverage"
31+
tag: "${{ github.run_number }}_${{ github.run_id }}"
32+
customSettings: ""
33+
toolpath: "reportgeneratortool"
34+
35+
- name: Upload combined coverage XML
36+
uses: actions/upload-artifact@v4
37+
with:
38+
name: coverage
39+
path: ${{ github.workspace }}/report
40+
retention-days: 7
41+
42+
- name: Publish code coverage report
43+
uses: irongut/[email protected]
44+
with:
45+
filename: "report/Cobertura.xml"
46+
badge: true
47+
fail_below_min: true
48+
format: markdown
49+
hide_branch_rate: false
50+
hide_complexity: false
51+
indicators: true
52+
output: both
53+
thresholds: "60 80"
54+
55+
- name: Upload combined coverage markdown
56+
uses: actions/upload-artifact@v4
57+
with:
58+
name: coverage-markdown
59+
path: ${{ github.workspace }}/code-coverage-results.md
60+
retention-days: 7
61+
62+
- name: Add Coverage PR Comment
63+
uses: marocchino/sticky-pull-request-comment@v2
64+
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
65+
with:
66+
recreate: true
67+
path: ${{ github.workspace }}/code-coverage-results.md
68+
69+
- name: Coverage on step summary
70+
if: always()
71+
run: cat "${{ github.workspace }}/report/SummaryGithub.md" >> $GITHUB_STEP_SUMMARY

Directory.Packages.props

+44-39
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
1-
<Project>
2-
<PropertyGroup>
3-
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
4-
<SystemVersion>9.0.3</SystemVersion>
5-
<System10Version>10.0.0-preview.2.25163.2</System10Version>
6-
<MicrosoftExtensionsVersion>9.0.3</MicrosoftExtensionsVersion>
7-
<MicrosoftExtensionsAIVersion>9.3.0-preview.1.25161.3</MicrosoftExtensionsAIVersion>
8-
</PropertyGroup>
9-
<ItemGroup>
10-
<!-- Product dependencies -->
11-
<PackageVersion Include="Microsoft.Bcl.Memory" Version="$(SystemVersion)" />
12-
<PackageVersion Include="Microsoft.Extensions.AI" Version="$(MicrosoftExtensionsAIVersion)" />
13-
<PackageVersion Include="Microsoft.Extensions.AI.Abstractions" Version="$(MicrosoftExtensionsAIVersion)" />
14-
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="$(MicrosoftExtensionsVersion)" />
15-
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsVersion)" />
16-
<PackageVersion Include="System.Net.ServerSentEvents" Version="$(System10Version)" />
17-
<PackageVersion Include="System.Text.Json" Version="$(SystemVersion)" />
18-
<PackageVersion Include="System.Threading.Channels" Version="$(SystemVersion)" />
19-
20-
<!-- Build Infra & Packaging -->
21-
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
22-
23-
<!-- Testing dependencies -->
24-
<PackageVersion Include="Anthropic.SDK" Version="4.7.1" />
25-
<PackageVersion Include="Microsoft.Extensions.AI.OpenAI" Version="$(MicrosoftExtensionsAIVersion)" />
26-
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="$(MicrosoftExtensionsVersion)" />
27-
<PackageVersion Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsVersion)" />
28-
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsVersion)" />
29-
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
30-
<PackageVersion Include="Moq" Version="4.20.72" />
31-
<PackageVersion Include="Serilog.Extensions.Hosting" Version="9.0.0" />
32-
<PackageVersion Include="Serilog.Extensions.Logging" Version="9.0.0" />
33-
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
34-
<PackageVersion Include="Serilog.Sinks.Debug" Version="3.0.0" />
35-
<PackageVersion Include="Serilog.Sinks.File" Version="6.0.0" />
36-
<PackageVersion Include="System.Linq.AsyncEnumerable" Version="$(System10Version)" />
37-
<PackageVersion Include="xunit.v3" Version="1.1.0" />
38-
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.2" />
39-
</ItemGroup>
1+
<Project>
2+
<PropertyGroup>
3+
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
4+
<SystemVersion>9.0.3</SystemVersion>
5+
<System10Version>10.0.0-preview.2.25163.2</System10Version>
6+
<MicrosoftExtensionsVersion>9.0.3</MicrosoftExtensionsVersion>
7+
<MicrosoftExtensionsAIVersion>9.3.0-preview.1.25161.3</MicrosoftExtensionsAIVersion>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<!-- Product dependencies -->
11+
<PackageVersion Include="coverlet.collector" Version="6.0.4">
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
<PrivateAssets>all</PrivateAssets>
14+
</PackageVersion>
15+
<PackageVersion Include="Microsoft.Bcl.Memory" Version="$(SystemVersion)" />
16+
<PackageVersion Include="Microsoft.Extensions.AI" Version="$(MicrosoftExtensionsAIVersion)" />
17+
<PackageVersion Include="Microsoft.Extensions.AI.Abstractions" Version="$(MicrosoftExtensionsAIVersion)" />
18+
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="$(MicrosoftExtensionsVersion)" />
19+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsVersion)" />
20+
<PackageVersion Include="System.Net.ServerSentEvents" Version="$(System10Version)" />
21+
<PackageVersion Include="System.Text.Json" Version="$(SystemVersion)" />
22+
<PackageVersion Include="System.Threading.Channels" Version="$(SystemVersion)" />
23+
24+
<!-- Build Infra & Packaging -->
25+
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
26+
27+
<!-- Testing dependencies -->
28+
<PackageVersion Include="Anthropic.SDK" Version="4.7.1" />
29+
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1" />
30+
<PackageVersion Include="Microsoft.Extensions.AI.OpenAI" Version="$(MicrosoftExtensionsAIVersion)" />
31+
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="$(MicrosoftExtensionsVersion)" />
32+
<PackageVersion Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsVersion)" />
33+
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsVersion)" />
34+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
35+
<PackageVersion Include="Moq" Version="4.20.72" />
36+
<PackageVersion Include="Serilog.Extensions.Hosting" Version="9.0.0" />
37+
<PackageVersion Include="Serilog.Extensions.Logging" Version="9.0.0" />
38+
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
39+
<PackageVersion Include="Serilog.Sinks.Debug" Version="3.0.0" />
40+
<PackageVersion Include="Serilog.Sinks.File" Version="6.0.0" />
41+
<PackageVersion Include="System.Linq.AsyncEnumerable" Version="$(System10Version)" />
42+
<PackageVersion Include="xunit.v3" Version="1.1.0" />
43+
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.2" />
44+
</ItemGroup>
4045
</Project>

0 commit comments

Comments
 (0)