diff --git a/.github/workflows/Algorithms_Scenario04.yml b/.github/workflows/Algorithms_Scenario04.yml new file mode 100644 index 0000000..e99596b --- /dev/null +++ b/.github/workflows/Algorithms_Scenario04.yml @@ -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: "Algorithms Scenario 04" + +on: + push: + branches: [ "main" ] + paths: [ 'samples/Algorithms/tests/**', 'samples/Algorithms/src/**', '.github/workflows/Algorithms_Scenario04.yml' ] + +jobs: + build: + + runs-on: ubuntu-latest + defaults: + run: + working-directory: ./samples/Algorithms/tests/Algorithms.Core.Tests + steps: + - uses: actions/checkout@v3 + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: 8.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: Test + run: dotnet-coverage collect --output $GITHUB_WORKSPACE/report.cobertura.xml --output-format cobertura "dotnet run --no-build" + - name: ReportGenerator + uses: danielpalme/ReportGenerator-GitHub-Action@5.2.0 + 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 diff --git a/samples/Algorithms/README.md b/samples/Algorithms/README.md index 55f5475..8d51851 100644 --- a/samples/Algorithms/README.md +++ b/samples/Algorithms/README.md @@ -8,4 +8,5 @@ Solution contains two projects: 1. [***Scenario 01*** Code coverage for MSTest Runner project using dynamic instrumentation](scenarios/scenario01/README.md) 2. [***Scenario 02*** Code coverage for MSTest Runner project using static instrumentation](scenarios/scenario02/README.md) -3. [***Scenario 03*** Code coverage for MSTest Runner project using compile-time instrumentation](scenarios/scenario03/README.md) \ No newline at end of file +3. [***Scenario 03*** Code coverage for MSTest Runner project using compile-time instrumentation](scenarios/scenario03/README.md) +4. [***Scenario 04*** Code coverage for MSTest Runner project using `dotnet-coverage` tool](scenarios/scenario04/README.md) \ No newline at end of file diff --git a/samples/Algorithms/scenarios/scenario01/azure-pipelines.jpg b/samples/Algorithms/scenarios/scenario01/azure-pipelines.jpg index e8018ca..e734698 100644 Binary files a/samples/Algorithms/scenarios/scenario01/azure-pipelines.jpg and b/samples/Algorithms/scenarios/scenario01/azure-pipelines.jpg differ diff --git a/samples/Algorithms/scenarios/scenario01/example.report.cobertura.xml b/samples/Algorithms/scenarios/scenario01/example.report.cobertura.xml index 7e6a3d8..c173f9f 100644 --- a/samples/Algorithms/scenarios/scenario01/example.report.cobertura.xml +++ b/samples/Algorithms/scenarios/scenario01/example.report.cobertura.xml @@ -1,74 +1,6 @@  - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/samples/Algorithms/scenarios/scenario01/example.report.jpg b/samples/Algorithms/scenarios/scenario01/example.report.jpg index 71f15ec..39c33d5 100644 Binary files a/samples/Algorithms/scenarios/scenario01/example.report.jpg and b/samples/Algorithms/scenarios/scenario01/example.report.jpg differ diff --git a/samples/Algorithms/scenarios/scenario02/azure-pipelines.jpg b/samples/Algorithms/scenarios/scenario02/azure-pipelines.jpg index e8018ca..b5e586c 100644 Binary files a/samples/Algorithms/scenarios/scenario02/azure-pipelines.jpg and b/samples/Algorithms/scenarios/scenario02/azure-pipelines.jpg differ diff --git a/samples/Algorithms/scenarios/scenario02/example.report.cobertura.xml b/samples/Algorithms/scenarios/scenario02/example.report.cobertura.xml index 7e6a3d8..ed2ad43 100644 --- a/samples/Algorithms/scenarios/scenario02/example.report.cobertura.xml +++ b/samples/Algorithms/scenarios/scenario02/example.report.cobertura.xml @@ -1,74 +1,6 @@  - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/samples/Algorithms/scenarios/scenario02/example.report.jpg b/samples/Algorithms/scenarios/scenario02/example.report.jpg index 71f15ec..c51d3c3 100644 Binary files a/samples/Algorithms/scenarios/scenario02/example.report.jpg and b/samples/Algorithms/scenarios/scenario02/example.report.jpg differ diff --git a/samples/Algorithms/scenarios/scenario03/azure-pipelines.jpg b/samples/Algorithms/scenarios/scenario03/azure-pipelines.jpg index 568b648..cd45b0d 100644 Binary files a/samples/Algorithms/scenarios/scenario03/azure-pipelines.jpg and b/samples/Algorithms/scenarios/scenario03/azure-pipelines.jpg differ diff --git a/samples/Algorithms/scenarios/scenario03/example.report.cobertura.xml b/samples/Algorithms/scenarios/scenario03/example.report.cobertura.xml index 9f256b1..f55d742 100644 --- a/samples/Algorithms/scenarios/scenario03/example.report.cobertura.xml +++ b/samples/Algorithms/scenarios/scenario03/example.report.cobertura.xml @@ -1,5 +1,5 @@  - + @@ -49,65 +49,5 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/Algorithms/scenarios/scenario03/example.report.jpg b/samples/Algorithms/scenarios/scenario03/example.report.jpg index db25eaf..82c8dfd 100644 Binary files a/samples/Algorithms/scenarios/scenario03/example.report.jpg and b/samples/Algorithms/scenarios/scenario03/example.report.jpg differ diff --git a/samples/Algorithms/scenarios/scenario04/README.md b/samples/Algorithms/scenarios/scenario04/README.md new file mode 100644 index 0000000..cc093ef --- /dev/null +++ b/samples/Algorithms/scenarios/scenario04/README.md @@ -0,0 +1,106 @@ +# Scenario Description + +Collect code coverage using `dotnet-coverage` tool for MSTest runner project. + +> **_NOTE:_** MSTest runner project coverage extension by default is not collecting native code coverage. If you want to enable please set to `True` `EnableStaticNativeInstrumentation` or `EnableDynamicNativeInstrumentation` in configuration. + +# Collect code coverage using command line + +```shell +git clone https://github.com/microsoft/codecoverage.git +cd codecoverage/samples/Algorithms/tests/Algorithms.Core.Tests/ +dotnet build +dotnet tool install -g dotnet-coverage +dotnet-coverage collect --output report.cobertura.xml --output-format cobertura "dotnet run --no-build" +``` + +You can also use [run.ps1](run.ps1) to collect code coverage. + +# Collect code coverage inside github workflow + +`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: 8.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: Test + run: dotnet-coverage collect --output $GITHUB_WORKSPACE/report.cobertura.xml --output-format cobertura "dotnet run --no-build" + - name: ReportGenerator + uses: danielpalme/ReportGenerator-GitHub-Action@5.2.0 + 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/Algorithms_Scenario04.yml) + +[Run example](../../../../../../actions/workflows/Algorithms_Scenario04.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: '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: '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 collect --output-format cobertura --output $(Agent.TempDirectory)/report.cobertura.xml "dotnet run --project $(projectPath) --no-build --report-trx --configuration $(buildConfiguration) --results-directory $(Agent.TempDirectory)"' + displayName: 'test' + +- task: PublishTestResults@2 + inputs: + testResultsFormat: 'VSTest' + testResultsFiles: '$(Agent.TempDirectory)/**/*.trx' + publishRunAttachments: false + +- task: PublishCodeCoverageResults@2 + inputs: + summaryFileLocation: $(Agent.TempDirectory)/**/*.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) \ No newline at end of file diff --git a/samples/Algorithms/scenarios/scenario04/azure-pipelines.jpg b/samples/Algorithms/scenarios/scenario04/azure-pipelines.jpg new file mode 100644 index 0000000..b112196 Binary files /dev/null and b/samples/Algorithms/scenarios/scenario04/azure-pipelines.jpg differ diff --git a/samples/Algorithms/scenarios/scenario04/azure-pipelines.yml b/samples/Algorithms/scenarios/scenario04/azure-pipelines.yml new file mode 100644 index 0000000..5c45119 --- /dev/null +++ b/samples/Algorithms/scenarios/scenario04/azure-pipelines.yml @@ -0,0 +1,45 @@ +name: "Algorithms Scenario 04" + +pool: + vmImage: 'ubuntu-latest' + +variables: + buildConfiguration: 'Debug' + projectPath: './samples/Algorithms/tests/Algorithms.Core.Tests/Algorithms.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: '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: '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 collect --output-format cobertura --output $(Agent.TempDirectory)/report.cobertura.xml "dotnet run --project $(projectPath) --no-build --report-trx --configuration $(buildConfiguration) --results-directory $(Agent.TempDirectory)"' + displayName: 'test' + +- task: PublishTestResults@2 + inputs: + testResultsFormat: 'VSTest' + testResultsFiles: '$(Agent.TempDirectory)/**/*.trx' + publishRunAttachments: false + +- task: PublishCodeCoverageResults@2 + inputs: + summaryFileLocation: $(Agent.TempDirectory)/**/*.cobertura.xml diff --git a/samples/Algorithms/scenarios/scenario04/example.report.cobertura.xml b/samples/Algorithms/scenarios/scenario04/example.report.cobertura.xml new file mode 100644 index 0000000..363130a --- /dev/null +++ b/samples/Algorithms/scenarios/scenario04/example.report.cobertura.xml @@ -0,0 +1,155 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/Algorithms/scenarios/scenario04/example.report.jpg b/samples/Algorithms/scenarios/scenario04/example.report.jpg new file mode 100644 index 0000000..64c2ab1 Binary files /dev/null and b/samples/Algorithms/scenarios/scenario04/example.report.jpg differ diff --git a/samples/Algorithms/scenarios/scenario04/run.ps1 b/samples/Algorithms/scenarios/scenario04/run.ps1 new file mode 100644 index 0000000..76230e8 --- /dev/null +++ b/samples/Algorithms/scenarios/scenario04/run.ps1 @@ -0,0 +1,4 @@ +cd $PSScriptRoot/../../tests/Algorithms.Core.Tests +dotnet build +dotnet tool install -g dotnet-coverage +dotnet-coverage collect --output report.cobertura.xml --output-format cobertura "dotnet run --no-build" \ No newline at end of file