diff --git a/Versions.props b/Versions.props index 373103b..ecf0ab2 100644 --- a/Versions.props +++ b/Versions.props @@ -4,6 +4,7 @@ 6.0.11 17.10.0 8.0.0 + 1.2.1 $(MicrosoftNetTestSdkVersion) 3.6.133 5.1.0 diff --git a/samples/NuGet.Config b/samples/NuGet.Config new file mode 100644 index 0000000..284a42a --- /dev/null +++ b/samples/NuGet.Config @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/samples/xunit-runner-sample/UnitTest1.cs b/samples/xunit-runner-sample/UnitTest1.cs new file mode 100644 index 0000000..931038b --- /dev/null +++ b/samples/xunit-runner-sample/UnitTest1.cs @@ -0,0 +1,10 @@ +namespace xunit_runner_sample; + +public class UnitTest1 +{ + [Fact] + public void Test1() + { + + } +} diff --git a/samples/xunit-runner-sample/xunit-runner-sample.csproj b/samples/xunit-runner-sample/xunit-runner-sample.csproj new file mode 100644 index 0000000..c5bf60c --- /dev/null +++ b/samples/xunit-runner-sample/xunit-runner-sample.csproj @@ -0,0 +1,27 @@ + + + + net462;net6.0 + xunit_runner_sample + enable + enable + + false + true + + true + Exe + true + + + + + + + + + + + + + diff --git a/samples/xunit-runner-sample/xunit-runner-sample.sln b/samples/xunit-runner-sample/xunit-runner-sample.sln new file mode 100644 index 0000000..a64082b --- /dev/null +++ b/samples/xunit-runner-sample/xunit-runner-sample.sln @@ -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 diff --git a/src/xunit.runner.visualstudio/TestingPlatform/TestApplicationBuilderExtensions.cs b/src/xunit.runner.visualstudio/TestingPlatform/TestApplicationBuilderExtensions.cs new file mode 100644 index 0000000..6abbb8b --- /dev/null +++ b/src/xunit.runner.visualstudio/TestingPlatform/TestApplicationBuilderExtensions.cs @@ -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> getTestAssemblies) + { + XunitExtension extension = new(); + testApplicationBuilder.AddRunSettingsService(extension); + testApplicationBuilder.AddTestCaseFilterService(extension); + testApplicationBuilder.RegisterTestFramework( + _ => new TestFrameworkCapabilities(new VSTestBridgeExtensionBaseCapabilities()), + (capabilities, serviceProvider) => new XunitBridgedTestFramework(extension, getTestAssemblies, serviceProvider, capabilities) + ); + } +} diff --git a/src/xunit.runner.visualstudio/TestingPlatform/TestingPlatformBuilderHook.cs b/src/xunit.runner.visualstudio/TestingPlatform/TestingPlatformBuilderHook.cs new file mode 100644 index 0000000..b531deb --- /dev/null +++ b/src/xunit.runner.visualstudio/TestingPlatform/TestingPlatformBuilderHook.cs @@ -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()!]); +} diff --git a/src/xunit.runner.visualstudio/TestingPlatform/XunitBridgedTestFramework.cs b/src/xunit.runner.visualstudio/TestingPlatform/XunitBridgedTestFramework.cs new file mode 100644 index 0000000..f5eb373 --- /dev/null +++ b/src/xunit.runner.visualstudio/TestingPlatform/XunitBridgedTestFramework.cs @@ -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> getTestAssemblies, + IServiceProvider serviceProvider, + ITestFrameworkCapabilities capabilities) : + base(extension, getTestAssemblies, serviceProvider, capabilities) + { } + + /// + 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; + } + + /// + 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; + } +} diff --git a/src/xunit.runner.visualstudio/TestingPlatform/XunitExtension.cs b/src/xunit.runner.visualstudio/TestingPlatform/XunitExtension.cs new file mode 100644 index 0000000..4ecefc0 --- /dev/null +++ b/src/xunit.runner.visualstudio/TestingPlatform/XunitExtension.cs @@ -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 IsEnabledAsync() => Task.FromResult(true); +} diff --git a/src/xunit.runner.visualstudio/VsTestRunner.cs b/src/xunit.runner.visualstudio/VsTestRunner.cs index 79bf42d..9e17d21 100644 --- a/src/xunit.runner.visualstudio/VsTestRunner.cs +++ b/src/xunit.runner.visualstudio/VsTestRunner.cs @@ -113,7 +113,7 @@ public void Cancel() cancelled = true; } - void ITestDiscoverer.DiscoverTests( + public void DiscoverTests( IEnumerable sources, IDiscoveryContext discoveryContext, IMessageLogger logger, @@ -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? sources, IRunContext? runContext, IFrameworkHandle? frameworkHandle) @@ -182,7 +182,7 @@ void ITestExecutor.RunTests( ); } - void ITestExecutor.RunTests( + public void RunTests( IEnumerable? tests, IRunContext? runContext, IFrameworkHandle? frameworkHandle) diff --git a/src/xunit.runner.visualstudio/build/xunit.runner.visualstudio.desktop.props b/src/xunit.runner.visualstudio/build/xunit.runner.visualstudio.desktop.props index 75d06f0..4325711 100644 --- a/src/xunit.runner.visualstudio/build/xunit.runner.visualstudio.desktop.props +++ b/src/xunit.runner.visualstudio/build/xunit.runner.visualstudio.desktop.props @@ -1,11 +1,23 @@ + + false + $(EnableXunitRunner) + + + + Exe + + + + + + xUnit.net + Xunit.Runner.VisualStudio.TestingPlatformBuilderHook + + + - - xunit.runner.visualstudio.testadapter.dll - PreserveNewest - False - xunit.abstractions.dll PreserveNewest diff --git a/src/xunit.runner.visualstudio/build/xunit.runner.visualstudio.dotnetcore.props b/src/xunit.runner.visualstudio/build/xunit.runner.visualstudio.dotnetcore.props index 89e8e7e..cc91e3c 100644 --- a/src/xunit.runner.visualstudio/build/xunit.runner.visualstudio.dotnetcore.props +++ b/src/xunit.runner.visualstudio/build/xunit.runner.visualstudio.dotnetcore.props @@ -1,11 +1,23 @@ + + false + $(EnableXunitRunner) + + + + Exe + + + + + + xUnit.net + Xunit.Runner.VisualStudio.TestingPlatformBuilderHook + + + - - xunit.runner.visualstudio.testadapter.dll - PreserveNewest - False - xunit.runner.reporters.netcoreapp10.dll PreserveNewest diff --git a/src/xunit.runner.visualstudio/build/xunit.runner.visualstudio.targets b/src/xunit.runner.visualstudio/build/xunit.runner.visualstudio.targets new file mode 100644 index 0000000..e52dc5b --- /dev/null +++ b/src/xunit.runner.visualstudio/build/xunit.runner.visualstudio.targets @@ -0,0 +1,32 @@ + + + + + + $(EnableXunitRunner) + false + true + + + + + + + + + $(MSBuildThisFileDirectory)xunit.runner.visualstudio.testadapter.dll + + + + + + + xunit.runner.visualstudio.testadapter.dll + PreserveNewest + False + + + + + + diff --git a/src/xunit.runner.visualstudio/xunit.runner.visualstudio.csproj b/src/xunit.runner.visualstudio/xunit.runner.visualstudio.csproj index 5cfd053..51d6ff1 100644 --- a/src/xunit.runner.visualstudio/xunit.runner.visualstudio.csproj +++ b/src/xunit.runner.visualstudio/xunit.runner.visualstudio.csproj @@ -19,6 +19,7 @@ + @@ -59,6 +60,7 @@ Configuration=$(Configuration); GitCommitId=$(GitCommitId); MicrosoftTestPlatformObjectModelVersion=$(MicrosoftTestPlatformObjectModelVersion); + MicrosoftTestingPlatformVersion=$(MicrosoftTestingPlatformVersion); PackageVersion=$(PackageVersion); SignedPath=$(SignedPath); diff --git a/src/xunit.runner.visualstudio/xunit.runner.visualstudio.nuspec b/src/xunit.runner.visualstudio/xunit.runner.visualstudio.nuspec index d6c897b..3946b65 100644 --- a/src/xunit.runner.visualstudio/xunit.runner.visualstudio.nuspec +++ b/src/xunit.runner.visualstudio/xunit.runner.visualstudio.nuspec @@ -14,12 +14,16 @@ 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. Copyright (C) .NET Foundation - true + + + + + + - @@ -35,12 +39,14 @@ + +