Repository navigation
Fix duplicate RTD nodes for parameterized fixtures under MTP #1514
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5ffb72e
Fix duplicate RTD nodes for parameterized fixtures under MTP (#1513)
OsirisTerje 659b991
Shorten Issue1513 acceptance test names to stay under MAX_PATH
OsirisTerje 091d183
Use NUnit's fixture name instead of parsing the test name (#1513)
OsirisTerje e87e83d
Name acceptance workspaces after the test method to stay under MAX_PATH
OsirisTerje File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
174 changes: 174 additions & 0 deletions
174
src/NUnit.TestAdapter.Tests.Acceptance/Issue1513Tests.cs
This file contains hidden or 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,174 @@ | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Xml.Linq; | ||
| using NUnit.Framework; | ||
| using NUnit.VisualStudio.TestAdapter.Tests.Acceptance.WorkspaceTools; | ||
|
|
||
| namespace NUnit.VisualStudio.TestAdapter.Tests.Acceptance; | ||
|
|
||
| /// <summary> | ||
| /// Issue #1513: with MTP, VS real-time discovery shows an extra "not run" node for every test in a | ||
| /// parameterized fixture. VS knows only the CLR type, so TestMethodIdentifierProperty.TypeName must not | ||
| /// contain the fixture arguments; they go in the display name instead. The bridge builds the trx className from the same TypeName, so when a | ||
| /// trx report is requested the fixture arguments are kept (#1351). | ||
| /// A data consumer registered in the test project dumps the property for every test node. | ||
| /// </summary> | ||
| public class Issue1513Tests : AcceptanceTests | ||
| { | ||
| // Same MTP version as Microsoft.Testing.Extensions.VSTestBridge in Directory.Packages.props. | ||
| private const string MicrosoftTestingPlatformVersion = "2.5.1"; | ||
|
|
||
| private const string TestCode = @" | ||
| using NUnit.Framework; | ||
|
|
||
| namespace Issue1513 | ||
| { | ||
| [TestFixture(""A"")] | ||
| [TestFixture(""B"")] | ||
| public class Fixture | ||
| { | ||
| private readonly string _arg; | ||
| public Fixture(string arg) => _arg = arg; | ||
|
|
||
| [Test] | ||
| public void NoParameter() => Assert.Pass(); | ||
|
|
||
| [TestCase(1)] | ||
| public void WithParameter(int value) => Assert.Pass(); | ||
| } | ||
|
|
||
| public class Plain | ||
| { | ||
| [Test] | ||
| public void Test() => Assert.Pass(); | ||
| } | ||
| } | ||
| "; | ||
|
|
||
| private const string DumpExtensionCode = @" | ||
| using System; | ||
| using System.IO; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Testing.Platform.Builder; | ||
| using Microsoft.Testing.Platform.Extensions; | ||
| using Microsoft.Testing.Platform.Extensions.Messages; | ||
|
|
||
| namespace Issue1513 | ||
| { | ||
| public static class IdentifierDumpHook | ||
| { | ||
| public static void AddExtensions(ITestApplicationBuilder builder, string[] args) | ||
| => builder.TestHost.AddDataConsumer(_ => new IdentifierDumpConsumer()); | ||
| } | ||
|
|
||
| public sealed class IdentifierDumpConsumer : IDataConsumer | ||
| { | ||
| private static readonly object Lock = new object(); | ||
| public Type[] DataTypesConsumed => new[] { typeof(TestNodeUpdateMessage) }; | ||
| public string Uid => nameof(IdentifierDumpConsumer); | ||
| public string Version => ""1.0.0""; | ||
| public string DisplayName => nameof(IdentifierDumpConsumer); | ||
| public string Description => nameof(IdentifierDumpConsumer); | ||
| public Task<bool> IsEnabledAsync() => Task.FromResult(true); | ||
|
|
||
| public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationToken cancellationToken) | ||
| { | ||
| var node = ((TestNodeUpdateMessage)value).TestNode; | ||
| var id = node.Properties.SingleOrDefault<TestMethodIdentifierProperty>(); | ||
| if (id != null) | ||
| { | ||
| lock (Lock) | ||
| File.AppendAllText( | ||
| Path.Combine(AppContext.BaseDirectory, ""identifiers.txt""), | ||
| id.Namespace + ""|"" + id.TypeName + ""|"" + id.MethodName + ""|"" + node.DisplayName + Environment.NewLine); | ||
| } | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
| } | ||
| "; | ||
|
|
||
| [TestCaseSource(typeof(SingleFrameworkSource), nameof(SingleFrameworkSource.AllFrameworks))] | ||
| public void WithoutTrx(SingleFrameworkSource source) | ||
| { | ||
| var workspace = CreateTestWorkspace(source); | ||
|
|
||
| var identifiers = Run(workspace, source); | ||
|
|
||
| Assert.That(identifiers, Is.EquivalentTo([ | ||
| "Issue1513|Fixture|NoParameter|Fixture(\"A\").NoParameter", | ||
| "Issue1513|Fixture|NoParameter|Fixture(\"B\").NoParameter", | ||
| "Issue1513|Fixture|WithParameter|Fixture(\"A\").WithParameter(1)", | ||
| "Issue1513|Fixture|WithParameter|Fixture(\"B\").WithParameter(1)", | ||
| "Issue1513|Plain|Test|Test" | ||
| ])); | ||
| } | ||
|
|
||
| [TestCaseSource(typeof(SingleFrameworkSource), nameof(SingleFrameworkSource.AllFrameworks))] | ||
| public void WithTrx(SingleFrameworkSource source) | ||
| { | ||
| var workspace = CreateTestWorkspace(source); | ||
| var resultsDirectory = Path.Combine(workspace.Directory, "TestResults"); | ||
|
|
||
| var identifiers = Run(workspace, source, "--report-trx", "--results-directory", resultsDirectory); | ||
|
|
||
| Assert.That(identifiers, Is.EquivalentTo([ | ||
| "Issue1513|Fixture(\"A\")|NoParameter|NoParameter", | ||
| "Issue1513|Fixture(\"A\")|WithParameter|WithParameter(1)", | ||
| "Issue1513|Fixture(\"B\")|NoParameter|NoParameter", | ||
| "Issue1513|Fixture(\"B\")|WithParameter|WithParameter(1)", | ||
| "Issue1513|Plain|Test|Test" | ||
| ])); | ||
|
|
||
| var trxFile = Directory.GetFiles(resultsDirectory, "*.trx").Single(); | ||
| XNamespace ns = "http://microsoft.com/schemas/VisualStudio/TeamTest/2010"; | ||
| var classNames = XDocument.Load(trxFile) | ||
| .Descendants(ns + "TestMethod") | ||
| .Select(m => (string)m.Attribute("className")) | ||
| .Distinct() | ||
| .ToArray(); | ||
| Assert.That(classNames, Is.EquivalentTo(["Issue1513.Fixture(\"A\")", "Issue1513.Fixture(\"B\")", "Issue1513.Plain"])); | ||
| } | ||
|
|
||
| private static IsolatedWorkspace CreateTestWorkspace(SingleFrameworkSource source) | ||
| { | ||
| var workspace = CreateWorkspace() | ||
| .AddProject("Test.csproj", $@" | ||
| <Project Sdk='Microsoft.NET.Sdk'> | ||
| <PropertyGroup> | ||
| <TargetFramework>{source.Framework}</TargetFramework> | ||
| <OutputType>Exe</OutputType> | ||
| <EnableNUnitRunner>true</EnableNUnitRunner> | ||
| <ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include='NUnit' Version='{source.NUnitVersion}' /> | ||
| <PackageReference Include='NUnit3TestAdapter' Version='{NuGetPackageVersion}' /> | ||
| <PackageReference Include='Microsoft.Testing.Extensions.TrxReport' Version='{MicrosoftTestingPlatformVersion}' /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <TestingPlatformBuilderHook Include='1513D0D0-0000-4000-8000-000000001513'> | ||
| <DisplayName>IdentifierDump</DisplayName> | ||
| <TypeFullName>Issue1513.IdentifierDumpHook</TypeFullName> | ||
| </TestingPlatformBuilderHook> | ||
| </ItemGroup> | ||
| </Project>") | ||
| .AddFile("Tests.cs", TestCode) | ||
| .AddFile("IdentifierDump.cs", DumpExtensionCode); | ||
|
|
||
| workspace.MsBuild(restore: true); | ||
| return workspace; | ||
| } | ||
|
|
||
| private static string[] Run(IsolatedWorkspace workspace, SingleFrameworkSource source, params string[] arguments) | ||
| { | ||
| var result = ProcessUtils.Run(workspace.Directory, "dotnet", ["run", "--no-build", "--", .. arguments]); | ||
| TestContext.Out.WriteLine(result.StdOut); | ||
| result.ThrowIfError(); | ||
|
|
||
| var dumpFile = Path.Combine(workspace.Directory, "bin", "Debug", source.Framework, "identifiers.txt"); | ||
| Assert.That(dumpFile, Does.Exist, "Data consumer saw no TestMethodIdentifierProperty"); | ||
| return File.ReadAllLines(dumpFile).Distinct().ToArray(); | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/NUnitTestAdapter/TestingPlatformAdapter/FixtureDisplayName.cs
This file contains hidden or 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,41 @@ | ||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel; | ||
|
|
||
| namespace NUnit.VisualStudio.TestAdapter.TestingPlatformAdapter; | ||
|
|
||
| /// <summary> | ||
| /// Puts the fixture arguments in front of the display name, e.g. Fixture("A").NoParameter (#1513). | ||
| /// Without trx, TestMethodIdentifierProperty.TypeName is the plain CLR type, so VS Test Explorer | ||
| /// shows all instances of a parameterized fixture under one class node. The display name is then | ||
| /// the only place that tells them apart. With trx, TypeName keeps the fixture arguments (#1351), | ||
| /// so the display name is left as it is. | ||
| /// </summary> | ||
| internal sealed class FixtureDisplayName(TestApplicationBuilderExtensions.NUnitTestFrameworkCapabilities capabilities) | ||
| { | ||
| public void Apply(TestCase testCase) | ||
| { | ||
| testCase.DisplayName = Create(testCase.DisplayName, testCase); | ||
| } | ||
|
|
||
| public void Apply(TestResult testResult) | ||
| { | ||
| Apply(testResult.TestCase); | ||
| testResult.DisplayName = Create(testResult.DisplayName, testResult.TestCase); | ||
| } | ||
|
|
||
| private string Create(string displayName, TestCase testCase) | ||
| { | ||
| if (capabilities.IsTrxReportEnabled || displayName is null) | ||
| return displayName; | ||
|
|
||
| var className = testCase.GetPropertyValue<string>(Seed.NUnitClassName, null); | ||
| var fixture = TestMethodIdentifierBuilder.ExtractClassFromFullName(testCase.FullyQualifiedName); | ||
| if (className is null || string.IsNullOrEmpty(fixture) || fixture == className) | ||
| return displayName; | ||
|
|
||
| var dot = TestMethodIdentifierBuilder.FindLastDotNotInParens(fixture); | ||
| var prefix = fixture.Substring(dot + 1) + "."; | ||
| return displayName.StartsWith(prefix) || displayName.StartsWith(fixture + ".") | ||
| ? displayName | ||
| : prefix + displayName; | ||
| } | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
src/NUnitTestAdapter/TestingPlatformAdapter/FixtureDisplayNameDiscoverySink.cs
This file contains hidden or 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,16 @@ | ||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel; | ||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; | ||
|
|
||
| namespace NUnit.VisualStudio.TestAdapter.TestingPlatformAdapter; | ||
|
|
||
| /// <summary> | ||
| /// Applies <see cref="FixtureDisplayName"/> to every discovered test case before the bridge sees it. | ||
| /// </summary> | ||
| internal sealed class FixtureDisplayNameDiscoverySink(ITestCaseDiscoverySink inner, FixtureDisplayName displayName) : ITestCaseDiscoverySink | ||
| { | ||
| public void SendTestCase(TestCase discoveredTest) | ||
| { | ||
| displayName.Apply(discoveredTest); | ||
| inner.SendTestCase(discoveredTest); | ||
| } | ||
| } |
46 changes: 46 additions & 0 deletions
46
src/NUnitTestAdapter/TestingPlatformAdapter/FixtureDisplayNameFrameworkHandle.cs
This file contains hidden or 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,46 @@ | ||
| using System.Collections.Generic; | ||
|
|
||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel; | ||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; | ||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; | ||
|
|
||
| namespace NUnit.VisualStudio.TestAdapter.TestingPlatformAdapter; | ||
|
|
||
| /// <summary> | ||
| /// Applies <see cref="FixtureDisplayName"/> to every test case and result before the bridge sees it. | ||
| /// </summary> | ||
| internal sealed class FixtureDisplayNameFrameworkHandle(IFrameworkHandle inner, FixtureDisplayName displayName) : IFrameworkHandle | ||
| { | ||
| #pragma warning disable CS0618 // Obsolete, but still part of the interface. | ||
| public bool EnableShutdownAfterTestRun | ||
| { | ||
| get => inner.EnableShutdownAfterTestRun; | ||
| set => inner.EnableShutdownAfterTestRun = value; | ||
| } | ||
| #pragma warning restore CS0618 | ||
|
|
||
| public void RecordStart(TestCase testCase) | ||
| { | ||
| displayName.Apply(testCase); | ||
| inner.RecordStart(testCase); | ||
| } | ||
|
|
||
| public void RecordResult(TestResult testResult) | ||
| { | ||
| displayName.Apply(testResult); | ||
| inner.RecordResult(testResult); | ||
| } | ||
|
|
||
| public void RecordEnd(TestCase testCase, TestOutcome outcome) | ||
| { | ||
| displayName.Apply(testCase); | ||
| inner.RecordEnd(testCase, outcome); | ||
| } | ||
|
|
||
| public void RecordAttachments(IList<AttachmentSet> attachmentSets) => inner.RecordAttachments(attachmentSets); | ||
|
|
||
| public void SendMessage(TestMessageLevel testMessageLevel, string message) => inner.SendMessage(testMessageLevel, message); | ||
|
|
||
| public int LaunchProcessWithDebuggerAttached(string filePath, string workingDirectory, string arguments, IDictionary<string, string> environmentVariables) | ||
| => inner.LaunchProcessWithDebuggerAttached(filePath, workingDirectory, arguments, environmentVariables); | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.