From f8d744959099d704872e4a09974850df35052c6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Tue, 27 Jan 2026 16:22:59 +0100 Subject: [PATCH] Add trx report acceptance test for MSTest --- .../TrxReportTests.cs | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 test/IntegrationTests/MSTest.Acceptance.IntegrationTests/TrxReportTests.cs diff --git a/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/TrxReportTests.cs b/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/TrxReportTests.cs new file mode 100644 index 0000000000..2943f72d25 --- /dev/null +++ b/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/TrxReportTests.cs @@ -0,0 +1,94 @@ +// 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; +using Microsoft.Testing.Platform.Helpers; + +namespace MSTest.Acceptance.IntegrationTests; + +[TestClass] +public sealed class TrxReportTests : AcceptanceTestBase +{ + [TestMethod] + [DynamicData(nameof(TargetFrameworks.AllForDynamicData), typeof(TargetFrameworks))] + public async Task TrxReport_WhenTestFails_ContainsExceptionInfoInOutput(string tfm) + { + string fileName = Guid.NewGuid().ToString("N"); + var testHost = TestHost.LocateFrom(AssetFixture.TargetAssetPath, TestAssetFixture.ProjectName, tfm); + TestHostResult testHostResult = await testHost.ExecuteAsync($"--report-trx --report-trx-filename {fileName}.trx", cancellationToken: TestContext.CancellationToken); + + testHostResult.AssertExitCodeIs(ExitCodes.AtLeastOneTestFailed); + + string trxFile = Directory.GetFiles(testHost.DirectoryName, $"{fileName}.trx", SearchOption.AllDirectories).Single(); + string trxContent = File.ReadAllText(trxFile); + + // Verify that the TRX contains the UnitTestResult with outcome="Failed" + Assert.Contains(@"", trxContent, trxContent); + Assert.Contains(@"", trxContent, trxContent); + + // Verify that exception message is present + Assert.Contains(@"", trxContent, trxContent); + Assert.Contains("Assert.AreEqual failed. Expected:<1>. Actual:<2>.", trxContent, trxContent); + + // Verify that stack trace is present + Assert.Contains(@"", trxContent, trxContent); + Assert.Contains("at MSTestTrxReport.UnitTest1.FailingTest()", trxContent, trxContent); + } + + public sealed class TestAssetFixture() : TestAssetFixtureBase(AcceptanceFixture.NuGetGlobalPackagesFolder) + { + public const string ProjectName = "MSTestTrxReport"; + + public string TargetAssetPath => GetAssetPath(ProjectName); + + public override IEnumerable<(string ID, string Name, string Code)> GetAssetsToGenerate() + { + yield return (ProjectName, ProjectName, + SourceCode + .PatchTargetFrameworks(TargetFrameworks.All) + .PatchCodeWithReplace("$MicrosoftTestingPlatformVersion$", MicrosoftTestingPlatformVersion) + .PatchCodeWithReplace("$MSTestVersion$", MSTestVersion)); + } + + private const string SourceCode = """ +#file MSTestTrxReport.csproj + + + + Exe + true + $TargetFrameworks$ + latest + + + + + + + + + +#file UnitTest1.cs +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace MSTestTrxReport; + +[TestClass] +public class UnitTest1 +{ + [TestMethod] + public void FailingTest() + { + Assert.AreEqual(1, 2); + } +} +"""; + } + + public TestContext TestContext { get; set; } = null!; +}