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 all commits
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.10.0</MicrosoftNetTestSdkVersion>
<MicrosoftSourceLinkGitHubVersion>8.0.0</MicrosoftSourceLinkGitHubVersion>
<MicrosoftTestingPlatformVersion>1.2.1</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=".\artifacts\packages\" />
</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()
{

}
}
27 changes: 27 additions & 0 deletions samples/xunit-runner-sample/xunit-runner-sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<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.4" />
</ItemGroup>

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

</Project>
22 changes: 22 additions & 0 deletions samples/xunit-runner-sample/xunit-runner-sample.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "xunit-runner-sample", "xunit-runner-sample.csproj", "{CA7073C4-D751-474E-80E0-C252F722DBAE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CA7073C4-D751-474E-80E0-C252F722DBAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CA7073C4-D751-474E-80E0-C252F722DBAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CA7073C4-D751-474E-80E0-C252F722DBAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CA7073C4-D751-474E-80E0-C252F722DBAE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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,12 @@
using System.Reflection;
using Microsoft.Testing.Platform.Builder;

namespace Xunit.Runner.VisualStudio;

public static class TestingPlatformBuilderHook
{
public static void AddExtensions(
ITestApplicationBuilder testApplicationBuilder,
string[] _) =>
testApplicationBuilder.AddXunit(() => [Assembly.GetEntryAssembly()!]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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;

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))
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))
if (request.VSTestFilter.TestCases is { } testCases)
runner.RunTests(testCases, request.RunContext, request.FrameworkHandle);
else
runner.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.net";

public string Version => ThisAssembly.AssemblyVersion;

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

public Task<bool> IsEnabledAsync() => Task.FromResult(true);
}
6 changes: 3 additions & 3 deletions src/xunit.runner.visualstudio/VsTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void Cancel()
cancelled = true;
}

void ITestDiscoverer.DiscoverTests(
public void DiscoverTests(
IEnumerable<string> sources,
IDiscoveryContext discoveryContext,
IMessageLogger logger,
Expand Down Expand Up @@ -153,7 +153,7 @@ static void PrintHeader(LoggerHelper loggerHelper)
loggerHelper.Log($"xUnit.net VSTest Adapter v{ThisAssembly.AssemblyInformationalVersion} ({IntPtr.Size * 8}-bit {RuntimeInformation.FrameworkDescription})");
}

void ITestExecutor.RunTests(
public void RunTests(
IEnumerable<string>? sources,
IRunContext? runContext,
IFrameworkHandle? frameworkHandle)
Expand Down Expand Up @@ -182,7 +182,7 @@ void ITestExecutor.RunTests(
);
}

void ITestExecutor.RunTests(
public void RunTests(
IEnumerable<TestCase>? tests,
IRunContext? runContext,
IFrameworkHandle? frameworkHandle)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
<?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>

<PropertyGroup Condition=" '$(EnableXunitRunner)' == 'true' ">
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<!-- This GUID comes from Microsoft.Testing.Platform, do not modify -->
Copy link
Author

Choose a reason for hiding this comment

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

The GUID is actually just any random GUID but it needs to be unique across the various extensions registered through the hook.

<TestingPlatformBuilderHook Include="17E773D9-071C-4C66-97DD-57A450BDB027" Condition=" '$(GenerateTestingPlatformEntryPoint)' == 'true' " >
<DisplayName>xUnit.net</DisplayName>
<TypeFullName>Xunit.Runner.VisualStudio.TestingPlatformBuilderHook</TypeFullName>
</TestingPlatformBuilderHook>
</ItemGroup>

<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)xunit.runner.visualstudio.testadapter.dll">
<Link>xunit.runner.visualstudio.testadapter.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>False</Visible>
</None>
<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,23 @@
<?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>

<PropertyGroup Condition=" '$(EnableXunitRunner)' == 'true' ">
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<!-- This GUID comes from Microsoft.Testing.Platform, do not modify -->
<TestingPlatformBuilderHook Include="17E773D9-071C-4C66-97DD-57A450BDB027" Condition=" '$(GenerateTestingPlatformEntryPoint)' == 'true' " >
<DisplayName>xUnit.net</DisplayName>
<TypeFullName>Xunit.Runner.VisualStudio.TestingPlatformBuilderHook</TypeFullName>
</TestingPlatformBuilderHook>
</ItemGroup>

<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)xunit.runner.visualstudio.testadapter.dll">
<Link>xunit.runner.visualstudio.testadapter.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>False</Visible>
</None>
<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 coexistence between testing platform and Microsoft.NET.Test.Sdk -->
<PropertyGroup>
<GenerateTestingPlatformEntryPoint Condition=" '$(GenerateTestingPlatformEntryPoint)' == '' ">$(EnableXunitRunner)</GenerateTestingPlatformEntryPoint>
<GenerateProgramFile Condition=" '$(EnableXunitRunner)' == 'true' ">false</GenerateProgramFile>
<DisableTestingPlatformServerCapability Condition=" '$(EnableXunitRunner)' != 'true' " >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 @@ -59,6 +60,7 @@
Configuration=$(Configuration);
GitCommitId=$(GitCommitId);
MicrosoftTestPlatformObjectModelVersion=$(MicrosoftTestPlatformObjectModelVersion);
MicrosoftTestingPlatformVersion=$(MicrosoftTestingPlatformVersion);
PackageVersion=$(PackageVersion);
SignedPath=$(SignedPath);
</NuspecProperties>
Expand Down
10 changes: 8 additions & 2 deletions src/xunit.runner.visualstudio/xunit.runner.visualstudio.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
<description>Visual Studio 2022+ Test Explorer runner for the xUnit.net framework. Capable of running xUnit.net v1.9.2 and v2.0+ tests. Supports .NET 4.6.2 or later, and .NET 6 or later.</description>
<copyright>Copyright (C) .NET Foundation</copyright>
<repository type="git" url="https://github.com/xunit/visualstudio.xunit" commit="$GitCommitId$" />
<developmentDependency>true</developmentDependency>
<dependencies>
<group targetFramework="net462">
<dependency id="Microsoft.TestPlatform.ObjectModel" version="$MicrosoftTestPlatformObjectModelVersion$" />
<dependency id="Microsoft.Testing.Extensions.VSTestBridge" version="$MicrosoftTestingPlatformVersion$" />
<dependency id="Microsoft.Testing.Platform.MSBuild" version="$MicrosoftTestingPlatformVersion$" />
</group>
<group targetFramework="net6.0">
<dependency id="Microsoft.Testing.Extensions.VSTestBridge" version="$MicrosoftTestingPlatformVersion$" />
<dependency id="Microsoft.Testing.Platform.MSBuild" version="$MicrosoftTestingPlatformVersion$" />
</group>
<group targetFramework="net6.0" />
</dependencies>
<frameworkAssemblies>
<frameworkAssembly assemblyName="mscorlib" targetFramework="net462" />
Expand All @@ -35,12 +39,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