Skip to content

Commit 967a10d

Browse files
authored
Adding MSTest Sdk sample (#117)
* Adding mstest sdk project,
1 parent 05f80f1 commit 967a10d

File tree

11 files changed

+321
-2
lines changed

11 files changed

+321
-2
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# This workflow will build a .NET project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net
3+
4+
name: "Algorithms Scenario 07"
5+
6+
on:
7+
push:
8+
branches: [ "main" ]
9+
paths: [ 'samples/Algorithms/tests/**', 'samples/Algorithms/src/**', '.github/workflows/Algorithms_Scenario07.yml' ]
10+
11+
jobs:
12+
build:
13+
14+
runs-on: ubuntu-latest
15+
defaults:
16+
run:
17+
working-directory: ./samples/Algorithms/tests/Algorithms.Core.MSTest.Sdk.Tests
18+
steps:
19+
- uses: actions/checkout@v3
20+
- name: Setup .NET
21+
uses: actions/setup-dotnet@v3
22+
with:
23+
dotnet-version: 8.0.x
24+
- name: Restore dependencies
25+
run: dotnet restore
26+
- name: Build
27+
run: dotnet build --no-restore
28+
- name: Test
29+
run: dotnet run --no-build --coverage --coverage-output $GITHUB_WORKSPACE/report.cobertura.xml --coverage-output-format cobertura
30+
- name: ReportGenerator
31+
uses: danielpalme/[email protected]
32+
with:
33+
reports: '${{ github.workspace }}/report.cobertura.xml'
34+
targetdir: '${{ github.workspace }}/coveragereport'
35+
reporttypes: 'MarkdownSummaryGithub'
36+
- name: Upload coverage into summary
37+
run: cat $GITHUB_WORKSPACE/coveragereport/SummaryGithub.md >> $GITHUB_STEP_SUMMARY
38+
- name: Archive code coverage results
39+
uses: actions/upload-artifact@v3
40+
with:
41+
name: code-coverage-report
42+
path: ${{ github.workspace }}/report.cobertura.xml

samples/Algorithms/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Solution contains two projects:
44
1. `Algorithms.Core` - contains core logic for solution
55
2. `Algorithms.Console` - contains Native AOT console app
66
3. `Algorithms.Core.Tests` - contains unit tests for `Algorithms.Core`. It is [MSTest runner project](https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-mstest-runner-intro?tabs=dotnetcli)
7+
4. `Algorithms.Core.MSTest.Sdk.Tests` - contains unit tests for `Algorithms.Core`. It is [MSTest runner project using MSTest SDK](https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-mstest-sdk)
78

89
# Scenarios
910

@@ -13,3 +14,4 @@ Solution contains two projects:
1314
4. [***Scenario 04*** Code coverage for MSTest Runner project using `dotnet-coverage` tool](scenarios/scenario04/README.md)
1415
5. [***Scenario 05*** Code coverage for Native AOT console app](scenarios/scenario05/README.md)
1516
6. [***Scenario 06*** Code coverage for Native AOT MSTest Runner project](scenarios/scenario06/README.md)
17+
7. [***Scenario 07*** Code coverage for MSTest SDK project using dynamic instrumentation](scenarios/scenario07/README.md)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Scenario Description
2+
3+
Collect code coverage using dynamic instrumentation for MSTest SDK project.
4+
5+
> **_NOTE:_** MSTest SDK 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.
6+
7+
## Collect code coverage using command line
8+
9+
```shell
10+
git clone https://github.com/microsoft/codecoverage.git
11+
cd codecoverage/samples/Algorithms/tests/Algorithms.Core.MSTest.Sdk.Tests/
12+
dotnet run --coverage --coverage-output report.cobertura.xml --coverage-output-format cobertura
13+
```
14+
15+
You can also use [run.ps1](run.ps1) to collect code coverage.
16+
17+
## Collect code coverage inside github workflow
18+
19+
`reportgenerator` can be used to generate final github summary markdown.
20+
21+
```yml
22+
steps:
23+
- uses: actions/checkout@v3
24+
- name: Setup .NET
25+
uses: actions/setup-dotnet@v3
26+
with:
27+
dotnet-version: 8.0.x
28+
- name: Restore dependencies
29+
run: dotnet restore
30+
- name: Build
31+
run: dotnet build --no-restore
32+
- name: Test
33+
run: dotnet run --no-build --coverage --coverage-output $GITHUB_WORKSPACE/report.cobertura.xml --coverage-output-format cobertura
34+
- name: ReportGenerator
35+
uses: danielpalme/[email protected]
36+
with:
37+
reports: '${{ github.workspace }}/report.cobertura.xml'
38+
targetdir: '${{ github.workspace }}/coveragereport'
39+
reporttypes: 'MarkdownSummaryGithub'
40+
- name: Upload coverage into summary
41+
run: cat $GITHUB_WORKSPACE/coveragereport/SummaryGithub.md >> $GITHUB_STEP_SUMMARY
42+
- name: Archive code coverage results
43+
uses: actions/upload-artifact@v3
44+
with:
45+
name: code-coverage-report
46+
path: ${{ github.workspace }}/report.cobertura.xml
47+
```
48+
49+
[Full source example](../../../../.github/workflows/Algorithms_Scenario07.yml)
50+
51+
[Run example](../../../../../../actions/workflows/Algorithms_Scenario07.yml)
52+
53+
## Collect code coverage inside Azure DevOps Pipelines
54+
55+
```yml
56+
steps:
57+
- task: DotNetCoreCLI@2
58+
inputs:
59+
command: 'restore'
60+
projects: '$(projectPath)' # this is specific to example - in most cases not needed
61+
displayName: 'restore'
62+
63+
- task: DotNetCoreCLI@2
64+
inputs:
65+
command: 'build'
66+
arguments: '--no-restore --configuration $(buildConfiguration)'
67+
projects: '$(projectPath)' # this is specific to example - in most cases not needed
68+
displayName: 'build'
69+
70+
- task: DotNetCoreCLI@2
71+
inputs:
72+
command: 'run'
73+
arguments: '--no-build --configuration $(buildConfiguration) --results-directory $(Agent.TempDirectory) --coverage --coverage-output $(Agent.TempDirectory)/report.cobertura.xml --coverage-output-format cobertura --report-trx'
74+
projects: '$(projectPath)' # this is specific to example - in most cases not needed
75+
displayName: 'test'
76+
77+
- task: PublishTestResults@2
78+
inputs:
79+
testResultsFormat: 'VSTest'
80+
testResultsFiles: '$(Agent.TempDirectory)/**/*.trx'
81+
publishRunAttachments: false
82+
83+
- task: PublishCodeCoverageResults@2
84+
inputs:
85+
summaryFileLocation: $(Agent.TempDirectory)/**/*.cobertura.xml
86+
```
87+
88+
[Full source example](azure-pipelines.yml)
89+
90+
![alt text](azure-pipelines.jpg "Code Coverage tab in Azure DevOps pipelines")
91+
92+
## Report example
93+
94+
![alt text](example.report.jpg "Example report")
95+
96+
[Link](example.report.cobertura.xml)
92.2 KB
Loading
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: "Algorithms Scenario 07"
2+
3+
pool:
4+
vmImage: 'ubuntu-latest'
5+
6+
variables:
7+
buildConfiguration: 'Debug'
8+
projectPath: './samples/Algorithms/tests/Algorithms.Core.MSTest.Sdk.Tests/Algorithms.Core.MSTest.Sdk.Tests.csproj' # this is specific to example - in most cases not needed
9+
10+
steps:
11+
- task: DotNetCoreCLI@2
12+
inputs:
13+
command: 'restore'
14+
projects: '$(projectPath)' # this is specific to example - in most cases not needed
15+
displayName: 'restore'
16+
17+
- task: DotNetCoreCLI@2
18+
inputs:
19+
command: 'build'
20+
arguments: '--no-restore --configuration $(buildConfiguration)'
21+
projects: '$(projectPath)' # this is specific to example - in most cases not needed
22+
displayName: 'build'
23+
24+
- task: DotNetCoreCLI@2
25+
inputs:
26+
command: 'run'
27+
arguments: '--no-build --configuration $(buildConfiguration) --results-directory $(Agent.TempDirectory) --coverage --coverage-output $(Agent.TempDirectory)/report.cobertura.xml --coverage-output-format cobertura --report-trx'
28+
projects: '$(projectPath)' # this is specific to example - in most cases not needed
29+
displayName: 'test'
30+
31+
- task: PublishTestResults@2
32+
inputs:
33+
testResultsFormat: 'VSTest'
34+
testResultsFiles: '$(Agent.TempDirectory)/**/*.trx'
35+
publishRunAttachments: false
36+
37+
- task: PublishCodeCoverageResults@2
38+
inputs:
39+
summaryFileLocation: $(Agent.TempDirectory)/**/*.cobertura.xml
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
2+
<coverage line-rate="1" branch-rate="1" complexity="10" version="1.9" timestamp="1705670662" lines-covered="17" lines-valid="17" branches-covered="10" branches-valid="10">
3+
<packages>
4+
<package line-rate="1" branch-rate="1" complexity="10" name="Algorithms.Core">
5+
<classes>
6+
<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">
7+
<methods>
8+
<method line-rate="1" branch-rate="1" complexity="10" name="Merge" signature="(int[], int[])">
9+
<lines>
10+
<line number="6" hits="1" branch="False" />
11+
<line number="7" hits="1" branch="False" />
12+
<line number="8" hits="1" branch="False" />
13+
<line number="9" hits="1" branch="False" />
14+
<line number="11" hits="1" branch="False" />
15+
<line number="13" hits="1" branch="True" condition-coverage="100% (4/4)">
16+
<conditions>
17+
<condition number="0" type="jump" coverage="100%" />
18+
<condition number="1" type="jump" coverage="100%" />
19+
</conditions>
20+
</line>
21+
<line number="14" hits="1" branch="False" />
22+
<line number="15" hits="1" branch="True" condition-coverage="100% (2/2)">
23+
<conditions>
24+
<condition number="0" type="jump" coverage="100%" />
25+
</conditions>
26+
</line>
27+
<line number="16" hits="1" branch="False" />
28+
<line number="18" hits="1" branch="False" />
29+
<line number="19" hits="1" branch="False" />
30+
<line number="21" hits="1" branch="True" condition-coverage="100% (2/2)">
31+
<conditions>
32+
<condition number="0" type="jump" coverage="100%" />
33+
</conditions>
34+
</line>
35+
<line number="22" hits="1" branch="False" />
36+
<line number="24" hits="1" branch="True" condition-coverage="100% (2/2)">
37+
<conditions>
38+
<condition number="0" type="jump" coverage="100%" />
39+
</conditions>
40+
</line>
41+
<line number="25" hits="1" branch="False" />
42+
<line number="27" hits="1" branch="False" />
43+
<line number="28" hits="1" branch="False" />
44+
</lines>
45+
</method>
46+
</methods>
47+
<lines>
48+
<line number="6" hits="1" branch="False" />
49+
<line number="7" hits="1" branch="False" />
50+
<line number="8" hits="1" branch="False" />
51+
<line number="9" hits="1" branch="False" />
52+
<line number="11" hits="1" branch="False" />
53+
<line number="13" hits="1" branch="True" condition-coverage="100% (4/4)">
54+
<conditions>
55+
<condition number="0" type="jump" coverage="100%" />
56+
<condition number="1" type="jump" coverage="100%" />
57+
</conditions>
58+
</line>
59+
<line number="14" hits="1" branch="False" />
60+
<line number="15" hits="1" branch="True" condition-coverage="100% (2/2)">
61+
<conditions>
62+
<condition number="0" type="jump" coverage="100%" />
63+
</conditions>
64+
</line>
65+
<line number="16" hits="1" branch="False" />
66+
<line number="18" hits="1" branch="False" />
67+
<line number="19" hits="1" branch="False" />
68+
<line number="21" hits="1" branch="True" condition-coverage="100% (2/2)">
69+
<conditions>
70+
<condition number="0" type="jump" coverage="100%" />
71+
</conditions>
72+
</line>
73+
<line number="22" hits="1" branch="False" />
74+
<line number="24" hits="1" branch="True" condition-coverage="100% (2/2)">
75+
<conditions>
76+
<condition number="0" type="jump" coverage="100%" />
77+
</conditions>
78+
</line>
79+
<line number="25" hits="1" branch="False" />
80+
<line number="27" hits="1" branch="False" />
81+
<line number="28" hits="1" branch="False" />
82+
</lines>
83+
</class>
84+
</classes>
85+
</package>
86+
</packages>
87+
</coverage>
68.4 KB
Loading
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cd $PSScriptRoot/../../tests/Algorithms.Core.MSTest.Sdk.Tests
2+
dotnet run --coverage --coverage-output report.cobertura.xml --coverage-output-format cobertura
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="MSTest.Sdk/3.3.1">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="../../src/Algorithms.Core/Algorithms.Core.csproj" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Algorithms.Core;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
4+
[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]
5+
6+
namespace Algorithms.Core.MSTest.Sdk.Tests;
7+
8+
[TestClass]
9+
public class MergerTests
10+
{
11+
[TestMethod]
12+
public void Test1()
13+
{
14+
var result = Merger.Merge([1, 3, 5], [2, 4, 5, 6]);
15+
AssertArrays([1, 2, 3, 4, 5, 5, 6], result);
16+
}
17+
18+
[TestMethod]
19+
public void Test2()
20+
{
21+
var result = Merger.Merge([], [2, 4, 5, 6]);
22+
AssertArrays([2, 4, 5, 6], result);
23+
}
24+
25+
[TestMethod]
26+
public void Test3()
27+
{
28+
var result = Merger.Merge([2, 4, 5, 6], []);
29+
AssertArrays([2, 4, 5, 6], result);
30+
}
31+
32+
private void AssertArrays(int[] expected, int[] actual)
33+
{
34+
Assert.AreEqual(expected.Length, actual.Length, $"Sizes of arrays are different. expected.Length={expected.Length} actual.Length={actual.Length}");
35+
for (int index = 0; index < expected.Length; index++)
36+
Assert.AreEqual(expected[index], actual[index], $"Arrays different on index={index}. expected[{index}]={expected[index]} actual[{index}]={actual[index]}");
37+
}
38+
}

0 commit comments

Comments
 (0)