-
Notifications
You must be signed in to change notification settings - Fork 326
/
Copy pathTestResult.cs
338 lines (294 loc) · 13.2 KB
/
TestResult.cs
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// 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;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Microsoft.VisualStudio.TestPlatform.CoreUtilities;
namespace Microsoft.VisualStudio.TestPlatform.ObjectModel;
/// <summary>
/// Represents the result of a test case.
/// </summary>
[DataContract]
public sealed class TestResult : TestObject
{
/// <summary>
/// Initializes a new instance of the <see cref="TestResult"/> class.
/// </summary>
/// <param name="testCase">The test case the result is for.</param>
public TestResult(TestCase testCase)
{
TestCase = testCase ?? throw new ArgumentNullException(nameof(testCase));
Messages = new Collection<TestResultMessage>();
Attachments = new Collection<AttachmentSet>();
// Default start and end time values for a test result are initialized to current time stamp
// to maintain compatibility.
StartTime = DateTimeOffset.UtcNow;
EndTime = DateTimeOffset.UtcNow;
}
/// <summary>
/// Gets the test case that this result is for.
/// </summary>
[DataMember]
public TestCase TestCase { get; private set; }
/// <summary>
/// Gets the list of attachment sets for this TestResult.
/// </summary>
[DataMember]
public Collection<AttachmentSet> Attachments { get; private set; }
/// <summary>
/// Gets or sets the outcome of a test case.
/// </summary>
[DataMember]
public TestOutcome Outcome { get; set; }
/// <summary>
/// Gets or sets the exception message.
/// </summary>
[DataMember]
public string? ErrorMessage { get; set; }
/// <summary>
/// Gets or sets the exception stack trace.
/// </summary>
[DataMember]
public string? ErrorStackTrace { get; set; }
/// <summary>
/// Gets or sets the TestResult Display name. Used for Data Driven Test (i.e. Data Driven Test. E.g. InlineData in xUnit)
/// </summary>
[DataMember]
public string? DisplayName { get; set; }
/// <summary>
/// Gets the test messages.
/// </summary>
[DataMember]
public Collection<TestResultMessage> Messages { get; private set; }
/// <summary>
/// Gets or sets test result ComputerName.
/// </summary>
[DataMember]
public string? ComputerName { get; set; }
/// <summary>
/// Gets or sets the test result Duration.
/// </summary>
[DataMember]
public TimeSpan Duration { get; set; }
/// <summary>
/// Gets or sets the test result StartTime.
/// </summary>
[DataMember]
public DateTimeOffset StartTime { get; set; }
/// <summary>
/// Gets or sets test result EndTime.
/// </summary>
[DataMember]
public DateTimeOffset EndTime { get; set; }
/// <summary>
/// Returns the TestProperties currently specified in this TestObject.
/// </summary>
public override IEnumerable<TestProperty> Properties
{
get
{
return TestResultProperties.Properties.Concat(base.Properties);
}
}
/// <inheritdoc/>
public override string ToString()
{
StringBuilder result = new();
// Add the outcome of the test and the name of the test.
result.AppendFormat(
CultureInfo.CurrentCulture,
Resources.Resources.BasicTestResultFormat,
TestCase.DisplayName,
TestOutcomeHelper.GetOutcomeString(Outcome));
// Add the error message and stack trace if this is a test failure.
if (Outcome == TestOutcome.Failed)
{
// Add Error message.
result.AppendLine();
result.AppendFormat(CultureInfo.CurrentCulture, Resources.Resources.TestFailureMessageFormat, ErrorMessage);
// Add stack trace if we have one.
if (!StringUtils.IsNullOrWhiteSpace(ErrorStackTrace))
{
result.AppendLine();
result.AppendFormat(
CultureInfo.CurrentCulture,
Resources.Resources.TestFailureStackTraceFormat,
ErrorStackTrace);
}
}
// Add any text messages we have.
if (Messages.Count > 0)
{
StringBuilder testMessages = new();
foreach (TestResultMessage message in Messages)
{
if (!StringUtils.IsNullOrEmpty(message?.Category) && !StringUtils.IsNullOrEmpty(message.Text))
{
testMessages.AppendFormat(
CultureInfo.CurrentCulture,
Resources.Resources.TestResultMessageFormat,
message.Category,
message.Text);
}
}
result.AppendLine();
result.AppendFormat(
CultureInfo.CurrentCulture,
Resources.Resources.TestResultTextMessagesFormat,
testMessages.ToString());
}
return result.ToString();
}
/// <summary>
/// Return TestProperty's value
/// </summary>
/// <returns></returns>
protected override object? ProtectedGetPropertyValue(TestProperty property, object? defaultValue)
{
ValidateArg.NotNull(property, nameof(property));
return property.Id switch
{
"TestResult.ComputerName" => ComputerName,
"TestResult.DisplayName" => DisplayName,
"TestResult.Duration" => Duration,
"TestResult.EndTime" => EndTime,
"TestResult.ErrorMessage" => ErrorMessage,
"TestResult.ErrorStackTrace" => ErrorStackTrace,
"TestResult.Outcome" => Outcome,
"TestResult.StartTime" => StartTime,
_ => base.ProtectedGetPropertyValue(property, defaultValue),
};
}
/// <summary>
/// Set TestProperty's value
/// </summary>
protected override void ProtectedSetPropertyValue(TestProperty property, object? value)
{
ValidateArg.NotNull(property, nameof(property));
switch (property.Id)
{
case "TestResult.ComputerName":
ComputerName = (string?)value; return;
case "TestResult.DisplayName":
DisplayName = (string?)value; return;
case "TestResult.Duration":
Duration = (TimeSpan)value!; return;
case "TestResult.EndTime":
EndTime = (DateTimeOffset)value!; return;
case "TestResult.ErrorMessage":
ErrorMessage = (string?)value; return;
case "TestResult.ErrorStackTrace":
ErrorStackTrace = (string?)value; return;
case "TestResult.Outcome":
Outcome = (TestOutcome)value!; return;
case "TestResult.StartTime":
StartTime = (DateTimeOffset)value!; return;
}
base.ProtectedSetPropertyValue(property, value);
}
}
/// <summary>
/// Represents the test result message.
/// </summary>
[DataContract]
public class TestResultMessage
{
// Bugfix: 297759 Moving the category from the resources to the code
// so that it works on machines which has eng OS & non-eng VS and vice versa.
/// <summary>
/// Standard Output Message Category
/// </summary>
public static readonly string StandardOutCategory = "StdOutMsgs";
/// <summary>
/// Standard Error Message Category
/// </summary>
public static readonly string StandardErrorCategory = "StdErrMsgs";
/// <summary>
/// Debug Trace Message Category
/// </summary>
public static readonly string DebugTraceCategory = "DbgTrcMsgs";
/// <summary>
/// Additional Information Message Category
/// </summary>
public static readonly string AdditionalInfoCategory = "AdtnlInfo";
/// <summary>
/// Initializes a new instance of the <see cref="TestResultMessage"/> class.
/// </summary>
/// <param name="category">Category of the message.</param>
/// <param name="text">Text of the message.</param>
public TestResultMessage(string category, string? text)
{
Category = category;
Text = text;
}
/// <summary>
/// Gets the message category
/// </summary>
[DataMember]
public string Category
{
get;
private set;
}
/// <summary>
/// Gets the message text
/// </summary>
[DataMember]
public string? Text
{
get;
private set;
}
}
/// <summary>
/// Well-known TestResult properties
/// </summary>
public static class TestResultProperties
{
#if !FullCLR
public static readonly TestProperty DisplayName = TestProperty.Register("TestResult.DisplayName", "TestResult Display Name", typeof(string), TestPropertyAttributes.Hidden, typeof(TestResult));
public static readonly TestProperty ComputerName = TestProperty.Register("TestResult.ComputerName", "Computer Name", typeof(string), TestPropertyAttributes.None, typeof(TestResult));
public static readonly TestProperty Outcome = TestProperty.Register("TestResult.Outcome", "Outcome", string.Empty, string.Empty, typeof(TestOutcome), ValidateOutcome, TestPropertyAttributes.None, typeof(TestResult));
public static readonly TestProperty Duration = TestProperty.Register("TestResult.Duration", "Duration", typeof(TimeSpan), typeof(TestResult));
public static readonly TestProperty StartTime = TestProperty.Register("TestResult.StartTime", "Start Time", typeof(DateTimeOffset), typeof(TestResult));
public static readonly TestProperty EndTime = TestProperty.Register("TestResult.EndTime", "End Time", typeof(DateTimeOffset), typeof(TestResult));
public static readonly TestProperty ErrorMessage = TestProperty.Register("TestResult.ErrorMessage", "Error Message", typeof(string), typeof(TestResult));
public static readonly TestProperty ErrorStackTrace = TestProperty.Register("TestResult.ErrorStackTrace", "Error Stack Trace", typeof(string), typeof(TestResult));
#else
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly TestProperty DisplayName = TestProperty.Register("TestResult.DisplayName", Resources.Resources.TestResultPropertyDisplayNameLabel, typeof(string), TestPropertyAttributes.Hidden, typeof(TestResult));
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly TestProperty ComputerName = TestProperty.Register("TestResult.ComputerName", Resources.Resources.TestResultPropertyComputerNameLabel, typeof(string), TestPropertyAttributes.None, typeof(TestResult));
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly TestProperty Outcome = TestProperty.Register("TestResult.Outcome", Resources.Resources.TestResultPropertyOutcomeLabel, string.Empty, string.Empty, typeof(TestOutcome), ValidateOutcome, TestPropertyAttributes.None, typeof(TestResult));
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly TestProperty Duration = TestProperty.Register("TestResult.Duration", Resources.Resources.TestResultPropertyDurationLabel, typeof(TimeSpan), typeof(TestResult));
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly TestProperty StartTime = TestProperty.Register("TestResult.StartTime", Resources.Resources.TestResultPropertyStartTimeLabel, typeof(DateTimeOffset), typeof(TestResult));
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly TestProperty EndTime = TestProperty.Register("TestResult.EndTime", Resources.Resources.TestResultPropertyEndTimeLabel, typeof(DateTimeOffset), typeof(TestResult));
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly TestProperty ErrorMessage = TestProperty.Register("TestResult.ErrorMessage", Resources.Resources.TestResultPropertyErrorMessageLabel, typeof(string), typeof(TestResult));
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly TestProperty ErrorStackTrace = TestProperty.Register("TestResult.ErrorStackTrace", Resources.Resources.TestResultPropertyErrorStackTraceLabel, typeof(string), typeof(TestResult));
#endif
internal static TestProperty[] Properties { get; } =
[
ComputerName,
DisplayName,
Duration,
EndTime,
ErrorMessage,
ErrorStackTrace,
Outcome,
StartTime
];
private static bool ValidateOutcome(object? value)
{
return value is TestOutcome testOutcome && testOutcome <= TestOutcome.NotFound && testOutcome >= TestOutcome.None;
}
}