Skip to content

Commit 727dc8e

Browse files
committed
Use xUnit v3 through Microsoft Testing Platform
Remaining issues * HTML logger: could not find an equivalent for Microsoft.Testing.Platform * The VSTest target is not called anymore when running `dotnet test` under the .NET 10 SDK ⇒ coverage related targets are not run anymore, see microsoft/testfx#6719 and danielpalme/ReportGenerator#767 * Requires an additional coverage.xml file instead of the much simpler ExcludeByAttribute=GeneratedCodeAttribute that could be configured directly in the csproj * A regression was introduced in the .NET SDK 10.0.101, requiring explicit --configuration option when running dotnet commands, see dotnet/sdk#52358 * Coverage is empty when `ContinuousIntegrationBuild` is set to `true` when using Microsoft.Testing.Extensions.CodeCoverage 18.5.1 or later (`dotnet test -p:ContinuousIntegrationBuild=true` produces empty coverage but `dotnet test -p:ContinuousIntegrationBuild=false` produces correct coverage) * ~~Stryker: has early support for MTP with `dotnet stryker -t mtp` but the results are currently not 100% (which they should)~~ (fixed in 4.15.0)
1 parent 23c3e6f commit 727dc8e

4 files changed

Lines changed: 43 additions & 20 deletions

File tree

.github/workflows/continuous-integration.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ jobs:
5252
- name: ⚙️ Restore NuGet packages
5353
run: dotnet restore
5454
- name: 🏗 Build solution
55-
run: dotnet build --no-restore
55+
# --configuration should not be needed, see https://github.com/dotnet/sdk/issues/52358
56+
run: dotnet build --no-restore --configuration ${{ env.Configuration }}
5657
- name: 🧪 Run tests
57-
run: dotnet test --no-build
58+
# --configuration should not be needed, see https://github.com/dotnet/sdk/issues/52358
59+
run: dotnet test --no-build --configuration ${{ env.Configuration }}
5860
- name: 📤 Upload received files from failing tests
5961
uses: actions/upload-artifact@v7
6062
if: failure()
@@ -85,7 +87,7 @@ jobs:
8587
if: matrix.os == 'ubuntu-latest' && env.CODECOV_TOKEN != ''
8688
uses: codecov/codecov-action@v6
8789
with:
88-
files: artifacts/coverage/*/coverage.cobertura.xml
90+
files: artifacts/coverage/*/*.cobertura.xml
8991
token: ${{ env.CODECOV_TOKEN }}
9092
- name: ☂️ Upload coverage report to Codacy
9193
env:
@@ -94,9 +96,10 @@ jobs:
9496
uses: codacy/codacy-coverage-reporter-action@v1
9597
with:
9698
project-token: ${{ env.CODACY_PROJECT_TOKEN }}
97-
coverage-reports: artifacts/coverage/*/coverage.cobertura.xml
99+
coverage-reports: artifacts/coverage/*/*.cobertura.xml
98100
- name: 📦 Create NuGet package
99-
run: dotnet pack --no-build --output .
101+
# --configuration should not be needed, see https://github.com/dotnet/sdk/issues/52358
102+
run: dotnet pack --no-build --output . --configuration ${{ env.Configuration }}
100103
- name: 📤 Upload NuGet package artifact
101104
if: matrix.os == 'ubuntu-latest'
102105
uses: actions/upload-artifact@v7
@@ -109,7 +112,7 @@ jobs:
109112
if: matrix.os == 'ubuntu-latest' && env.STRYKER_DASHBOARD_API_KEY != ''
110113
run: |
111114
dotnet tool restore
112-
dotnet tool run dotnet-stryker --reporter dashboard --open-report:dashboard --version ${GITHUB_REF_NAME} --dashboard-api-key ${{ env.STRYKER_DASHBOARD_API_KEY }} --configuration ${{ env.Configuration }}
115+
dotnet tool run dotnet-stryker --test-runner mtp --reporter dashboard --open-report:dashboard --version ${GITHUB_REF_NAME} --dashboard-api-key ${{ env.STRYKER_DASHBOARD_API_KEY }} --configuration ${{ env.Configuration }}
113116
- name: 📝 Retrieve release notes from tag
114117
if: matrix.os == 'ubuntu-latest' && startsWith(github.ref, 'refs/tags/')
115118
run: |

global.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
"version": "8.0.300",
55
"allowPrerelease": false,
66
"rollForward": "latestMajor"
7+
},
8+
"test": {
9+
"runner": "Microsoft.Testing.Platform"
710
}
811
}

tests/Serilog.Formatting.Log4Net.Tests.csproj

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,54 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
45
<TargetFramework>net8.0</TargetFramework>
56
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
<ItemGroup>
910
<PackageReference Include="AwesomeAssertions" Version="9.4.0" />
10-
<PackageReference Include="coverlet.collector" Version="10.0.1" PrivateAssets="all" />
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.7.0" />
11+
<!-- Starting with version 18.5.1, coverage is empty when ContinuousIntegrationBuild is set to true -->
12+
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="[18.4.1, 18.5.1)" />
13+
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="2.2.1" />
1214
<PackageReference Include="PublicApiGenerator" Version="11.5.4" />
1315
<PackageReference Include="ReportGenerator" Version="5.5.10" PrivateAssets="all" />
1416
<PackageReference Include="Serilog" Version="4.3.1" />
1517
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
1618
<PackageReference Include="Serilog.Enrichers.Thread" Version="4.0.0" />
1719
<PackageReference Include="Verify.XunitV3" Version="31.20.0" />
18-
<PackageReference Include="xunit.v3.mtp-off" Version="3.2.2" />
19-
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" PrivateAssets="all" />
20+
<PackageReference Include="xunit.v3.mtp-v2" Version="3.2.2" />
2021
</ItemGroup>
2122

22-
<ItemGroup>
23-
<VSTestLogger Include="trx%3BLogFileName=$(MSBuildProjectName).trx" Visible="false" />
24-
<VSTestLogger Include="html%3BLogFileName=$(MSBuildProjectName).html" Visible="false" />
25-
</ItemGroup>
23+
<PropertyGroup Label="xUnit">
24+
<!-- UseMicrosoftTestingPlatformRunner is required for JetBrains Rider to see the tests when discovery is configured to use metadata (Settings | Build, Execution, Deployment | Unit Testing | xUnit.net) -->
25+
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
26+
<!-- TestingPlatformDotnetTestSupport is ignored with the .NET 10 SDK but is required for older SDKs -->
27+
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
28+
</PropertyGroup>
2629

2730
<PropertyGroup Label="Coverage">
31+
<TestResultsDirectory>$([System.IO.Path]::Combine($(ArtifactsPath),'test-results',$(TargetFramework)))</TestResultsDirectory>
2832
<CoverageReportDirectory>$([System.IO.Path]::Combine($(ArtifactsPath),'coverage',$(TargetFramework)))</CoverageReportDirectory>
29-
<VSTestResultsDirectory>$([System.IO.Path]::Combine($(ArtifactsPath),'test-results',$(TargetFramework)))</VSTestResultsDirectory>
30-
<VSTestCollect>XPlat Code Coverage%3BExcludeByAttribute=GeneratedCodeAttribute</VSTestCollect>
31-
<VSTestLogger>@(VSTestLogger)</VSTestLogger>
33+
<CoverageReportFile>$([System.IO.Path]::Combine($(CoverageReportDirectory),'$(MSBuildProjectName).cobertura.xml'))</CoverageReportFile>
34+
<CoverageSettingsFile>$([System.IO.Path]::GetFullPath($([System.IO.Path]::Combine($(MSBuildThisFileDirectory), 'coverage.xml'))))</CoverageSettingsFile>
35+
<ArgsCoverage>--coverage --coverage-output-format cobertura --coverage-output $(CoverageReportFile) --coverage-settings $(CoverageSettingsFile)</ArgsCoverage>
36+
<ArgsTrx>--report-trx --report-trx-filename $(MSBuildProjectName).trx --results-directory $(TestResultsDirectory)</ArgsTrx>
37+
<TestingPlatformCommandLineArguments>$(ArgsCoverage) $(ArgsTrx)</TestingPlatformCommandLineArguments>
3238
</PropertyGroup>
3339

3440
<Target Name="GenerateCoverageReports" AfterTargets="VSTest" Condition="$(TargetFramework) != ''">
3541
<ItemGroup>
36-
<CoverageReport Include="$(VSTestResultsDirectory)/*/coverage.cobertura.xml" />
42+
<CoverageReport Include="$(CoverageReportDirectory)/*.cobertura.xml" />
3743
</ItemGroup>
3844
<ReportGenerator ReportFiles="@(CoverageReport)" TargetDirectory="$(CoverageReportDirectory)" ReportTypes="HtmlInline;TextSummary" />
3945
<Move SourceFiles="@(CoverageReport)" DestinationFolder="$(CoverageReportDirectory)" />
4046
</Target>
4147

42-
<!-- Because of https://github.com/microsoft/vstest/issues/2334 and https://github.com/microsoft/vstest/issues/2378 -->
48+
<!-- Because of https://github.com/microsoft/vstest/issues/2334 -->
4349
<Target Name="RemoveSpuriousDirectories" AfterTargets="GenerateCoverageReports" Condition="$(TargetFramework) != ''">
4450
<ItemGroup>
45-
<SpuriousDirectory Include="$([System.IO.Directory]::GetDirectories($(VSTestResultsDirectory)))" />
51+
<SpuriousDirectory Include="$([System.IO.Directory]::GetDirectories($(TestResultsDirectory)))" />
4652
</ItemGroup>
4753
<RemoveDir Directories="@(SpuriousDirectory)" />
4854
</Target>

tests/coverage.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- https://learn.microsoft.com/en-us/dotnet/core/additional-tools/dotnet-coverage#settings -->
3+
<Configuration>
4+
<CodeCoverage>
5+
<Attributes>
6+
<Exclude>
7+
<Attribute>^System\.CodeDom\.Compiler\.GeneratedCodeAttribute$</Attribute>
8+
</Exclude>
9+
</Attributes>
10+
</CodeCoverage>
11+
</Configuration>

0 commit comments

Comments
 (0)