8000 [port] Fix "Found multiple properties of type 'Microsoft.Testing.Plat… by Evangelink · Pull Request #4589 · microsoft/testfx · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,35 @@ public static TestNode ToTestNode(this TestResult testResult, bool isTrxEnabled,

testNode.Properties.Add(new TimingProperty(new(testResult.StartTime, testResult.EndTime, testResult.Duration), []));

10000 var standardErrorMessages = new List<string>();
var standardOutputMessages = new List<string>();
foreach (TestResultMessage testResultMessage in testResult.Messages)
{
if (testResultMessage.Category == TestResultMessage.StandardErrorCategory)
{
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.TestCase.StandardError", testResultMessage.Text ?? string.Empty));
testNode.Properties.Add(new StandardErrorProperty(testResultMessage.Text ?? string.Empty));
string message = testResultMessage.Text ?? string.Empty;
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.TestCase.StandardError", message));
standardErrorMessages.Add(message);
}

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

if (standardErrorMessages.Count > 0)
{
testNode.Properties.Add(new StandardErrorProperty(string.Join(Environment.NewLine, standardErrorMessages)));
}

if (standardOutputMessages.Count > 0)
{
testNode.Properties.Add(new StandardOutputProperty(string.Join(Environment.NewLine, standardOutputMessages)));
}

return testNode;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Testing.Platform.Acceptance.IntegrationTests;
using Microsoft.Testing.Platform.Acceptance.IntegrationTests.Helpers;

namespace MSTest.Acceptance.IntegrationTests;

[TestClass]
public sealed class OutputTests : AcceptanceTestBase<OutputTests.TestAssetFixture>
{
[TestMethod]
[DynamicData(nameof(TargetFrameworks.AllForDynamicData), typeof(TargetFrameworks))]
public async Task DetailedOutputIsAsExpected(string tfm)
{
var testHost = TestHost.LocateFrom(AssetFixture.ProjectPath, TestAssetFixture.ProjectName, tfm);
TestHostResult testHostResult = await testHost.ExecuteAsync("--output detailed");

// Assert
testHostResult.AssertOutputContains("Assert.AreEqual failed. Expected:<1>. Actual:<2>.");
testHostResult.AssertOutputContains("""
Standard output
Console message
TestContext Messages:
TestContext message
Error output
""");
}

public sealed class TestAssetFixture() : TestAssetFixtureBase(AcceptanceFixture.NuGetGlobalPackagesFolder)
{
public const string ProjectName = "TestOutput";

public string ProjectPath => GetAssetPath(ProjectName);

public override IEnumerable<(string ID, string Name, string Code)> GetAssetsToGenerate()
{
yield return (ProjectName, ProjectName,
SourceCode
.PatchTargetFrameworks(TargetFrameworks.All)
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion));
}

private const string SourceCode = """
#file TestOutput.csproj
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<EnableMSTestRunner>true</EnableMSTestRunner>
<TargetFrameworks>$TargetFrameworks$</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MSTest.TestAdapter" Version="$MSTestVersion$" />
<PackageReference Include="MSTest.TestFramework" Version="$MSTestVersion$" />
</ItemGroup>

</Project>

#file UnitTest1.cs
using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class UnitTest1
{
public TestContext TestContext { get; set; }

[TestMethod]
public void TestMethod()
{
Debug.WriteLine("Debug message");
Console.WriteLine("Console message");
TestContext.WriteLine("TestContext message");

Assert.AreEqual(1, 2);
}
}
""";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,44 @@ public void ToTestNode_WhenTestResultHasTraits_TestNodePropertiesContainIt()
Assert. 9BCA AreEqual("key", errorTestNodeStateProperties[0].Pairs[0].Key);
Assert.AreEqual("value", errorTestNodeStateProperties[0].Pairs[0].Value);
}

[TestMethod]
public void ToTestNode_WhenTestResultHasMultipleStandardOutputMessages_TestNodePropertiesHasASingleOne()
{
TestResult testResult = new(new TestCase("SomeFqn", new("executor://uri", UriKind.Absolute), "source.cs"))
{
DisplayName = "TestName",
Messages =
{
new TestResultMessage(TestResultMessage.StandardOutCategory, "message1"),
new TestResultMessage(TestResultMessage.StandardOutCategory, "message2"),
},
};

var testNode = testResult.ToTestNode(false, VSTestClient);

StandardOutputProperty[] standardOutputProperties = testNode.Properties.OfType<StandardOutputProperty>().ToArray();
Assert.IsTrue(standardOutputProperties.Length == 1);
Assert.AreEqual($"message1{Environment.NewLine}message2", standardOutputProperties[0].StandardOutput);
}

[TestMethod]
public void ToTestNode_WhenTestResultHasMultipleStandardErrorMessages_TestNodePropertiesHasASingleOne()
{
TestResult testResult = new(new TestCase("SomeFqn", new("executor://uri", UriKind.Absolute), "source.cs"))
{
DisplayName = "TestName",
Messages =
{
new TestResultMessage(TestResultMessage.StandardErrorCategory, "message1"),
new TestResultMessage(TestResultMessage.StandardErrorCategory, "message2"),
},
};

var testNode = testResult.ToTestNode(false, VSTestClient);

StandardErrorProperty[] standardErrorProperties = testNode.Properties.OfType<StandardErrorProperty>().ToArray();
Assert.IsTrue(standardErrorProperties.Length == 1);
Assert.AreEqual($"message1{Environment.NewLine}message2", standardErrorProperties[0].StandardError);
}
}
0