Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial support for Microsoft.Testing.Platform #403

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<MicrosoftNetCoreAppRefVersion>6.0.11</MicrosoftNetCoreAppRefVersion>
<MicrosoftNetTestSdkVersion>17.8.0</MicrosoftNetTestSdkVersion>
<MicrosoftSourceLinkGitHubVersion>8.0.0</MicrosoftSourceLinkGitHubVersion>
<MicrosoftTestingPlatformVersion>1.0.2</MicrosoftTestingPlatformVersion>
<MicrosoftTestPlatformObjectModelVersion>$(MicrosoftNetTestSdkVersion)</MicrosoftTestPlatformObjectModelVersion>
<NerdbankGitVersioningVersion>3.6.133</NerdbankGitVersioningVersion>
<NSubstituteVersion>5.1.0</NSubstituteVersion>
Expand Down
9 changes: 9 additions & 0 deletions samples/NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="feedz.io/xunit/xunit" value="https://f.feedz.io/xunit/xunit/nuget/index.json" protocolVersion="3" />
<add key="local" value="C:\src\visualstudio.xunit\artifacts\packages\" />
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be kept, this is just a temp change for you to test locally (you'll need to update the path as you know).

</packageSources>
</configuration>
10 changes: 10 additions & 0 deletions samples/xunit-runner-sample/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace xunit_runner_sample;

public class UnitTest1
{
[Fact]
public void Test1()
{

}
}
30 changes: 30 additions & 0 deletions samples/xunit-runner-sample/xunit-runner-sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net462;net6.0</TargetFrameworks>
<RootNamespace>xunit_runner_sample</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>

<EnableXunitRunner>true</EnableXunitRunner>
<OutputType>Exe</OutputType>
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8-pre.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 Xunit.Runner.VisualStudio;

public static class TestApplicationBuilderExtensions
{
public static void AddXunit(this ITestApplicationBuilder testApplicationBuilder, Func<IEnumerable<Assembly>> getTestAssemblies)
{
XunitExtension extension = new();
testApplicationBuilder.AddRunSettingsService(extension);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This provides the runsettings support.

testApplicationBuilder.AddTestCaseFilterService(extension);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This provides support for the vstest way of filtering tests.

testApplicationBuilder.RegisterTestFramework(
_ => new TestFrameworkCapabilities(new VSTestBridgeExtensionBaseCapabilities()),
(capabilities, serviceProvider) => new XunitBridgedTestFramework(extension, getTestAssemblies, serviceProvider, capabilities));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Reflection;

using Microsoft.Testing.Platform.Builder;

namespace Xunit.Runner.VisualStudio;

public static class TestingPlatformBuilderHook
{
#pragma warning disable IDE0060 // Remove unused parameter
public static void AddExtensions(ITestApplicationBuilder testApplicationBuilder, string[] arguments)
#pragma warning restore IDE0060 // Remove unused parameter
{
testApplicationBuilder.AddXunit(() => [Assembly.GetEntryAssembly()!]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
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;

namespace Xunit.Runner.VisualStudio;

internal sealed class XunitBridgedTestFramework : SynchronizedSingleSessionVSTestBridgedTestFramework
{
public XunitBridgedTestFramework(XunitExtension extension, Func<IEnumerable<Assembly>> getTestAssemblies,
IServiceProvider serviceProvider, ITestFrameworkCapabilities capabilities)
: base(extension, getTestAssemblies, serviceProvider, capabilities)
{
}

/// <inheritdoc />
protected override Task SynchronizedDiscoverTestsAsync(VSTestDiscoverTestExecutionRequest request, IMessageBus messageBus,
CancellationToken cancellationToken)
{
var discoverer = new VsTestRunner();
using (cancellationToken.Register(discoverer.Cancel))
{
((ITestDiscoverer)discoverer).DiscoverTests(request.AssemblyPaths, request.DiscoveryContext, request.MessageLogger, request.DiscoverySink);
}

return Task.CompletedTask;
}

/// <inheritdoc />
protected override Task SynchronizedRunTestsAsync(VSTestRunTestExecutionRequest request, IMessageBus messageBus,
CancellationToken cancellationToken)
{
var runner = new VsTestRunner();
using (cancellationToken.Register(runner.Cancel))
{
var executor = (ITestExecutor)runner;
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;
}
}
17 changes: 17 additions & 0 deletions src/xunit.runner.visualstudio/TestingPlatform/XunitExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Threading.Tasks;
using Microsoft.Testing.Platform.Extensions;

namespace Xunit.Runner.VisualStudio;

internal sealed class XunitExtension : IExtension
{
public string Uid => nameof(XunitExtension);

public string DisplayName => "xUnit";

public string Version => ThisAssembly.AssemblyVersion;

public string Description => "xUnit Framework for Microsoft Testing Platform";

public Task<bool> IsEnabledAsync() => Task.FromResult(true);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<EnableXunitRunner Condition=" '$(EnableXunitRunner)' == '' ">false</EnableXunitRunner>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll let you decide on the name, this is just an example.

<IsTestingPlatformApplication>$(EnableXunitRunner)</IsTestingPlatformApplication>
</PropertyGroup>

<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)xunit.runner.visualstudio.testadapter.dll">
<Link>xunit.runner.visualstudio.testadapter.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>False</Visible>
</None>
<!--
!!! IMPORTANT !!!
DO NOT CHANGE THE GUID, IT'S A WELL KNOWN EXTENSION POINT AND THIS EXTENSION NEEDS TO BE REGISTERED AT THE END
WE HAVE CODE INSIDE THE TASK 'TestingPlatformEntryPoint' TO ENSURE THE ORDER OF THE REGISTRATION BASED ON THIS GUID
-->
<TestingPlatformBuilderHook Include="17E773D9-071C-4C66-97DD-57A450BDB027" Condition=" $(GenerateTestingPlatformEntryPoint) == 'true' " >
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allows Microsoft.Testing.Platform.MSBuild to detect the hook to call so we can auto-generate the entry point.

<DisplayName>xUnit</DisplayName>
<TypeFullName>Xunit.Runner.VisualStudio.TestingPlatformBuilderHook</TypeFullName>
</TestingPlatformBuilderHook>
</ItemGroup>

<ItemGroup>
<!-- Adapter dll is included in the .targets -->
<None Include="$(MSBuildThisFileDirectory)xunit.abstractions.dll">
<Link>xunit.abstractions.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<EnableXunitRunner Condition=" '$(EnableXunitRunner)' == '' ">false</EnableXunitRunner>
<IsTestingPlatformApplication>$(EnableXunitRunner)</IsTestingPlatformApplication>
</PropertyGroup>

<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)xunit.runner.visualstudio.testadapter.dll">
<Link>xunit.runner.visualstudio.testadapter.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>False</Visible>
</None>
<!--
!!! IMPORTANT !!!
DO NOT CHANGE THE GUID, IT'S A WELL KNOWN EXTENSION POINT AND THIS EXTENSION NEEDS TO BE REGISTERED AT THE END
WE HAVE CODE INSIDE THE TASK 'TestingPlatformEntryPoint' TO ENSURE THE ORDER OF THE REGISTRATION BASED ON THIS GUID
-->
<TestingPlatformBuilderHook Include="17E773D9-071C-4C66-97DD-57A450BDB027" Condition=" $(GenerateTestingPlatformEntryPoint) == 'true' " >
<DisplayName>xUnit</DisplayName>
<TypeFullName>Xunit.Runner.VisualStudio.TestingPlatformBuilderHook</TypeFullName>
</TestingPlatformBuilderHook>
</ItemGroup>

<ItemGroup>
<!-- Adapter dll is included in the .targets -->
<None Include="$(MSBuildThisFileDirectory)xunit.runner.reporters.netcoreapp10.dll">
<Link>xunit.runner.reporters.netcoreapp10.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<!-- Handle the coexistance between testing platform and Microsoft.NET.Test.Sdk -->
<PropertyGroup>
<GenerateTestingPlatformEntryPoint Condition=" '$(GenerateTestingPlatformEntryPoint)' == '' ">$(EnableXunitRunner)</GenerateTestingPlatformEntryPoint>
<GenerateProgramFile Condition=" '$(EnableXunitRunner)' == 'true' ">false</GenerateProgramFile>
<DisableTestingPlatformServerCapability Condition=" '$(EnableXunitRunner)' == 'false' or '$(EnableXunitRunner)' == '' " >true</DisableTestingPlatformServerCapability>
</PropertyGroup>

<Choose>
<!-- Avoid false warning about missing reference (msbuild bug) -->
<!-- https://github.com/dotnet/msbuild/issues/9698#issuecomment-1945763467 -->
<When Condition=" '$(EnableXunitRunner)' == 'true' ">
<ItemGroup>
<Reference Include="xunit.runner.visualstudio.testadapter">
<HintPath>$(MSBuildThisFileDirectory)xunit.runner.visualstudio.testadapter.dll</HintPath>
</Reference>
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)xunit.runner.visualstudio.testadapter.dll">
<Link>xunit.runner.visualstudio.testadapter.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>False</Visible>
</None>
</ItemGroup>
</Otherwise>
</Choose>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="$(MicrosoftTestPlatformObjectModelVersion)" PrivateAssets="All" />
<PackageReference Include="Microsoft.Testing.Extensions.VSTestBridge" Version="$(MicrosoftTestingPlatformVersion)" />
<PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="$(TunnelVisionLabsReferenceAssemblyAnnotatorVersion)" PrivateAssets="All" />
<PackageReference Include="xunit.analyzers" Version="$(XunitAnalyzersVersion)" PrivateAssets="All" />
<PackageReference Include="xunit.runner.reporters" Version="$(XunitVersion)" />
Expand Down Expand Up @@ -55,6 +56,7 @@
Configuration=$(Configuration);
GitCommitId=$(GitCommitId);
MicrosoftTestPlatformObjectModelVersion=$(MicrosoftTestPlatformObjectModelVersion);
MicrosoftTestingPlatformVersion=$(MicrosoftTestingPlatformVersion);
PackageVersion=$(PackageVersion);
SignedPath=$(SignedPath);
</NuspecProperties>
Expand Down
11 changes: 10 additions & 1 deletion src/xunit.runner.visualstudio/xunit.runner.visualstudio.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@
<dependencies>
<group targetFramework="net462">
<dependency id="Microsoft.TestPlatform.ObjectModel" version="$MicrosoftTestPlatformObjectModelVersion$" />
<dependency id="Microsoft.Testing.Extensions.Telemetry" version="$MicrosoftTestingPlatformVersion$" />
<dependency id="Microsoft.Testing.Extensions.VSTestBridge" version="$MicrosoftTestingPlatformVersion$" />
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why the indent is all messed up, I don't see that locally:

Uploading image.png…

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is mixing tabs/spaces, you'll need to configure your editor to show whistespaces

<dependency id="Microsoft.Testing.Platform.MSBuild" version="$MicrosoftTestingPlatformVersion$" />
</group>
<group targetFramework="net6.0" />
<group targetFramework="net6.0">
<dependency id="Microsoft.Testing.Extensions.Telemetry" version="$MicrosoftTestingPlatformVersion$" />
<dependency id="Microsoft.Testing.Extensions.VSTestBridge" version="$MicrosoftTestingPlatformVersion$" />
<dependency id="Microsoft.Testing.Platform.MSBuild" version="$MicrosoftTestingPlatformVersion$" />
</group>
</dependencies>
<frameworkAssemblies>
<frameworkAssembly assemblyName="mscorlib" targetFramework="net462" />
Expand All @@ -35,12 +42,14 @@
<file target="build\net462\" src="bin\$Configuration$\net462\xunit.runner.utility.net452.dll" />
<file target="build\net462\" src="bin\$Configuration$\net462\$SignedPath$xunit.runner.visualstudio.testadapter.dll" />
<file target="build\net462\xunit.runner.visualstudio.props" src="build\xunit.runner.visualstudio.desktop.props" />
<file target="build\net462\xunit.runner.visualstudio.targets" src="build\xunit.runner.visualstudio.targets" />

<file target="build\net6.0\" src="bin\$Configuration$\net6.0\xunit.abstractions.dll" />
<file target="build\net6.0\" src="bin\$Configuration$\net6.0\xunit.runner.reporters.netcoreapp10.dll" />
<file target="build\net6.0\" src="bin\$Configuration$\net6.0\xunit.runner.utility.netcoreapp10.dll" />
<file target="build\net6.0\" src="bin\$Configuration$\net6.0\$SignedPath$xunit.runner.visualstudio.testadapter.dll" />
<file target="build\net6.0\xunit.runner.visualstudio.props" src="build\xunit.runner.visualstudio.dotnetcore.props" />
<file target="build\net6.0\xunit.runner.visualstudio.targets" src="build\xunit.runner.visualstudio.targets" />

<file target="lib\net462\" src="build\_._" />

Expand Down