Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if !WINDOWS_UWP
using Microsoft.Testing.Extensions.VSTestBridge.Capabilities;
using Microsoft.Testing.Extensions.TrxReport.Abstractions;
using Microsoft.Testing.Extensions.VSTestBridge.Helpers;
using Microsoft.Testing.Platform.Builder;
using Microsoft.Testing.Platform.Capabilities.TestFramework;
Expand All @@ -17,6 +17,17 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting;
[SuppressMessage("ApiDesign", "RS0030:Do not use banned APIs", Justification = "We can use MTP from this folder")]
public static class TestApplicationBuilderExtensions
{
// NOTE: We intentionally use this class and not VSTestBridgeExtensionBaseCapabilities because
// we don't want MSTest to use vstestProvider capability
private sealed class MSTestCapabilities : ITrxReportCapability
{
bool ITrxReportCapability.IsSupported { get; } = true;

void ITrxReportCapability.Enable()
{
}
}

/// <summary>
/// Register MSTest as the test framework and register the necessary services.
/// </summary>
Expand All @@ -34,7 +45,7 @@ public static void AddMSTest(this ITestApplicationBuilder testApplicationBuilder
testApplicationBuilder.AddRunSettingsEnvironmentVariableProvider(extension);
testApplicationBuilder.RegisterTestFramework(
serviceProvider => new TestFrameworkCapabilities(
new VSTestBridgeExtensionBaseCapabilities(),
new MSTestCapabilities(),
Comment thread
fhnaseer marked this conversation as resolved.
Resolved
#pragma warning disable TPEXP // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
new MSTestBannerCapability(serviceProvider.GetRequiredService<IPlatformInformation>()),
MSTestGracefulStopTestExecutionCapability.Instance),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ namespace Microsoft.Testing.Extensions.VSTestBridge.Capabilities;
/// <summary>
/// The VSTest bridged test framework capabilities.
/// </summary>
// NOTE: MSTest no longer uses this, as we don't want to use the vstestProvider.
// Only NUnit and Expecto use this.
// https://github.com/nunit/nunit3-vs-adapter/blob/3d0f824243aaaeb85621d3c7dddc92e7a7c45097/src/NUnitTestAdapter/TestingPlatformAdapter/TestApplicationBuilderExtensions.cs#L20
// https://github.com/YoloDev/YoloDev.Expecto.TestSdk/blob/0d1a3eadd65b605f61bb01d302f28382be76b8ac/src/YoloDev.Expecto.TestSdk/TestApplicationHelpers.fs#L16
public sealed class VSTestBridgeExtensionBaseCapabilities : ITrxReportCapability, IVSTestFlattenedTestNodesReportCapability, INamedFeatureCapability
Comment thread
Youssef1313 marked this conversation as resolved.
Resolved
{
private const string VSTestProviderSupport = "vstestProvider";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" />
<PackageReference Include="Microsoft.TestPlatform.AdapterUtilities" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Logging;
using Microsoft.Testing.Platform.Messages;
using Microsoft.Testing.Platform.OutputDevice;
using Microsoft.Testing.Platform.Services;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
Expand All @@ -30,14 +29,14 @@ internal sealed class FrameworkHandlerAdapter : IFrameworkHandle
private readonly IMessageBus _messageBus;
private readonly VSTestBridgedTestFrameworkBase _adapterExtensionBase;
private readonly TestSessionContext _session;
private readonly IClientInfo _clientInfo;
private readonly CancellationToken _cancellationToken;
private readonly bool _isTrxEnabled;
private readonly MessageLoggerAdapter _comboMessageLogger;
private readonly string _testAssemblyPath;
private readonly IServiceProvider _serviceProvider;

public FrameworkHandlerAdapter(VSTestBridgedTestFrameworkBase adapterExtensionBase, TestSessionContext session, IClientInfo clientInfo, string[] testAssemblyPaths,
ITestApplicationModuleInfo testApplicationModuleInfo, ILoggerFactory loggerFactory, IMessageBus messageBus, IOutputDevice outputDevice,
public FrameworkHandlerAdapter(VSTestBridgedTestFrameworkBase adapterExtensionBase, TestSessionContext session, string[] testAssemblyPaths,
Comment thread
Youssef1313 marked this conversation as resolved.
Resolved
IServiceProvider serviceProvider, ILoggerFactory loggerFactory,
bool isTrxEnabled, CancellationToken cancellationToken, IFrameworkHandle? frameworkHandle = null)
{
if (testAssemblyPaths.Length == 0)
Expand All @@ -46,7 +45,7 @@ public FrameworkHandlerAdapter(VSTestBridgedTestFrameworkBase adapterExtensionBa
}
else if (testAssemblyPaths.Length > 1)
{
_testAssemblyPath = testApplicationModuleInfo.GetCurrentTestApplicationFullPath();
_testAssemblyPath = serviceProvider.GetTestApplicationModuleInfo().GetCurrentTestApplicationFullPath();

if (!testAssemblyPaths.Contains(_testAssemblyPath))
{
Expand All @@ -58,15 +57,15 @@ public FrameworkHandlerAdapter(VSTestBridgedTestFrameworkBase adapterExtensionBa
_testAssemblyPath = testAssemblyPaths[0];
}

_serviceProvider = serviceProvider;
_frameworkHandle = frameworkHandle;
_logger = loggerFactory.CreateLogger<FrameworkHandlerAdapter>();
_messageBus = messageBus;
_messageBus = serviceProvider.GetMessageBus();
_adapterExtensionBase = adapterExtensionBase;
_session = session;
_clientInfo = clientInfo;
_cancellationToken = cancellationToken;
_isTrxEnabled = isTrxEnabled;
_comboMessageLogger = new MessageLoggerAdapter(loggerFactory, outputDevice, adapterExtensionBase, frameworkHandle);
_comboMessageLogger = new MessageLoggerAdapter(loggerFactory, serviceProvider.GetOutputDevice(), adapterExtensionBase, frameworkHandle);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -126,7 +125,7 @@ public void RecordResult(TestResult testResult)
_frameworkHandle?.RecordResult(testResult);

// Publish node state change to Microsoft Testing Platform
var testNode = testResult.ToTestNode(_isTrxEnabled, _clientInfo);
var testNode = testResult.ToTestNode(_isTrxEnabled, _serviceProvider);

var testNodeChange = new TestNodeUpdateMessage(_session.SessionUid, testNode);
_messageBus.PublishAsync(_adapterExtensionBase, testNodeChange).Await();
Expand All @@ -145,7 +144,7 @@ public void RecordStart(TestCase testCase)
_frameworkHandle?.RecordStart(testCase);

// Publish node state change to Microsoft Testing Platform
var testNode = testCase.ToTestNode(_isTrxEnabled, _clientInfo);
var testNode = testCase.ToTestNode(_isTrxEnabled, _serviceProvider);
testNode.Properties.Add(InProgressTestNodeStateProperty.CachedInstance);
var testNodeChange = new TestNodeUpdateMessage(_session.SessionUid, testNode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

using Microsoft.Testing.Extensions.TrxReport.Abstractions;
using Microsoft.Testing.Platform;
using Microsoft.Testing.Platform.Capabilities.TestFramework;
using Microsoft.Testing.Platform.CommandLine;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.ServerMode;
using Microsoft.Testing.Platform.Services;
using Microsoft.Testing.Platform.TestHost;
using Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;

namespace Microsoft.Testing.Extensions.VSTestBridge.ObjectModel;
Expand All @@ -21,10 +24,22 @@ internal static class ObjectModelConverters
VSTestTestNodeProperties.OriginalExecutorUriPropertyName, VSTestTestNodeProperties.OriginalExecutorUriPropertyName,
typeof(Uri), typeof(TestNode));

private static readonly TestProperty ManagedTypeProperty = TestProperty.Register(
id: "TestCase.ManagedType",
label: "TestCase.ManagedType",
valueType: typeof(string),
owner: typeof(TestCase));

private static readonly TestProperty ManagedMethodProperty = TestProperty.Register(
id: "TestCase.ManagedMethod",
label: "TestCase.ManagedMethod",
valueType: typeof(string),
owner: typeof(TestCase));

/// <summary>
/// Converts a VSTest <see cref="TestCase"/> to a Microsoft Testing Platform <see cref="TestNode"/>.
/// </summary>
public static TestNode ToTestNode(this TestCase testCase, bool isTrxEnabled, IClientInfo client, string? displayNameFromTestResult = null)
public static TestNode ToTestNode(this TestCase testCase, bool isTrxEnabled, IServiceProvider serviceProvider, string? displayNameFromTestResult = null)
{
string testNodeUid = testCase.Id.ToString();

Expand All @@ -34,7 +49,13 @@ public static TestNode ToTestNode(this TestCase testCase, bool isTrxEnabled, ICl
DisplayName = displayNameFromTestResult ?? testCase.DisplayName ?? testCase.FullyQualifiedName,
};

CopyVSTestProperties(testCase.Properties, testNode, testCase, testCase.GetPropertyValue, isTrxEnabled, client);
// This will be false for Expecto and NUnit currently, as they don't provide ManagedType/ManagedMethod.
if (TryGetMethodIdentifierProperty(testCase, out TestMethodIdentifierProperty? methodIdentifierProperty))
{
testNode.Properties.Add(methodIdentifierProperty);
}

CopyVSTestProperties(testCase.Properties, testNode, testCase, testCase.GetPropertyValue, isTrxEnabled, ShouldAddVSTestProviderProperties(serviceProvider));
Comment thread
Youssef1313 marked this conversation as resolved.
Resolved
if (testCase.CodeFilePath is not null)
{
testNode.Properties.Add(new TestFileLocationProperty(testCase.CodeFilePath, new(new(testCase.LineNumber, -1), new(testCase.LineNumber, -1))));
Expand All @@ -44,12 +65,10 @@ public static TestNode ToTestNode(this TestCase testCase, bool isTrxEnabled, ICl
}

private static void CopyVSTestProperties(IEnumerable<TestProperty> testProperties, TestNode testNode, TestCase testCase, Func<TestProperty, object?> getPropertyValue,
bool isTrxEnabled, IClientInfo client)
bool isTrxEnabled, bool addVSTestProviderProperties)
Comment thread
Youssef1313 marked this conversation as resolved.
Resolved
{
foreach (TestProperty property in testProperties)
{
testNode.Properties.Add(new VSTestProperty(property, testCase));

if (isTrxEnabled)
{
// TPv2 is doing some special handling for MSTest... we should probably do the same.
Expand All @@ -61,9 +80,10 @@ private static void CopyVSTestProperties(IEnumerable<TestProperty> testPropertie
}
}

// Implement handling of specific vstest properties for VS/VS Code Test Explorer,
// see https://github.com/microsoft/testanywhere/blob/main/docs/design/proposed/IDE_Protocol_IDE_Integration.md#vstest-test-node
if (client.Id == WellKnownClients.VisualStudio)
// If vstestProvider is enabled (only known to be true for NUnit and Expecto so far), and we are running server mode in IDE (not dotnet test),
// we add these stuff.
// Once NUnit and Expecto allow us to move forward and remove vstestProvider, we can remove this logic and get rid of the whole vstestProvider capability.
Comment thread
Youssef1313 marked this conversation as resolved.
Resolved
if (addVSTestProviderProperties)
{
if (property.Id == TestCaseProperties.Id.Id
&& getPropertyValue(property) is Guid testCaseId)
Expand Down Expand Up @@ -117,10 +137,13 @@ private static void CopyVSTestProperties(IEnumerable<TestProperty> testPropertie
/// <summary>
/// Converts a VSTest <see cref="TestResult"/> to a Microsoft Testing Platform <see cref="TestNode"/>.
/// </summary>
public static TestNode ToTestNode(this TestResult testResult, bool isTrxEnabled, IClientInfo client)
public static TestNode ToTestNode(this TestResult testResult, bool isTrxEnabled, IServiceProvider serviceProvider)
{
var testNode = testResult.TestCase.ToTestNode(isTrxEnabled, client, testResult.DisplayName);
CopyVSTestProperties(testResult.Properties, testNode, testResult.TestCase, testResult.GetPropertyValue, isTrxEnabled, client);
var testNode = testResult.TestCase.ToTestNode(isTrxEnabled, serviceProvider, testResult.DisplayName);

bool addVSTestProviderProperties = ShouldAddVSTestProviderProperties(serviceProvider);
CopyVSTestProperties(testResult.Properties, testNode, testResult.TestCase, testResult.GetPropertyValue, isTrxEnabled, addVSTestProviderProperties);

testNode.AddOutcome(testResult);

if (isTrxEnabled)
Expand Down Expand Up @@ -157,14 +180,22 @@ public static TestNode ToTestNode(this TestResult testResult, bool isTrxEnabled,
if (testResultMessage.Category == TestResultMessage.StandardErrorCategory)
{
string message = testResultMessage.Text ?? string.Empty;
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.TestCase.StandardError", message));
if (addVSTestProviderProperties)
{
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.TestCase.StandardError", message));
}

standardErrorMessages.Add(message);
}

if (testResultMessage.Category == TestResultMessage.StandardOutCategory)
{
string message = testResultMessage.Text ?? string.Empty;
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.TestCase.StandardOutput", message));
if (addVSTestProviderProperties)
{
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.TestCase.StandardOutput", message));
}

standardOutputMessages.Add(message);
}
}
Expand Down Expand Up @@ -238,6 +269,45 @@ internal static void FixUpTestCase(this TestCase testCase, string? testAssemblyP
testCase.ExecutorUri = new(Constants.ExecutorUri);
}

private static bool TryGetMethodIdentifierProperty(TestCase testCase, [NotNullWhen(true)] out TestMethodIdentifierProperty? methodIdentifierProperty)
{
string? managedType = testCase.GetPropertyValue<string>(ManagedTypeProperty, defaultValue: null);
string? managedMethod = testCase.GetPropertyValue<string>(ManagedMethodProperty, defaultValue: null);
// NOTE: ManagedMethod, in case of MSTest, will have the parameter types.
// So, we prefer using it to display the parameter types in Test Explorer.
if (RoslynString.IsNullOrEmpty(managedType) || RoslynString.IsNullOrEmpty(managedMethod))
{
methodIdentifierProperty = null;
return false;
}

methodIdentifierProperty = GetMethodIdentifierPropertyFromManagedTypeAndManagedMethod(managedType, managedMethod);
return true;
}
Comment thread
Youssef1313 marked this conversation as resolved.
Resolved

private static TestMethodIdentifierProperty GetMethodIdentifierPropertyFromManagedTypeAndManagedMethod(
string managedType,
string managedMethod)
{
ManagedNameParser.ParseManagedMethodName(managedMethod, out string methodName, out int arity, out string[]? parameterTypes);
if (arity != 0)
{
methodName = $"{methodName}`{arity.ToString(CultureInfo.InvariantCulture)}";
}

parameterTypes ??= [];

ManagedNameParser.ParseManagedTypeName(managedType, out string @namespace, out string typeName);

// In the context of the VSTestBridge where we only have access to VSTest object model, we cannot determine ReturnTypeFullName.
// For now, we lose this bit of information.
// If really needed in the future, we can introduce a VSTest property to hold this info.
// But the eventual goal should be to stop using the VSTestBridge altogether.
Comment thread
Youssef1313 marked this conversation as resolved.
Resolved
// TODO: For AssemblyFullName, can we use Assembly.GetEntryAssembly().FullName?
// Or alternatively, does VSTest object model expose the assembly full name somewhere?
Comment thread
Youssef1313 marked this conversation as resolved.
Resolved
return new TestMethodIdentifierProperty(AssemblyFullName: string.Empty, @namespace, typeName, methodName, parameterTypes, ReturnTypeFullName: string.Empty);
}

private static bool TryParseFullyQualifiedType(string fullyQualifiedName, [NotNullWhen(true)] out string? fullyQualifiedType)
{
fullyQualifiedType = null;
Expand All @@ -256,4 +326,9 @@ private static bool TryParseFullyQualifiedType(string fullyQualifiedName, [NotNu
fullyQualifiedType = fullyQualifiedName[..lastDotIndexBeforeOpenBracket];
return true;
}

private static bool ShouldAddVSTestProviderProperties(IServiceProvider serviceProvider)
=> serviceProvider.GetTestFrameworkCapabilities().GetCapability<INamedFeatureCapability>()?.IsSupported(JsonRpcStrings.VSTestProviderSupport) == true &&
serviceProvider.GetCommandLineOptions().IsOptionSet(PlatformCommandLineProvider.ServerOptionKey) &&
!serviceProvider.GetCommandLineOptions().IsOptionSet(PlatformCommandLineProvider.DotNetTestPipeOptionKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@ internal sealed class TestCaseDiscoverySinkAdapter : ITestCaseDiscoverySink
/// </remarks>
private readonly ITestCaseDiscoverySink? _testCaseDiscoverySink;
private readonly ILogger<TestCaseDiscoverySinkAdapter> _logger;
private readonly IServiceProvider _serviceProvider;
private readonly IMessageBus _messageBus;
private readonly bool _isTrxEnabled;
private readonly IClientInfo _clientInfo;
private readonly VSTestBridgedTestFrameworkBase _adapterExtension;
private readonly TestSessionContext _session;
private readonly CancellationToken _cancellationToken;
private readonly string? _testAssemblyPath;

public TestCaseDiscoverySinkAdapter(VSTestBridgedTestFrameworkBase adapterExtension, TestSessionContext session, string[] testAssemblyPaths,
ITestApplicationModuleInfo testApplicationModuleInfo,
IServiceProvider serviceProvider,
ILoggerFactory loggerFactory,
IMessageBus messageBus, bool isTrxEnabled,
IClientInfo clientInfo,
bool isTrxEnabled,
CancellationToken cancellationToken,
ITestCaseDiscoverySink? testCaseDiscoverySink = null)
{
Expand All @@ -47,7 +46,7 @@ public TestCaseDiscoverySinkAdapter(VSTestBridgedTestFrameworkBase adapterExtens
}
else if (testAssemblyPaths.Length > 1)
{
_testAssemblyPath = testApplicationModuleInfo.GetCurrentTestApplicationFullPath();
_testAssemblyPath = serviceProvider.GetTestApplicationModuleInfo().GetCurrentTestApplicationFullPath();

if (!testAssemblyPaths.Contains(_testAssemblyPath))
{
Expand All @@ -61,9 +60,9 @@ public TestCaseDiscoverySinkAdapter(VSTestBridgedTestFrameworkBase adapterExtens

_testCaseDiscoverySink = testCaseDiscoverySink;
_logger = loggerFactory.CreateLogger<TestCaseDiscoverySinkAdapter>();
_messageBus = messageBus;
_serviceProvider = serviceProvider;
_messageBus = serviceProvider.GetMessageBus();
_isTrxEnabled = isTrxEnabled;
_clientInfo = clientInfo;
_adapterExtension = adapterExtension;
_session = session;
_cancellationToken = cancellationToken;
Expand All @@ -82,7 +81,7 @@ public void SendTestCase(TestCase discoveredTest)
_testCaseDiscoverySink?.SendTestCase(discoveredTest);

// Publish node state change to Microsoft Testing Platform
var testNode = discoveredTest.ToTestNode(_isTrxEnabled, _clientInfo);
var testNode = discoveredTest.ToTestNode(_isTrxEnabled, _serviceProvider);
testNode.Properties.Add(DiscoveredTestNodeStateProperty.CachedInstance);
var testNodeChange = new TestNodeUpdateMessage(_session.SessionUid, testNode);

Expand Down
Loading