Skip to content

Commit

Permalink
MSTest native aot example
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Chocholowicz committed Mar 25, 2024
1 parent 508cc4d commit 5466021
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 0 deletions.
106 changes: 106 additions & 0 deletions samples/Algorithms/scenarios/scenario06/README.md
Original file line number Diff line number Diff line change
@@ -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/[email protected]
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)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions samples/Algorithms/scenarios/scenario06/azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: "Algorithms Scenario 06"

pool:
vmImage: 'ubuntu-latest'

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

steps:
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: false
zipAfterPublish: false
arguments: '-r linux-x64 -c Release /p:AotMsCodeCoverageInstrumentation="true"'
projects: '$(projectPath)' # this is specific to example - in most cases not needed
displayName: 'publish'

- task: Bash@3
inputs:
targetType: 'inline'
script: './samples/Algorithms/tests/Algorithms.Core.NativeAot.Tests/bin/Release/net8.0/linux-x64/publish/Algorithms.Core.NativeAot.Tests --coverage --coverage-output-format cobertura --coverage-output $(Agent.TempDirectory)/report.cobertura.xml --report-trx --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
155 changes: 155 additions & 0 deletions samples/Algorithms/scenarios/scenario06/example.report.cobertura.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<coverage line-rate="1" branch-rate="1" complexity="15" version="1.9" timestamp="1705679599" lines-covered="34" lines-valid="34" branches-covered="12" branches-valid="12">
<packages>
<package line-rate="1" branch-rate="1" complexity="10" name="Algorithms.Core">
<classes>
<class line-rate="1" branch-rate="1" complexity="10" name="Algorithms.Core.Merger" filename="/home/runner/work/codecoverage/codecoverage/samples/Algorithms/src/Algorithms.Core/Merger.cs">
<methods>
<method line-rate="1" branch-rate="1" complexity="10" name="Merge" signature="(int[], int[])">
<lines>
<line number="6" hits="1" branch="False" />
<line number="7" hits="1" branch="False" />
<line number="8" hits="1" branch="False" />
<line number="9" hits="1" branch="False" />
<line number="11" hits="1" branch="False" />
<line number="13" hits="1" branch="True" condition-coverage="100% (4/4)">
<conditions>
<condition number="0" type="jump" coverage="100%" />
<condition number="1" type="jump" coverage="100%" />
</conditions>
</line>
<line number="14" hits="1" branch="False" />
<line number="15" hits="1" branch="True" condition-coverage="100% (2/2)">
<conditions>
<condition number="0" type="jump" coverage="100%" />
</conditions>
</line>
<line number="16" hits="1" branch="False" />
<line number="18" hits="1" branch="False" />
<line number="19" hits="1" branch="False" />
<line number="21" hits="1" branch="True" condition-coverage="100% (2/2)">
<conditions>
<condition number="0" type="jump" coverage="100%" />
</conditions>
</line>
<line number="22" hits="1" branch="False" />
<line number="24" hits="1" branch="True" condition-coverage="100% (2/2)">
<conditions>
<condition number="0" type="jump" coverage="100%" />
</conditions>
</line>
<line number="25" hits="1" branch="False" />
<line number="27" hits="1" branch="False" />
<line number="28" hits="1" branch="False" />
</lines>
</method>
</methods>
<lines>
<line number="6" hits="1" branch="False" />
<line number="7" hits="1" branch="False" />
<line number="8" hits="1" branch="False" />
<line number="9" hits="1" branch="False" />
<line number="11" hits="1" branch="False" />
<line number="13" hits="1" branch="True" condition-coverage="100% (4/4)">
<conditions>
<condition number="0" type="jump" coverage="100%" />
<condition number="1" type="jump" coverage="100%" />
</conditions>
</line>
<line number="14" hits="1" branch="False" />
<line number="15" hits="1" branch="True" condition-coverage="100% (2/2)">
<conditions>
<condition number="0" type="jump" coverage="100%" />
</conditions>
</line>
<line number="16" hits="1" branch="False" />
<line number="18" hits="1" branch="False" />
<line number="19" hits="1" branch="False" />
<line number="21" hits="1" branch="True" condition-coverage="100% (2/2)">
<conditions>
<condition number="0" type="jump" coverage="100%" />
</conditions>
</line>
<line number="22" hits="1" branch="False" />
<line number="24" hits="1" branch="True" condition-coverage="100% (2/2)">
<conditions>
<condition number="0" type="jump" coverage="100%" />
</conditions>
</line>
<line number="25" hits="1" branch="False" />
<line number="27" hits="1" branch="False" />
<line number="28" hits="1" branch="False" />
</lines>
</class>
</classes>
</package>
<package line-rate="1" branch-rate="1" complexity="5" name="Algorithms.Core.Tests">
<classes>
<class line-rate="1" branch-rate="1" complexity="5" name="Algorithms.Core.Tests.MergerTests" filename="/home/runner/work/codecoverage/codecoverage/samples/Algorithms/tests/Algorithms.Core.Tests/MergerTests.cs">
<methods>
<method line-rate="1" branch-rate="1" complexity="1" name="Test1" signature="()">
<lines>
<line number="13" hits="1" branch="False" />
<line number="14" hits="1" branch="False" />
<line number="15" hits="1" branch="False" />
<line number="16" hits="1" branch="False" />
</lines>
</method>
<method line-rate="1" branch-rate="1" complexity="1" name="Test2" signature="()">
<lines>
<line number="20" hits="1" branch="False" />
<line number="21" hits="1" branch="False" />
<line number="22" hits="1" branch="False" />
<line number="23" hits="1" branch="False" />
</lines>
</method>
<method line-rate="1" branch-rate="1" complexity="1" name="Test3" signature="()">
<lines>
<line number="27" hits="1" branch="False" />
<line number="28" hits="1" branch="False" />
<line number="29" hits="1" branch="False" />
<line number="30" hits="1" branch="False" />
</lines>
</method>
<method line-rate="1" branch-rate="1" complexity="2" name="AssertArrays" signature="(int[], int[])">
<lines>
<line number="33" hits="1" branch="False" />
<line number="34" hits="1" branch="False" />
<line number="35" hits="1" branch="True" condition-coverage="100% (2/2)">
<conditions>
<condition number="0" type="jump" coverage="100%" />
</conditions>
</line>
<line number="36" hits="1" branch="False" />
<line number="37" hits="1" branch="False" />
</lines>
</method>
</methods>
<lines>
<line number="13" hits="1" branch="False" />
<line number="14" hits="1" branch="False" />
<line number="15" hits="1" branch="False" />
<line number="16" hits="1" branch="False" />
<line number="20" hits="1" branch="False" />
<line number="21" hits="1" branch="False" />
<line number="22" hits="1" branch="False" />
<line number="23" hits="1" branch="False" />
<line number="27" hits="1" branch="False" />
<line number="28" hits="1" branch="False" />
<line number="29" hits="1" branch="False" />
<line number="30" hits="1" branch="False" />
<line number="33" hits="1" branch="False" />
<line number="34" hits="1" branch="False" />
<line number="35" hits="1" branch="True" condition-coverage="100% (2/2)">
<conditions>
<condition number="0" type="jump" coverage="100%" />
</conditions>
</line>
<line number="36" hits="1" branch="False" />
<line number="37" hits="1" branch="False" />
</lines>
</class>
</classes>
</package>
</packages>
</coverage>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions samples/Algorithms/scenarios/scenario06/run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cd $PSScriptRoot/../../tests/Algorithms.Core.NativeAot.Tests
dotnet publish -c Release -r win-x64 /p:AotMsCodeCoverageInstrumentation="true"
.\bin\Release\net8.0\win-x64\publish\Algorithms.Core.NativeAot.Tests.exe --coverage --coverage-output-format cobertura --coverage-output report.cobertura.xml

0 comments on commit 5466021

Please sign in to comment.