Skip to content

Commit

Permalink
Merge pull request #53 from jakubch1/main
Browse files Browse the repository at this point in the history
Scenarios 9-12
  • Loading branch information
jakubch1 committed Sep 6, 2023
2 parents adfedd9 + b78dea7 commit ef6cb52
Show file tree
Hide file tree
Showing 34 changed files with 880 additions and 11 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/Calculator_Scenario10.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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 10"

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

jobs:
build:

runs-on: ubuntu-latest
defaults:
run:
working-directory: ./samples/Calculator/src/Calculator.Console
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: Install dotnet-coverage
run: dotnet tool install -g dotnet-coverage
- name: Instrument Calculator.Core.dll
run: dotnet-coverage instrument --session-id TagScenario10 "./bin/Debug/net7.0/Calculator.Core.dll"
- name: Instrument Calculator.Console.dll
run: dotnet-coverage instrument --session-id TagScenario10 "./bin/Debug/net7.0/Calculator.Console.dll"
- name: Start code coverage collection session
run: dotnet-coverage collect --session-id TagScenario10 --server-mode --background -f cobertura -o $GITHUB_WORKSPACE/report.cobertura.xml
- name: Run (add)
run: dotnet run --no-build add 10 24
- name: Run (multiply)
run: dotnet run --no-build multiply 10 24
- name: Stop code coverage collection session
run: dotnet-coverage shutdown TagScenario10
- 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:
name: code-coverage-report
path: '${{ github.workspace }}/report.cobertura.xml'
50 changes: 50 additions & 0 deletions .github/workflows/Calculator_Scenario11.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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 11"

on:
push:
branches: [ "main" ]
paths: [ 'samples/Calculator/tests/**', 'samples/Calculator/src/**', '.github/workflows/Calculator_Scenario11.yml', 'samples/Calculator/scenarios/scenario11/coverage.runsettings' ]

jobs:
build:

runs-on: ubuntu-latest
defaults:
run:
working-directory: ./samples/Calculator/tests/Calculator.Console.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: Restore dependencies (Console project)
run: dotnet restore ../../src/Calculator.Console/Calculator.Console.csproj
- name: Build
run: dotnet build --no-restore
- name: Build (Console project)
run: dotnet build --no-restore ../../src/Calculator.Console/Calculator.Console.csproj
- name: Test
run: dotnet test --settings ../../scenarios/scenario11/coverage.runsettings --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.Console.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:
name: code-coverage-report
path: ./**/TestResults/**/*.coverage
50 changes: 50 additions & 0 deletions .github/workflows/Calculator_Scenario12.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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 12"

on:
push:
branches: [ "main" ]
paths: [ 'samples/Calculator/tests/**', 'samples/Calculator/src/**', '.github/workflows/Calculator_Scenario12.yml', 'samples/Calculator/scenarios/scenario12/coverage.runsettings' ]

jobs:
build:

runs-on: ubuntu-latest
defaults:
run:
working-directory: ./samples/Calculator/tests/Calculator.Console.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: Restore dependencies (Console project)
run: dotnet restore ../../src/Calculator.Console/Calculator.Console.csproj
- name: Build
run: dotnet build --no-restore
- name: Build (Console project)
run: dotnet build --no-restore ../../src/Calculator.Console/Calculator.Console.csproj
- name: Test
run: dotnet test --settings ../../scenarios/scenario12/coverage.runsettings --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.Console.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:
name: code-coverage-report
path: ./**/TestResults/**/*.coverage
5 changes: 4 additions & 1 deletion samples/Calculator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ Solution contains seven projects:
6. [***Scenario 06*** Exclude tests projects and other libraries (xunit, moq)](scenarios/scenario06/README.md)
7. [***Scenario 07*** Static instrumentation](scenarios/scenario07/README.md)
8. [***Scenario 08*** Code coverage for console application](scenarios/scenario08/README.md)
9. [***Scenario 09*** Code coverage for console application - static instrumentation](scenarios/scenario09/README.md)
9. [***Scenario 09*** Code coverage for console application - static instrumentation](scenarios/scenario09/README.md)
10. [***Scenario 10*** Code coverage for console application - static instrumentation with instrument command](scenarios/scenario10/README.md)
11. [***Scenario 11*** Code coverage for child processes enabled](scenarios/scenario11/README.md)
12. [***Scenario 12*** Code coverage for child processes disabled](scenarios/scenario12/README.md)
2 changes: 1 addition & 1 deletion samples/Calculator/scenarios/scenario01/run.ps1
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cd $PSScriptRoot/../../tests/Calculator.Core.tests
cd $PSScriptRoot/../../tests/Calculator.Core.Tests
dotnet test --collect "Code Coverage"
2 changes: 1 addition & 1 deletion samples/Calculator/scenarios/scenario02/run.ps1
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cd $PSScriptRoot/../../tests/Calculator.Core.tests
cd $PSScriptRoot/../../tests/Calculator.Core.Tests
dotnet test --collect "Code Coverage;Format=cobertura"
2 changes: 1 addition & 1 deletion samples/Calculator/scenarios/scenario03/run.ps1
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cd $PSScriptRoot/../../tests/Calculator.Core.tests
cd $PSScriptRoot/../../tests/Calculator.Core.Tests
dotnet test --settings ../../scenarios/scenario03/coverage.runsettings
2 changes: 1 addition & 1 deletion samples/Calculator/scenarios/scenario04/run.ps1
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cd $PSScriptRoot/../../tests/Calculator.Core.tests
cd $PSScriptRoot/../../tests/Calculator.Core.Tests
dotnet test --collect "Code Coverage;Format=xml"
2 changes: 1 addition & 1 deletion samples/Calculator/scenarios/scenario05/run.ps1
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cd $PSScriptRoot/../../tests/Calculator.Core.tests
cd $PSScriptRoot/../../tests/Calculator.Core.Tests
dotnet test --settings ../../scenarios/scenario05/coverage.runsettings
2 changes: 1 addition & 1 deletion samples/Calculator/scenarios/scenario06/run.ps1
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cd $PSScriptRoot/../../tests/Calculator.Core.tests
cd $PSScriptRoot/../../tests/Calculator.Core.Tests
dotnet test --settings ../../scenarios/scenario06/coverage.runsettings
2 changes: 1 addition & 1 deletion samples/Calculator/scenarios/scenario07/run.ps1
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cd $PSScriptRoot/../../tests/Calculator.Core.tests
cd $PSScriptRoot/../../tests/Calculator.Core.Tests
dotnet test --settings ../../scenarios/scenario07/coverage.runsettings
4 changes: 2 additions & 2 deletions samples/Calculator/scenarios/scenario09/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ You can also use [run.ps1](run.ps1) to execute this scenario.
path: '${{ github.workspace }}/report.cobertura.xml'
```

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

[Run example](../../../../../../actions/workflows/Calculator_Scenario08.yml)
[Run example](../../../../../../actions/workflows/Calculator_Scenario09.yml)

# Collect code coverage inside Azure DevOps Pipelines

Expand Down
143 changes: 143 additions & 0 deletions samples/Calculator/scenarios/scenario10/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Scenario Description

This example shows how our tool [dotnet-coverage](https://aka.ms/dotnet-coverage) can be used to collect code coverage for console application using [static instrumentation](../../../../docs/instrumentation.md). `dotnet-coverage` `instrument` command is used to instrument files and code coverage session is started in background. Similar sequence can be used for more advanced scenarios - for example IIS. When executing tests this is not needed as our tooling automatically detects directory with libraries and instrument it. Cobertura report format can be used to generate HTML report using [report generator](https://github.com/danielpalme/ReportGenerator). This format can be also used with [PublishCodeCoverageResults@2](https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/publish-code-coverage-results-v2?view=azure-pipelines) in Azure DevOps pipelines.

# Collect code coverage using command line

```shell
git clone https://github.com/microsoft/codecoverage.git
cd codecoverage/samples/Calculator/src/Calculator.Console/
dotnet build
dotnet tool install -g dotnet-coverage
dotnet-coverage instrument --session-id TagScenario10 "./bin/Debug/net7.0/Calculator.Core.dll"
dotnet-coverage instrument --session-id TagScenario10 "./bin/Debug/net7.0/Calculator.Console.dll"
dotnet-coverage collect --session-id TagScenario10 --server-mode --background -f cobertura
dotnet run --no-build add 10 24
dotnet run --no-build multiply 10 24
dotnet-coverage shutdown TagScenario10
```

You can also use [run.ps1](run.ps1) to execute this scenario.

# Collect code coverage inside github workflow

```yml
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: Install dotnet-coverage
run: dotnet tool install -g dotnet-coverage
- name: Instrument Calculator.Core.dll
run: dotnet-coverage instrument --session-id TagScenario10 "./bin/Debug/net7.0/Calculator.Core.dll"
- name: Instrument Calculator.Console.dll
run: dotnet-coverage instrument --session-id TagScenario10 "./bin/Debug/net7.0/Calculator.Console.dll"
- name: Start code coverage collection session
run: dotnet-coverage collect --session-id TagScenario10 --server-mode --background -f cobertura -o $GITHUB_WORKSPACE/report.cobertura.xml
- name: Run (add)
run: dotnet run --no-build add 10 24
- name: Run (multiply)
run: dotnet run --no-build multiply 10 24
- name: Stop code coverage collection session
run: dotnet-coverage shutdown TagScenario10
- 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:
name: code-coverage-report
path: '${{ github.workspace }}/report.cobertura.xml'
```

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

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

# Collect code coverage inside Azure DevOps Pipelines

```yml
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: 'custom'
custom: "tool"
arguments: 'install -g dotnet-coverage'
displayName: 'install dotnet-coverage'

- task: Bash@3
inputs:
targetType: 'inline'
script: 'dotnet-coverage instrument --session-id TagScenario10 "$(Build.SourcesDirectory)/samples/Calculator/src/Calculator.Console/bin/Debug/net7.0/Calculator.Core.dll"'
displayName: 'Instrument Calculator.Core.dll'

- task: Bash@3
inputs:
targetType: 'inline'
script: 'dotnet-coverage instrument --session-id TagScenario10 "$(Build.SourcesDirectory)/samples/Calculator/src/Calculator.Console/bin/Debug/net7.0/Calculator.Console.dll"'
displayName: 'Instrument Calculator.Console.dll'

- task: Bash@3
inputs:
targetType: 'inline'
script: 'dotnet-coverage collect --session-id TagScenario10 --server-mode --background -f cobertura -o report.cobertura.xml'
displayName: 'Start code coverage collection session'

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

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

- task: Bash@3
inputs:
targetType: 'inline'
script: 'dotnet-coverage shutdown TagScenario10'
displayName: 'Stop code coverage collection session'

- task: PublishCodeCoverageResults@2
inputs:
summaryFileLocation: '$(Build.SourcesDirectory)/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.
Loading

0 comments on commit ef6cb52

Please sign in to comment.