Skip to content

Commit

Permalink
Merge pull request #77 from jakubch1/main
Browse files Browse the repository at this point in the history
Scenario 25
  • Loading branch information
jakubch1 committed Jan 2, 2024
2 parents b79887d + 3c585ca commit ecaf850
Show file tree
Hide file tree
Showing 9 changed files with 669 additions and 2 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/Calculator_Scenario25.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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 25"

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

jobs:
build:

runs-on: ubuntu-latest
defaults:
run:
working-directory: ./samples/Calculator
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
- name: Install dotnet-coverage
run: dotnet tool install -g dotnet-coverage
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Run tests
run: dotnet-coverage collect --output-format cobertura --output ./TestResults/report.cobertura.xml "dotnet test --no-build"
- name: ReportGenerator
uses: danielpalme/ReportGenerator-GitHub-Action@5.2.0
with:
reports: '${{ github.workspace }}/samples/Calculator/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:
name: code-coverage-report
path: '${{ github.workspace }}/samples/Calculator/TestResults/**/*.cobertura.xml'
3 changes: 2 additions & 1 deletion samples/Calculator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ Solution contains seven projects:
21. [***Scenario 21*** Code coverage for whole solution with script to run tests](scenarios/scenario21/README.md)
22. [***Scenario 22*** Code coverage for child processes using static instrumentation without binaries restore](scenarios/scenario22/README.md)
23. [***Scenario 23*** Code coverage for child processes using static instrumentation with binaries restore](scenarios/scenario23/README.md)
24. [***Scenario 24*** Code coverage for whole solution (only unit tests)](scenarios/scenario24/README.md)
24. [***Scenario 24*** Code coverage for whole solution (only unit tests)](scenarios/scenario24/README.md)
25. [***Scenario 25*** Code coverage for whole solution (only unit tests - using dotnet-coverage)](scenarios/scenario25/README.md)
1 change: 0 additions & 1 deletion samples/Calculator/nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="test-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/test-tools/nuget/v3/index.json" />
</packageSources>
</configuration>
104 changes: 104 additions & 0 deletions samples/Calculator/scenarios/scenario25/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Scenario Description

Gather code coverage for the entire solution using dotnet-coverage tool. Refer to the provided example for collecting coverage across all unit tests. The system automatically consolidates coverage reports from all test projects. Integration tests are excluded for simplification, as the server is not initiated during this process.

# Collect code coverage using command line

```shell
git clone https://github.com/microsoft/codecoverage.git
cd codecoverage/samples/Calculator
dotnet build
dotnet tool install -g dotnet-coverage
dotnet-coverage collect -f cobertura -o report.cobertura.xml "dotnet test --no-build"
```

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

# Collect code coverage inside github workflow

Executing tests is automatically creating `cobertura` report. Then `reportgenerator` can be used to generate final github summary markdown.

```yml
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
- name: Install dotnet-coverage
run: dotnet tool install -g dotnet-coverage
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Run tests
run: dotnet-coverage collect --output-format cobertura --output ./TestResults/report.cobertura.xml "dotnet test --no-build"
- name: ReportGenerator
uses: danielpalme/ReportGenerator-GitHub-Action@5.2.0
with:
reports: '${{ github.workspace }}/samples/Calculator/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:
name: code-coverage-report
path: '${{ github.workspace }}/samples/Calculator/TestResults/**/*.cobertura.xml'
```

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

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

# Collect code coverage inside Azure DevOps Pipelines

```yml
steps:
- task: DotNetCoreCLI@2
inputs:
command: 'custom'
custom: "tool"
arguments: 'install -g dotnet-coverage'
displayName: 'install dotnet-coverage'

- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '$(solutionPath)' # 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: '$(solutionPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet build'

- task: Bash@3
inputs:
targetType: 'inline'
script: 'dotnet-coverage collect -f cobertura -o $(Agent.TempDirectory)/report.cobertura.xml "dotnet test --configuration $(buildConfiguration) --no-build --logger trx --results-directory $(Agent.TempDirectory)"'
workingDirectory: '$(Build.SourcesDirectory)/samples/Calculator/'
displayName: 'dotnet test'

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

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

[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.cobertura.xml)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions samples/Calculator/scenarios/scenario25/azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: "Calculator Scenario 25"

pool:
vmImage: 'ubuntu-latest'

variables:
buildConfiguration: 'Debug'
solutionPath: './samples/Calculator/Calculator.sln' # this is specific to example - in most cases not needed

steps:
- task: DotNetCoreCLI@2
inputs:
command: 'custom'
custom: "tool"
arguments: 'install -g dotnet-coverage'
displayName: 'install dotnet-coverage'

- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '$(solutionPath)' # 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: '$(solutionPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet build'

- task: Bash@3
inputs:
targetType: 'inline'
script: 'dotnet-coverage collect -f cobertura -o $(Agent.TempDirectory)/report.cobertura.xml "dotnet test --configuration $(buildConfiguration) --no-build --logger trx --results-directory $(Agent.TempDirectory)"'
workingDirectory: '$(Build.SourcesDirectory)/samples/Calculator/'
displayName: 'dotnet test'

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

- task: PublishCodeCoverageResults@2
inputs:
summaryFileLocation: $(Agent.TempDirectory)/report.cobertura.xml

Loading

0 comments on commit ecaf850

Please sign in to comment.