-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support of Microsoft.Testing.Platform (#1153)
* Initial support of Microsoft.Testing.Platform * Bump bridge version * Drop telemetry * Enable trace writer fix for NUnit * Bump version of the bridge * Comment out object model v11 * Remove samples and "local" entry in nuget.config * Tests: drop Microsoft.TestPlatform.ObjectModel v11 * Fix ObjectModel v11 test * Fix broken tests The copy of the file was moved from the .props to the .targets * Bump platform version in nuspec * Nit: tab vs space
- Loading branch information
1 parent
a042f08
commit 72e9b6b
Showing
16 changed files
with
293 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
|
||
<!-- Handle the coexistance between Microsoft.Testing.Platform and Microsoft.NET.Test.Sdk --> | ||
<PropertyGroup> | ||
<GenerateTestingPlatformEntryPoint Condition=" '$(GenerateTestingPlatformEntryPoint)' == '' ">$(EnableNUnitRunner)</GenerateTestingPlatformEntryPoint> | ||
<GenerateProgramFile Condition=" '$(EnableNUnitRunner)' == 'true' ">false</GenerateProgramFile> | ||
</PropertyGroup> | ||
|
||
<Choose> | ||
<!-- Avoid false warning about missing reference (msbuild bug) --> | ||
<!-- https://github.com/dotnet/msbuild/issues/9698#issuecomment-1945763467 --> | ||
<When Condition=" '$(EnableNUnitRunner)' == 'true' "> | ||
<ItemGroup> | ||
<Reference Include="NUnit3.TestAdapter"> | ||
<HintPath>$(MSBuildThisFileDirectory)NUnit3.TestAdapter.dll</HintPath> | ||
</Reference> | ||
</ItemGroup> | ||
</When> | ||
<Otherwise> | ||
<ItemGroup> | ||
<None Include="$(MSBuildThisFileDirectory)NUnit3.TestAdapter.dll"> | ||
<Link>NUnit3.TestAdapter.dll</Link> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
<Visible>False</Visible> | ||
</None> | ||
</ItemGroup> | ||
</Otherwise> | ||
</Choose> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
|
||
<!-- Handle the coexistance between Microsoft.Testing.Platform and Microsoft.NET.Test.Sdk --> | ||
<PropertyGroup> | ||
<GenerateTestingPlatformEntryPoint Condition=" '$(GenerateTestingPlatformEntryPoint)' == '' ">$(EnableNUnitRunner)</GenerateTestingPlatformEntryPoint> | ||
<GenerateProgramFile Condition=" '$(EnableNUnitRunner)' == 'true' ">false</GenerateProgramFile> | ||
</PropertyGroup> | ||
|
||
<Choose> | ||
<!-- Avoid false warning about missing reference (msbuild bug) --> | ||
<!-- https://github.com/dotnet/msbuild/issues/9698#issuecomment-1945763467 --> | ||
<When Condition=" '$(EnableNUnitRunner)' == 'true' "> | ||
<ItemGroup> | ||
<Reference Include="NUnit3.TestAdapter"> | ||
<HintPath>$(MSBuildThisFileDirectory)NUnit3.TestAdapter.dll</HintPath> | ||
</Reference> | ||
</ItemGroup> | ||
</When> | ||
<Otherwise> | ||
<ItemGroup> | ||
<None Include="$(MSBuildThisFileDirectory)NUnit3.TestAdapter.dll"> | ||
<Link>NUnit3.TestAdapter.dll</Link> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
<Visible>False</Visible> | ||
</None> | ||
</ItemGroup> | ||
</Otherwise> | ||
</Choose> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/NUnitTestAdapter/TestingPlatformAdapter/NUnitBridgedTestFramework.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using Microsoft.Testing.Extensions.VSTestBridge; | ||
using Microsoft.Testing.Extensions.VSTestBridge.Requests; | ||
using Microsoft.Testing.Platform.Capabilities.TestFramework; | ||
using Microsoft.Testing.Platform.Messages; | ||
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; | ||
|
||
using NUnit.Framework.Internal; | ||
|
||
namespace NUnit.VisualStudio.TestAdapter.TestingPlatformAdapter | ||
{ | ||
internal sealed class NUnitBridgedTestFramework : SynchronizedSingleSessionVSTestBridgedTestFramework | ||
{ | ||
private static readonly object InitializationLock = new(); | ||
private static bool initialized; | ||
|
||
public NUnitBridgedTestFramework(NUnitExtension extension, Func<IEnumerable<Assembly>> getTestAssemblies, | ||
IServiceProvider serviceProvider, ITestFrameworkCapabilities capabilities) | ||
: base(extension, getTestAssemblies, serviceProvider, capabilities) | ||
{ | ||
PatchNUnit3InternalLoggerBug(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override Task SynchronizedDiscoverTestsAsync(VSTestDiscoverTestExecutionRequest request, IMessageBus messageBus, | ||
CancellationToken cancellationToken) | ||
{ | ||
new NUnit3TestDiscoverer() | ||
.DiscoverTests(request.AssemblyPaths, request.DiscoveryContext, request.MessageLogger, request.DiscoverySink); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override Task SynchronizedRunTestsAsync(VSTestRunTestExecutionRequest request, IMessageBus messageBus, | ||
CancellationToken cancellationToken) | ||
{ | ||
ITestExecutor executor = new NUnit3TestExecutor(); | ||
using (cancellationToken.Register(executor.Cancel)) | ||
{ | ||
if (request.VSTestFilter.TestCases is { } testCases) | ||
{ | ||
executor.RunTests(testCases, request.RunContext, request.FrameworkHandle); | ||
} | ||
else | ||
{ | ||
executor.RunTests(request.AssemblyPaths, request.RunContext, request.FrameworkHandle); | ||
} | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private static void PatchNUnit3InternalLoggerBug() | ||
{ | ||
// NUnit3 will internally call InternalTracer.Initialize(...), and it will check that it is not called multiple times. | ||
// We call it multiple times in server mode, in indeterminate order (it does not always have to be Discover and then Run), | ||
// When InternalTracer.Initialize is called with Off, it will not set traceWriter internal field to anything, but when you call it again | ||
// it will try to write "Initialize was already called" via this writer without null check and will fail with NRE. | ||
// | ||
// Patch for this was implemented in NUnit4, but won't be backported to NUnit3 because we are hitting a bit of an edge case: | ||
// https://github.com/nunit/nunit/issues/4255 | ||
// | ||
// To fix us, we set the writer to a null writer, so any subsequent calls to Initialize will write to null instead of failing. | ||
// We also need to do this under a lock, and not rely on the InternalTracer.Initialized, because that might be set to late and we would | ||
// re-enter the method twice. | ||
if (initialized) | ||
{ | ||
return; | ||
} | ||
|
||
lock (InitializationLock) | ||
{ | ||
if (initialized) | ||
{ | ||
return; | ||
} | ||
|
||
var nopWriter = new InternalTraceWriter(new StreamWriter(Stream.Null)); | ||
|
||
// Initialize log level to be Off (see issue https://github.com/microsoft/testanywhere/issues/1369) | ||
// because we don't have settings from the request available yet, and the internal tracer is static, so it would set to the | ||
// level that is set by the first request and always keep it that way. | ||
// | ||
// Alternatively we could hook this up to a ILogger, and write the logs via a TextWriter. | ||
InternalTrace.Initialize(nopWriter, InternalTraceLevel.Off); | ||
|
||
// When we allow the trace level to be set then we need to set the internal writer field only when the level is Off. | ||
// In that case you will need to do something like this: | ||
// FieldInfo traceLevelField = typeof(InternalTrace).GetField("traceLevel", BindingFlags.Static | BindingFlags.NonPublic)!; | ||
// bool isOff = ((InternalTraceLevel)traceLevelField.GetValue(null)!) != InternalTraceLevel.Off; | ||
// if (isOff) | ||
// { | ||
// FieldInfo traceWriterField = typeof(InternalTrace).GetField("traceWriter", BindingFlags.Static | BindingFlags.NonPublic)!; | ||
// traceWriterField.SetValue(null, nopWriter); | ||
// } | ||
|
||
FieldInfo traceWriterField = typeof(InternalTrace).GetField("traceWriter", BindingFlags.Static | BindingFlags.NonPublic)!; | ||
traceWriterField.SetValue(null, nopWriter); | ||
|
||
initialized = true; | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/NUnitTestAdapter/TestingPlatformAdapter/NUnitExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Threading.Tasks; | ||
|
||
using Microsoft.Testing.Platform.Extensions; | ||
|
||
namespace NUnit.VisualStudio.TestAdapter.TestingPlatformAdapter | ||
{ | ||
internal sealed class NUnitExtension : IExtension | ||
{ | ||
public string Uid => nameof(NUnitExtension); | ||
|
||
public string DisplayName => "NUnit"; | ||
|
||
// TODO: Decide whether to read from assembly or use hardcoded string. | ||
public string Version => "4.5.0"; | ||
|
||
public string Description => "NUnit adapter for Microsoft Testing Platform"; | ||
|
||
public Task<bool> IsEnabledAsync() => Task.FromResult(true); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/NUnitTestAdapter/TestingPlatformAdapter/TestApplicationBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
using Microsoft.Testing.Extensions.VSTestBridge.Capabilities; | ||
using Microsoft.Testing.Extensions.VSTestBridge.Helpers; | ||
using Microsoft.Testing.Platform.Builder; | ||
using Microsoft.Testing.Platform.Capabilities.TestFramework; | ||
|
||
namespace NUnit.VisualStudio.TestAdapter.TestingPlatformAdapter | ||
{ | ||
public static class TestApplicationBuilderExtensions | ||
{ | ||
public static void AddNUnit(this ITestApplicationBuilder testApplicationBuilder, Func<IEnumerable<Assembly>> getTestAssemblies) | ||
{ | ||
NUnitExtension extension = new(); | ||
testApplicationBuilder.AddRunSettingsService(extension); | ||
testApplicationBuilder.AddTestCaseFilterService(extension); | ||
testApplicationBuilder.RegisterTestFramework( | ||
_ => new TestFrameworkCapabilities(new VSTestBridgeExtensionBaseCapabilities()), | ||
(capabilities, serviceProvider) => new NUnitBridgedTestFramework(extension, getTestAssemblies, serviceProvider, capabilities)); | ||
} | ||
} | ||
} |
Oops, something went wrong.
There's a problem here. The hooks which will be introduced with the new framework will be added but currently the nuspec do not add direct reference to the NUnit3.TestAdapter. Thus any inherited projects will face a build error that the TestPlatformEntryPoint.cs is missing a reference to:
NUnit.VisualStudio.TestAdapter.TestingPlatformAdapter.TestingPlatformBuilderHook.AddExtensions(builder, args);
The .target file contains a condition which is tied to '$(EnableNUnitRunner)' == 'true' but in my opinion it should be tied to GenerateTestingPlatformEntryPoint as the platorm will rely on that. If the package is added and the setting is set we need that reference.