Skip to content

Commit ef9ddb1

Browse files
authored
feat!: use MTP v2 in testlogger (#214)
Versions 8.x onward will support MTPv2 and VSTest runners. Please use v7.x for MTPv1 loggers. Fixes #209
1 parent 0adb7df commit ef9ddb1

15 files changed

Lines changed: 99 additions & 38 deletions

File tree

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
---
22
name: Bug report
33
about: Create a report to help us improve
4-
title: ''
5-
labels: ''
6-
assignees: ''
7-
4+
title: ""
5+
labels: ""
6+
assignees: ""
87
---
98

109
**Describe the bug**
11-
A clear and concise description of what the bug is.
10+
A clear and concise description of the problem.
1211

12+
- **Test Runner** (choose one): VSTest/MTPv1/MTPv2
1313
- **Test Framework** (choose one): MSTest/Nunit/Xunit
14-
- **.NET project version, sdk version** (choose applicable): net7.0/net8.0/net48
14+
- **.NET project version, sdk version** (choose applicable): net10.0/net48
1515
- **Command line used** (please share the test run command): `dotnet test --logger:xyz...`
1616
- **OS**: Linux/Windows/Mac
1717

@@ -20,8 +20,8 @@ A clear and concise description of what you expected to happen.
2020

2121
**Other details**
2222

23-
- [ ] Here's a minimal repro project <include link to a git repo>
24-
- [ ] I have attached logs from `dotnet test --diag:log.txt` to the issue. Please redact any confidential info from the logs before attaching. See https://github.com/spekt/testlogger/wiki/Collecting-vstest-logs
23+
- [ ] Minimal repro project <include link to a git repo>
24+
- [ ] Logs from `dotnet test --diag:log.txt` are attached. Please redact any confidential info from the logs before attaching. See https://github.com/spekt/testlogger/wiki/Collecting-vstest-logs
2525

2626
**Additional context**
2727
Add any other context about the problem here.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,4 @@ site
344344
.vscode/settings.json
345345

346346
*.received.txt
347+
global.json

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup>
33
<SourceRoot Condition="$(SourceRoot) == ''">$(MSBuildThisFileDirectory)</SourceRoot>
4-
<SourcePrefix>7.0.1</SourcePrefix>
4+
<SourcePrefix>8.0.0</SourcePrefix>
55
</PropertyGroup>
66

77
<PropertyGroup>

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66
<ItemGroup>
77
<PackageVersion Include="Microsoft.TestPlatform.ObjectModel" Version="15.5.0" />
8-
<PackageVersion Include="Microsoft.Testing.Platform" Version="1.8.4" />
8+
<PackageVersion Include="Microsoft.Testing.Platform" Version="2.0.2" />
99
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
1010

1111
<PackageVersion Include="Stylecop.Analyzers" Version="1.2.0-beta.556" />

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Junit, NUnit and Xunit logger/reporter extensions for [Visual Studio Test Platfo
77

88
## Packages
99

10+
> [!IMPORTANT]
11+
> If you're using MTPv1 runner, kindly use v7.x of the loggers.
12+
> v8+ supports MTPv2 and VSTest runners.
13+
1014
| Logger | Stable Package | Pre-release Package | Usage |
1115
| ------ | ----------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------- |
1216
| JUnit | [![NuGet](https://img.shields.io/nuget/v/JUnitXml.TestLogger.svg)](https://www.nuget.org/packages/JUnitXml.TestLogger/) | [![MyGet Pre Release](https://img.shields.io/myget/spekt/vpre/junitxml.testlogger.svg)](https://www.myget.org/feed/spekt/package/nuget/JunitXml.TestLogger) | [README](src/JUnit.Xml.Package/README.md) |
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<!-- This project ensures we accidentally don't break MTP v1. No assemblies or tests use this. -->
4+
<PropertyGroup>
5+
<TargetFramework>netstandard2.0</TargetFramework>
6+
<AssemblyName>Spekt.TestLogger</AssemblyName>
7+
<Version>$(PackageVersion)</Version>
8+
<IsPackable>true</IsPackable>
9+
<DebugType>embedded</DebugType>
10+
<EmbedAllSources>true</EmbedAllSources>
11+
<DefineConstants>$(DefineConstants);MTP_V1</DefineConstants>
12+
</PropertyGroup>
13+
14+
<PropertyGroup Label="Configuration">
15+
<RootNamespace>Spekt.TestLogger</RootNamespace>
16+
</PropertyGroup>
17+
18+
<ItemGroup>
19+
<Compile Include="../TestLogger/**/*.cs" Exclude="../TestLogger/obj/**;../TestLogger/bin/**" />
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" />
24+
<PackageReference Include="Microsoft.Testing.Platform" VersionOverride="1.9.1"/>
25+
</ItemGroup>
26+
27+
</Project>

src/TestLogger/TestReporter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,22 @@ public Task<bool> IsEnabledAsync()
8787
return Task.FromResult(isEnabled);
8888
}
8989

90+
#if MTP_V1
9091
public Task OnTestSessionStartingAsync(SessionUid sessionUid, CancellationToken cancellationToken)
92+
#else
93+
public Task OnTestSessionStartingAsync(ITestSessionContext testSessionContext)
94+
#endif
9195
{
9296
var assembly = Assembly.GetEntryAssembly();
9397
((TestRun)this.testRun).RunConfiguration = this.testRun.Start(assembly.Location, assembly.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName ?? "unknown-targetframework");
9498
return Task.CompletedTask;
9599
}
96100

101+
#if MTP_V1
97102
public Task OnTestSessionFinishingAsync(SessionUid sessionUid, CancellationToken cancellationToken)
103+
#else
104+
public Task OnTestSessionFinishingAsync(ITestSessionContext testSessionContext)
105+
#endif
98106
{
99107
this.testRun.Complete(this.testAttachmentInfos);
100108
return Task.CompletedTask;

test/TestLogger.Fixtures/DotnetTestFixture.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@ public string Execute(string assemblyName, string loggerArgs, bool collectCovera
5757
cleanProcess.WaitForExit();
5858
}
5959

60-
if (!isMTP)
60+
// Clean up global.json to allow running both VSTest and MTP tests in the same build
61+
var globalJsonTemplate = Path.Combine(assemblyName.ToAssetDirectoryPath(), "..", "global.json.template");
62+
var globalJsonPath = Path.Combine(assemblyName.ToAssetDirectoryPath(), "..", "global.json");
63+
if (File.Exists(globalJsonPath))
6164
{
62-
loggerArgs = $"--logger:\"{loggerArgs}\"";
65+
File.Delete(globalJsonPath);
6366
}
6467

6568
var resultsDirectory = Path.Combine(assemblyName.ToAssetDirectoryPath(), this.relativeResultsDirectory);
@@ -71,27 +74,36 @@ public string Execute(string assemblyName, string loggerArgs, bool collectCovera
7174

7275
// Run dotnet test with logger
7376
var buildArgs = this.buildProject ? string.Empty : "--no-build";
77+
var resultDirectoryArgs = string.IsNullOrEmpty(this.relativeResultsDirectory) ? string.Empty : $"--results-directory \"{resultsDirectory}\"";
78+
7479
if (isMTP)
7580
{
7681
buildArgs += " -p:IsMTP=true";
77-
}
82+
if (resultDirectoryArgs.Length == 0)
83+
{
84+
resultDirectoryArgs = $"--results-directory \"{resultsDirectory}\"";
85+
}
7886

79-
var resultDirectoryArgs = string.IsNullOrEmpty(this.relativeResultsDirectory) ? string.Empty : $"--results-directory \"{resultsDirectory}\"";
80-
if (isMTP && resultDirectoryArgs.Length == 0)
87+
File.Copy(globalJsonTemplate, globalJsonPath);
88+
}
89+
else
8190
{
82-
resultDirectoryArgs = $"--results-directory \"{resultsDirectory}\"";
91+
loggerArgs = $"--logger:\"{loggerArgs}\"";
8392
}
8493

94+
var testProjectPath = Path.Combine(assemblyName.ToAssetDirectoryPath(), $"{assemblyName}.csproj");
8595
var commandlineSuffix = string.IsNullOrEmpty(this.runSettingsSuffix) ? string.Empty : $"--{(isMTP ? "test-parameter" : string.Empty)} {this.runSettingsSuffix}";
96+
var testCommand = isMTP ? $"test --project \"{testProjectPath}\"" : $"test \"{testProjectPath}\"";
8697
using var dotnet = new Process
8798
{
8899
StartInfo =
89100
{
101+
WorkingDirectory = assemblyName.ToAssetDirectoryPath(),
90102
UseShellExecute = false,
91103
RedirectStandardOutput = true,
92104
RedirectStandardError = true,
93105
FileName = "dotnet",
94-
Arguments = $"test \"{assemblyName.ToAssetDirectoryPath()}\\{assemblyName}.csproj\" {buildArgs}{(isMTP ? " --" : string.Empty)} {loggerArgs} {resultDirectoryArgs} {commandlineSuffix}"
106+
Arguments = $"{testCommand} {buildArgs} {loggerArgs} {resultDirectoryArgs} {commandlineSuffix}"
95107
}
96108
};
97109

@@ -104,7 +116,8 @@ public string Execute(string assemblyName, string loggerArgs, bool collectCovera
104116
throw new NotSupportedException("Coverlet isn't supported with MTP yet.");
105117
}
106118

107-
dotnet.StartInfo.Arguments += " --collect:\"XPlat Code Coverage\" --settings coverlet.runsettings";
119+
var coverletRunSettingsPath = Path.Combine(Environment.CurrentDirectory, "coverlet.runsettings");
120+
dotnet.StartInfo.Arguments += $" --collect:\"XPlat Code Coverage\" --settings \"{coverletRunSettingsPath}\"";
108121
}
109122

110123
this.LogTestAssetOutDir(assemblyName);

test/assets/Directory.Build.props

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
<TestingPlatformCaptureOutput>false</TestingPlatformCaptureOutput>
3232

3333
<!-- Need a combination with VSTestBridge >= 1.82.0 dependency -->
34-
<NUnitTestAdapterVersion>5.1.1-alpha.1</NUnitTestAdapterVersion>
35-
<MSTestVersion>3.10.1</MSTestVersion>
34+
<NUnitTestAdapterVersion>6.0.0</NUnitTestAdapterVersion>
35+
<MSTestVersion>4.0.0</MSTestVersion>
36+
<XunitV3Version>3.2.1</XunitV3Version>
37+
<XunitV3TestAdapterVersion>3.1.5</XunitV3TestAdapterVersion>
3638
</PropertyGroup>
3739

3840
<ItemGroup>

test/assets/JUnit.Xml.TestLogger.XUnit.NetCore.Tests/JUnit.Xml.TestLogger.XUnit.NetCore.Tests.csproj

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
5-
<TargetFramework Condition="'$(UsesXunit3)'=='true'">net10.0</TargetFramework>
6-
<!-- Disable stylecop for test assets -->
7-
<StylecopEnabled>false</StylecopEnabled>
5+
<!-- <OutputType Condition="'$(UsesXunit3)'=='true'">exe</OutputType> -->
86
</PropertyGroup>
97

108
<ItemGroup>
@@ -13,11 +11,11 @@
1311

1412
<ItemGroup>
1513
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(NETTestSdkMinimumVersion)" />
16-
<PackageReference Include="xunit" Version="$(XunitVersion)" Condition="'$(UsesXunit3)' != 'true'" />
17-
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitTestAdapterVersion)" Condition="'$(UsesXunit3)' != 'true'" />
14+
<PackageReference Condition="'$(UsesXunit3)' != 'true'" Include="xunit" Version="$(XunitVersion)" />
15+
<PackageReference Condition="'$(UsesXunit3)' != 'true'" Include="xunit.runner.visualstudio" Version="$(XunitTestAdapterVersion)" />
1816

19-
<PackageReference Include="xunit.v3" Version="$(XunitV3Version)" Condition="'$(UsesXunit3)' == 'true'" />
20-
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitV3TestAdapterVersion)" Condition="'$(UsesXunit3)' == 'true'" />
17+
<PackageReference Condition="'$(UsesXunit3)' == 'true'" Include="xunit.v3.mtp-v2" Version="$(XunitV3Version)" />
18+
<PackageReference Condition="'$(UsesXunit3)' == 'true'" Include="xunit.runner.visualstudio" Version="$(XunitV3TestAdapterVersion)" />
2119
</ItemGroup>
2220

2321
<ItemGroup>

0 commit comments

Comments
 (0)