-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathTrxReportTests.cs
More file actions
207 lines (165 loc) · 7.91 KB
/
TrxReportTests.cs
File metadata and controls
207 lines (165 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Xml.Linq;
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<TrxReportTests.TestAssetFixture>
{
[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(@"<UnitTestResult", trxContent, trxContent);
Assert.Contains(@"outcome=""Failed""", trxContent, trxContent);
// Verify that the TRX contains the Output element with error info
Assert.Contains(@"<Output>", trxContent, trxContent);
Assert.Contains(@"<ErrorInfo>", trxContent, trxContent);
// Verify that exception message is present
Assert.Contains(@"<Message>", trxContent, trxContent);
Assert.Contains("Assert.AreEqual failed. Expected:<1>. Actual:<2>.", trxContent, trxContent);
// Verify that stack trace is present
Assert.Contains(@"<StackTrace>", trxContent, trxContent);
Assert.Contains("at MSTestTrxReport.UnitTest1.FailingTest()", trxContent, trxContent);
}
public sealed class TestAssetFixture() : TestAssetFixtureBase()
{
public const string ProjectName = "MSTestTrxReport";
public string TargetAssetPath => GetAssetPath(ProjectName);
public override (string ID, string Name, string Code) GetAssetsToGenerate() => (ProjectName, ProjectName,
SourceCode
.PatchTargetFrameworks(TargetFrameworks.All)
.PatchCodeWithReplace("$MicrosoftTestingPlatformVersion$", MicrosoftTestingPlatformVersion)
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion));
private const string SourceCode = """
#file MSTestTrxReport.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<EnableMSTestRunner>true</EnableMSTestRunner>
<TargetFrameworks>$TargetFrameworks$</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Testing.Platform" Version="$MicrosoftTestingPlatformVersion$" />
<PackageReference Include="MSTest" Version="$MSTestVersion$" />
</ItemGroup>
</Project>
#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!;
}
[TestClass]
public sealed class TrxReportDataDrivenOutputTests : AcceptanceTestBase<TrxReportDataDrivenOutputTests.TestAssetFixture>
{
/// <summary>
/// Regression test for https://github.com/microsoft/testfx/issues/7908.
/// Verifies that data-driven test output does not accumulate across data rows in the TRX file.
/// Each data row's StdOut in the TRX should contain only that row's output, not output from previous rows.
/// </summary>
[TestMethod]
[DynamicData(nameof(TargetFrameworks.AllForDynamicData), typeof(TargetFrameworks))]
public async Task TrxReport_DataDrivenTestOutput_DoesNotAccumulateAcrossRows(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.Success);
string trxFile = Directory.GetFiles(testHost.DirectoryName, $"{fileName}.trx", SearchOption.AllDirectories).Single();
string trxContent = File.ReadAllText(trxFile);
XNamespace ns = "http://microsoft.com/schemas/VisualStudio/TeamTest/2010";
var trxDoc = XDocument.Parse(trxContent);
var results = trxDoc.Descendants(ns + "UnitTestResult").ToList();
// We have 3 data rows, each writing a unique marker like "UNIQUE_ROW_0_MARKER", "UNIQUE_ROW_1_MARKER", "UNIQUE_ROW_2_MARKER"
Assert.IsGreaterThanOrEqualTo(3, results.Count, $"Expected at least 3 test results but found {results.Count}. TRX content:\n{trxContent}");
int resultsWithOutput = 0;
foreach (XElement result in results)
{
string? stdOut = result.Descendants(ns + "StdOut").FirstOrDefault()?.Value;
if (stdOut is null)
{
continue;
}
resultsWithOutput++;
// Count how many unique row markers appear in this single result's StdOut.
// Each result should contain exactly ONE marker (its own row's output).
int markerCount = 0;
for (int i = 0; i < 3; i++)
{
if (stdOut.Contains($"UNIQUE_ROW_{i}_MARKER"))
{
markerCount++;
}
}
Assert.AreEqual(
1,
markerCount,
$"Test result '{result.Attribute("testName")?.Value}' contains output from {markerCount} data rows. " +
$"Each row should only contain its own output. StdOut:\n{stdOut}");
}
Assert.IsGreaterThanOrEqualTo(3, resultsWithOutput, $"Expected at least 3 test results to have StdOut output but only {resultsWithOutput} did. TRX content:\n{trxContent}");
}
public sealed class TestAssetFixture() : TestAssetFixtureBase()
{
public const string ProjectName = "MSTestTrxDataDriven";
public string TargetAssetPath => GetAssetPath(ProjectName);
public override (string ID, string Name, string Code) GetAssetsToGenerate() => (ProjectName, ProjectName,
SourceCode
.PatchTargetFrameworks(TargetFrameworks.All)
.PatchCodeWithReplace("$MicrosoftTestingPlatformVersion$", MicrosoftTestingPlatformVersion)
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion));
private const string SourceCode = """
#file MSTestTrxDataDriven.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<EnableMSTestRunner>true</EnableMSTestRunner>
<TargetFrameworks>$TargetFrameworks$</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Testing.Platform" Version="$MicrosoftTestingPlatformVersion$" />
<PackageReference Include="MSTest" Version="$MSTestVersion$" />
</ItemGroup>
</Project>
#file UnitTest1.cs
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MSTestTrxDataDriven;
[TestClass]
public class UnitTest1
{
[TestMethod]
[DataRow(0)]
[DataRow(1)]
[DataRow(2)]
public void DataDrivenTestWithOutput(int row)
{
Console.WriteLine($"UNIQUE_ROW_{row}_MARKER");
}
}
""";
}
public TestContext TestContext { get; set; } = null!;
}