Skip to content

Commit

Permalink
Merge pull request #106 from jakubch1/main
Browse files Browse the repository at this point in the history
Native AOT MSTest runner example
  • Loading branch information
jakubch1 authored Mar 25, 2024
2 parents 93ff6f3 + 724242a commit 3c710e7
Show file tree
Hide file tree
Showing 11 changed files with 391 additions and 1 deletion.
40 changes: 40 additions & 0 deletions .github/workflows/Algorithms_Scenario06.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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 06"

on:
push:
branches: [ "main" ]
paths: [ 'samples/Algorithms/src/**', '.github/workflows/Algorithms_Scenario06.yml' ]

jobs:
build:

runs-on: ubuntu-latest
defaults:
run:
working-directory: ./samples/Algorithms/tests/Algorithms.Core.NativeAot.Tests
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Publish
run: dotnet publish -r linux-x64 -c Release /p:AotMsCodeCoverageInstrumentation="true"
- name: Run
run: ./bin/Release/net8.0/linux-x64/publish/Algorithms.Core.NativeAot.Tests --coverage --coverage-output $GITHUB_WORKSPACE/report.cobertura.xml --coverage-output-format cobertura
- 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
3 changes: 2 additions & 1 deletion samples/Algorithms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ Solution contains two projects:
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)
4. [***Scenario 04*** Code coverage for MSTest Runner project using `dotnet-coverage` tool](scenarios/scenario04/README.md)
5. [***Scenario 05*** Code coverage for Native AOT console app](scenarios/scenario05/README.md)
5. [***Scenario 05*** Code coverage for Native AOT console app](scenarios/scenario05/README.md)
6. [***Scenario 06*** Code coverage for Native AOT MSTest Runner project](scenarios/scenario06/README.md)
91 changes: 91 additions & 0 deletions samples/Algorithms/scenarios/scenario06/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Scenario Description

Collect code coverage for MSTest runner project in Native AOT mode.

> **_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.NativeAot.Tests/
dotnet publish -r win-x64 -c Release /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
```

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: Publish
run: dotnet publish -r linux-x64 -c Release /p:AotMsCodeCoverageInstrumentation="true"
- name: Run
run: ./bin/Release/net8.0/linux-x64/publish/Algorithms.Core.NativeAot.Tests --coverage --coverage-output $GITHUB_WORKSPACE/report.cobertura.xml --coverage-output-format cobertura
- 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_Scenario06.yml)

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

# Collect code coverage inside Azure DevOps Pipelines

```yml
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
```
[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
137 changes: 137 additions & 0 deletions samples/Algorithms/scenarios/scenario06/example.report.cobertura.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<coverage line-rate="0.8888888888888888" branch-rate="1" complexity="7" version="1.9" timestamp="1711367026" lines-covered="32" lines-valid="36">
<packages>
<package line-rate="0.8260869565217391" branch-rate="1" complexity="6" name="Algorithms.Core.NativeAot.Tests">
<classes>
<class line-rate="0" branch-rate="1" complexity="1" name="Microsoft.Testing.Framework.SourceGeneration.SourceGeneratedTestingPlatformBuilderHook" filename="/home/runner/work/codecoverage/codecoverage/samples/Algorithms/tests/Algorithms.Core.NativeAot.Tests/MSTest.SourceGeneration/Microsoft.Testing.Framework.SourceGeneration.TestNodesGenerator/SourceGeneratedTestingPlatformBuilderHook.g.cs">
<methods>
<method line-rate="0" branch-rate="1" complexity="1" name="AddExtensions" signature="(Microsoft.Testing.Platform.Builder.ITestApplicationBuilder, string[])">
<lines>
<line number="8" hits="0" branch="False" />
<line number="9" hits="0" branch="False" />
<line number="10" hits="0" branch="False" />
</lines>
</method>
</methods>
<lines>
<line number="8" hits="0" branch="False" />
<line number="9" hits="0" branch="False" />
<line number="10" hits="0" branch="False" />
</lines>
</class>
<class line-rate="1" branch-rate="1" complexity="4" name="Algorithms.Core.NativeAot.Tests.MergerTests" filename="/home/runner/work/codecoverage/codecoverage/samples/Algorithms/tests/Algorithms.Core.NativeAot.Tests/MergerTests.cs">
<methods>
<method line-rate="1" branch-rate="1" complexity="1" name="Test1" signature="()">
<lines>
<line number="11" hits="1" branch="False" />
<line number="12" hits="1" branch="False" />
<line number="13" hits="1" branch="False" />
</lines>
</method>
<method line-rate="1" branch-rate="1" complexity="1" name="Test2" signature="()">
<lines>
<line number="18" hits="1" branch="False" />
<line number="19" hits="1" branch="False" />
<line number="20" hits="1" branch="False" />
</lines>
</method>
<method line-rate="1" branch-rate="1" complexity="1" name="Test3" signature="()">
<lines>
<line number="25" hits="1" branch="False" />
<line number="26" hits="1" branch="False" />
<line number="27" hits="1" branch="False" />
</lines>
</method>
<method line-rate="1" branch-rate="1" complexity="1" name="AssertArrays" signature="(int[], int[])">
<lines>
<line number="31" hits="1" branch="False" />
<line number="32" hits="1" branch="False" />
<line number="33" hits="1" branch="False" />
<line number="34" hits="1" branch="False" />
</lines>
</method>
</methods>
<lines>
<line number="11" hits="1" branch="False" />
<line number="12" hits="1" branch="False" />
<line number="13" hits="1" branch="False" />
<line number="18" hits="1" branch="False" />
<line number="19" hits="1" branch="False" />
<line number="20" hits="1" branch="False" />
<line number="25" hits="1" branch="False" />
<line number="26" hits="1" branch="False" />
<line number="27" hits="1" branch="False" />
<line number="31" hits="1" branch="False" />
<line number="32" hits="1" branch="False" />
<line number="33" hits="1" branch="False" />
<line number="34" hits="1" branch="False" />
</lines>
</class>
<class line-rate="0.8571428571428571" branch-rate="1" complexity="1" name="Algorithms.Core.NativeAot.Tests.Program.&lt;Main&gt;d__0" filename="/home/runner/work/codecoverage/codecoverage/samples/Algorithms/tests/Algorithms.Core.NativeAot.Tests/Program.cs">
<methods>
<method line-rate="0.8571428571428571" branch-rate="1" complexity="1" name="MoveNext" signature="()">
<lines>
<line number="11" hits="1" branch="False" />
<line number="12" hits="1" branch="False" />
<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="17" hits="0" branch="False" />
</lines>
</method>
</methods>
<lines>
<line number="11" hits="1" branch="False" />
<line number="12" hits="1" branch="False" />
<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="17" hits="0" branch="False" />
</lines>
</class>
</classes>
</package>
<package line-rate="1" branch-rate="1" complexity="1" name="Algorithms.Core">
<classes>
<class line-rate="1" branch-rate="1" complexity="1" 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="1" name="Merge" signature="(int[], int[])">
<lines>
<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="False" />
<line number="15" hits="1" branch="False" />
<line number="16" hits="1" branch="False" />
<line number="18" hits="1" branch="False" />
<line number="21" hits="1" branch="False" />
<line number="22" hits="1" branch="False" />
<line number="24" hits="1" branch="False" />
<line number="25" hits="1" branch="False" />
<line number="27" hits="1" branch="False" />
</lines>
</method>
</methods>
<lines>
<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="False" />
<line number="15" hits="1" branch="False" />
<line number="16" hits="1" branch="False" />
<line number="18" hits="1" branch="False" />
<line number="21" hits="1" branch="False" />
<line number="22" hits="1" branch="False" />
<line number="24" hits="1" branch="False" />
<line number="25" hits="1" branch="False" />
<line number="27" 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<PublishAot>true</PublishAot>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MSTest.Engine" Version="1.0.0-alpha.24163.4" />
<PackageReference Include="MSTest.SourceGeneration" Version="1.0.0-alpha.24163.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.2" />
<PackageReference Include="MSTest.Analyzers" Version="3.2.2" />
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="1.0.2" />
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="17.10.4" />
<PackageReference Include="Microsoft.CodeCoverage.MSBuild" Version="17.10.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../../src/Algorithms.Core/Algorithms.Core.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]

namespace Algorithms.Core.NativeAot.Tests;

[TestClass]
public class MergerTests
{
[TestMethod]
public void Test1()
{
var result = Merger.Merge([1, 3, 5], [2, 4, 5, 6]);
AssertArrays([1, 2, 3, 4, 5, 5, 6], result);
}

[TestMethod]
public void Test2()
{
var result = Merger.Merge([], [2, 4, 5, 6]);
AssertArrays([2, 4, 5, 6], result);
}

[TestMethod]
public void Test3()
{
var result = Merger.Merge([2, 4, 5, 6], []);
AssertArrays([2, 4, 5, 6], result);
}

private void AssertArrays(int[] expected, int[] actual)
{
Assert.AreEqual(expected.Length, actual.Length, $"Sizes of arrays are different. expected.Length={expected.Length} actual.Length={actual.Length}");
for (int index = 0; index < expected.Length; index++)
Assert.AreEqual(expected[index], actual[index], $"Arrays different on index={index}. expected[{index}]={expected[index]} actual[{index}]={actual[index]}");
}
}
Loading

0 comments on commit 3c710e7

Please sign in to comment.