Skip to content

Commit

Permalink
Merge pull request #48 from jakubch1/main
Browse files Browse the repository at this point in the history
Add scenario 04 - update previous scenarios with github summary
  • Loading branch information
jakubch1 committed Aug 31, 2023
2 parents 4e5c6d9 + 308d3dc commit 2e593c3
Show file tree
Hide file tree
Showing 13 changed files with 397 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/Calculator_Scenario01.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ jobs:
run: dotnet build --no-restore
- name: Test
run: dotnet test --collect "Code Coverage" --no-build --verbosity normal
- name: Install dotnet-coverage
run: dotnet tool install -g dotnet-coverage
- name: Convert .coverage report to cobertura
run: dotnet-coverage merge -r $GITHUB_WORKSPACE/samples/Calculator/tests/Calculator.Core.Tests/TestResults/*.coverage -f cobertura -o $GITHUB_WORKSPACE/report.cobertura.xml
- name: ReportGenerator
uses: danielpalme/ReportGenerator-GitHub-Action@5.1.24
with:
reports: '${{ github.workspace }}/report.cobertura.xml'
targetdir: '${{ github.workspace }}/coveragereport'
reporttypes: 'MarkdownSummaryGithub'
- name: Upload coverage into summary
run: cat $GITHUB_WORKSPACE/coveragereport/SummaryGithub.md >> $GITHUB_STEP_SUMMARY
- name: Archive code coverage results
uses: actions/upload-artifact@v3
with:
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/Calculator_Scenario02.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ jobs:
run: dotnet build --no-restore
- name: Test
run: dotnet test --collect "Code Coverage;Format=cobertura" --no-build --verbosity normal
- name: ReportGenerator
uses: danielpalme/ReportGenerator-GitHub-Action@5.1.24
with:
reports: './**/TestResults/**/*.cobertura.xml'
targetdir: '${{ github.workspace }}/coveragereport'
reporttypes: 'MarkdownSummaryGithub'
- name: Upload coverage into summary
run: cat $GITHUB_WORKSPACE/coveragereport/SummaryGithub.md >> $GITHUB_STEP_SUMMARY
- name: Archive code coverage results
uses: actions/upload-artifact@v3
with:
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/Calculator_Scenario03.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ jobs:
run: dotnet build --no-restore
- name: Test
run: dotnet test --settings ../../scenarios/scenario03/coverage.runsettings --no-build --verbosity normal
- name: ReportGenerator
uses: danielpalme/ReportGenerator-GitHub-Action@5.1.24
with:
reports: './**/TestResults/**/*.cobertura.xml'
targetdir: '${{ github.workspace }}/coveragereport'
reporttypes: 'MarkdownSummaryGithub'
- name: Upload coverage into summary
run: cat $GITHUB_WORKSPACE/coveragereport/SummaryGithub.md >> $GITHUB_STEP_SUMMARY
- name: Archive code coverage results
uses: actions/upload-artifact@v3
with:
Expand Down
42 changes: 42 additions & 0 deletions .github/workflows/Calculator_Scenario04.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: "Calculator Scenario 04"

on:
push:
branches: [ "main" ]
paths: [ 'samples/Calculator/tests/**', 'samples/Calculator/src/**', '.github/workflows/Calculator_Scenario04.yml' ]

jobs:
build:

runs-on: ubuntu-latest
defaults:
run:
working-directory: ./samples/Calculator/tests/Calculator.Core.Tests
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --collect "Code Coverage;Format=xml" --no-build --verbosity normal
- name: ReportGenerator
uses: danielpalme/ReportGenerator-GitHub-Action@5.1.24
with:
reports: './**/TestResults/**/*.xml'
targetdir: '${{ github.workspace }}/coveragereport'
reporttypes: 'MarkdownSummaryGithub'
- name: Upload coverage into summary
run: cat $GITHUB_WORKSPACE/coveragereport/SummaryGithub.md >> $GITHUB_STEP_SUMMARY
- name: Upload raw coverage report artifact
uses: actions/upload-artifact@v3
with:
name: RawCoverageReport
path: ./**/TestResults/**/*.xml
14 changes: 14 additions & 0 deletions samples/Calculator/scenarios/scenario01/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ You can also use [run.ps1](run.ps1) to collect code coverage.

# Collect code coverage inside github workflow

To generate summary report `.coverage` report needs to be converted to `cobertura` report using `dotnet-coverage` tool. Then `reportgenerator` can be used to generate final github summary markdown.

```shell
steps:
- uses: actions/checkout@v3
Expand All @@ -27,6 +29,18 @@ You can also use [run.ps1](run.ps1) to collect code coverage.
run: dotnet build --no-restore
- name: Test
run: dotnet test --collect "Code Coverage" --no-build --verbosity normal
- name: Install dotnet-coverage
run: dotnet tool install -g dotnet-coverage
- name: Convert .coverage report to cobertura
run: dotnet-coverage merge -r $GITHUB_WORKSPACE/samples/Calculator/tests/Calculator.Core.Tests/TestResults/*.coverage -f cobertura -o $GITHUB_WORKSPACE/report.cobertura.xml
- name: ReportGenerator
uses: danielpalme/ReportGenerator-GitHub-Action@5.1.24
with:
reports: '${{ github.workspace }}/report.cobertura.xml'
targetdir: '${{ github.workspace }}/coveragereport'
reporttypes: 'MarkdownSummaryGithub'
- name: Upload coverage into summary
run: cat $GITHUB_WORKSPACE/coveragereport/SummaryGithub.md >> $GITHUB_STEP_SUMMARY
- name: Archive code coverage results
uses: actions/upload-artifact@v3
with:
Expand Down
8 changes: 8 additions & 0 deletions samples/Calculator/scenarios/scenario02/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ You can also use [run.ps1](run.ps1) to collect code coverage.
run: dotnet build --no-restore
- name: Test
run: dotnet test --collect "Code Coverage;Format=cobertura" --no-build --verbosity normal
- name: ReportGenerator
uses: danielpalme/ReportGenerator-GitHub-Action@5.1.24
with:
reports: './**/TestResults/**/*.cobertura.xml'
targetdir: '${{ github.workspace }}/coveragereport'
reporttypes: 'MarkdownSummaryGithub'
- name: Upload coverage into summary
run: cat $GITHUB_WORKSPACE/coveragereport/SummaryGithub.md >> $GITHUB_STEP_SUMMARY
- name: Archive code coverage results
uses: actions/upload-artifact@v3
with:
Expand Down
8 changes: 8 additions & 0 deletions samples/Calculator/scenarios/scenario03/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ You can also use [run.ps1](run.ps1) to collect code coverage.
run: dotnet build --no-restore
- name: Test
run: dotnet test --settings ../../scenarios/scenario03/coverage.runsettings --no-build --verbosity normal
- name: ReportGenerator
uses: danielpalme/ReportGenerator-GitHub-Action@5.1.24
with:
reports: './**/TestResults/**/*.cobertura.xml'
targetdir: '${{ github.workspace }}/coveragereport'
reporttypes: 'MarkdownSummaryGithub'
- name: Upload coverage into summary
run: cat $GITHUB_WORKSPACE/coveragereport/SummaryGithub.md >> $GITHUB_STEP_SUMMARY
- name: Archive code coverage results
uses: actions/upload-artifact@v3
with:
Expand Down
95 changes: 95 additions & 0 deletions samples/Calculator/scenarios/scenario04/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Scenario Description

Collect code coverage with XML report format.

# Collect code coverage using command line

```shell
git clone https://github.com/microsoft/codecoverage.git
cd codecoverage/samples/Calculator/tests/Calculator.Core.Tests/
dotnet test --collect "Code Coverage;Format=xml"
```

You can also use [run.ps1](run.ps1) to collect code coverage.

# Collect code coverage inside github workflow

```shell
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --collect "Code Coverage;Format=xml" --no-build --verbosity normal
- name: ReportGenerator
uses: danielpalme/ReportGenerator-GitHub-Action@5.1.24
with:
reports: './**/TestResults/**/*.xml'
targetdir: '${{ github.workspace }}/coveragereport'
reporttypes: 'MarkdownSummaryGithub'
- name: Upload coverage into summary
run: cat $GITHUB_WORKSPACE/coveragereport/SummaryGithub.md >> $GITHUB_STEP_SUMMARY
- name: Upload raw coverage report artifact
uses: actions/upload-artifact@v3
with:
name: RawCoverageReport
path: ./**/TestResults/**/*.xml
```

[Full source example](../../../../.github/workflows/Calculator_Scenario04.yml)

[Run example](../../../../../../actions/workflows/Calculator_Scenario04.yml)

# Collect code coverage inside Azure DevOps Pipelines

```shell
steps:
- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '$(projectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet restore'

- task: DotNetCoreCLI@2
inputs:
command: 'build'
arguments: '--no-restore --configuration $(buildConfiguration)'
projects: '$(projectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet build'

- task: DotNetCoreCLI@2
inputs:
command: 'test'
arguments: '--no-build --configuration $(buildConfiguration) --collect "Code Coverage;Format=xml" --logger trx --results-directory $(Agent.TempDirectory)/TestResults'
publishTestResults: false
projects: '$(projectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet test'

- task: PublishTestResults@2
inputs:
testResultsFormat: 'VSTest'
testResultsFiles: '$(Agent.TempDirectory)/TestResults/**/*.trx'
publishRunAttachments: false

- task: PublishCodeCoverageResults@2
inputs:
summaryFileLocation: $(Agent.TempDirectory)/TestResults/**/*.xml
```

> **_NOTE:_** To make sure that Code Coverage tab will be visible in Azure DevOps you need to make sure that previous steps will not publish test attachments (`publishRunAttachments: false` and `publishTestResults: false`).
[Full source example](azure-pipelines.yml)

![alt text](azure-pipelines.jpg "Code Coverage tab in Azure DevOps pipelines")

# Report example

![alt text](example.report.jpg "Example report")

[Link](example.report.xml)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions samples/Calculator/scenarios/scenario04/azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: "Calculator Scenario 04"

pool:
vmImage: 'ubuntu-latest'

variables:
buildConfiguration: 'Debug'
projectPath: './samples/Calculator/tests/Calculator.Core.Tests/Calculator.Core.Tests.csproj' # this is specific to example - in most cases not needed

steps:
- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '$(projectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet restore'

- task: DotNetCoreCLI@2
inputs:
command: 'build'
arguments: '--no-restore --configuration $(buildConfiguration)'
projects: '$(projectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet build'

- task: DotNetCoreCLI@2
inputs:
command: 'test'
arguments: '--no-build --configuration $(buildConfiguration) --collect "Code Coverage;Format=xml" --logger trx --results-directory $(Agent.TempDirectory)/TestResults'
publishTestResults: false
projects: '$(projectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet test'

- task: PublishTestResults@2
inputs:
testResultsFormat: 'VSTest'
testResultsFiles: '$(Agent.TempDirectory)/TestResults/**/*.trx'
publishRunAttachments: false

- task: PublishCodeCoverageResults@2
inputs:
summaryFileLocation: $(Agent.TempDirectory)/TestResults/**/*.xml
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2e593c3

Please sign in to comment.