-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathUnitTestResult.cs
218 lines (183 loc) · 8.8 KB
/
UnitTestResult.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Constants = Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Constants;
[Serializable]
public class UnitTestResult
{
/// <summary>
/// Initializes a new instance of the <see cref="UnitTestResult"/> class.
/// </summary>
internal UnitTestResult()
{
this.DatarowIndex = -1;
}
/// <summary>
/// Initializes a new instance of the <see cref="UnitTestResult"/> class.
/// </summary>
/// <param name="testFailedException"> The test failed exception. </param>
internal UnitTestResult(TestFailedException testFailedException)
: this()
{
this.Outcome = testFailedException.Outcome;
this.ErrorMessage = testFailedException.Message;
if (testFailedException.StackTraceInformation != null)
{
this.ErrorStackTrace = testFailedException.StackTraceInformation.ErrorStackTrace;
this.ErrorLineNumber = testFailedException.StackTraceInformation.ErrorLineNumber;
this.ErrorFilePath = testFailedException.StackTraceInformation.ErrorFilePath;
this.ErrorColumnNumber = testFailedException.StackTraceInformation.ErrorColumnNumber;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="UnitTestResult"/> class.
/// </summary>
/// <param name="outcome"> The outcome. </param>
/// <param name="errorMessage"> The error message. </param>
internal UnitTestResult(UnitTestOutcome outcome, string errorMessage)
: this()
{
this.Outcome = outcome;
this.ErrorMessage = errorMessage;
}
/// <summary>
/// Gets the display name for the result
/// </summary>
public string DisplayName { get; internal set; }
/// <summary>
/// Gets the outcome of the result
/// </summary>
public UnitTestOutcome Outcome { get; internal set; }
/// <summary>
/// Gets the errorMessage of the result
/// </summary>
public string ErrorMessage { get; internal set; }
/// <summary>
/// Gets the stackTrace of the result
/// </summary>
public string ErrorStackTrace { get; internal set; }
/// <summary>
/// Gets the id of the test case this result is for
/// </summary>
public Guid TestId { get; internal set; }
/// <summary>
/// Gets the execution id of the result
/// </summary>
public Guid ExecutionId { get; internal set; }
/// <summary>
/// Gets the parent execution id of the result
/// </summary>
public Guid ParentExecId { get; internal set; }
/// <summary>
/// Gets the inner results count of the result
/// </summary>
public int InnerResultsCount { get; internal set; }
/// <summary>
/// Gets the duration of the result
/// </summary>
public TimeSpan Duration { get; internal set; }
/// <summary>
/// Gets the standard output of the result
/// </summary>
public string StandardOut { get; internal set; }
/// <summary>
/// Gets the Standard Error of the result
/// </summary>
public string StandardError { get; internal set; }
/// <summary>
/// Gets the debug trace of the result
/// </summary>
public string DebugTrace { get; internal set; }
/// <summary>
/// Gets additional information messages generated by TestContext.WriteLine.
/// </summary>
public string TestContextMessages { get; internal set; }
/// <summary>
/// Gets the source code FilePath where the error was thrown.
/// </summary>
public string ErrorFilePath { get; internal set; }
/// <summary>
/// Gets the line number in the source code file where the error was thrown.
/// </summary>
public int ErrorLineNumber { get; private set; }
/// <summary>
/// Gets the column number in the source code file where the error was thrown.
/// </summary>
public int ErrorColumnNumber { get; private set; }
/// <summary>
/// Gets data row index in data source. Set only for results of individual
/// run of data row of a data driven test.
/// </summary>
public int DatarowIndex { get; internal set; }
/// <summary>
/// Gets the result files attached by the test.
/// </summary>
public IList<string> ResultFiles { get; internal set; }
/// <summary>
/// Convert parameter unitTestResult to testResult
/// </summary>
/// <param name="testCase"> The test Case. </param>
/// <param name="startTime"> The start Time. </param>
/// <param name="endTime"> The end Time. </param>
/// <param name="currentSettings">Current MSTest settings.</param>
/// <returns> The <see cref="TestResult"/>. </returns>
internal TestResult ToTestResult(TestCase testCase, DateTimeOffset startTime, DateTimeOffset endTime, MSTestSettings currentSettings)
{
Debug.Assert(testCase != null, "testCase");
var testResult = new TestResult(testCase)
{
DisplayName = this.DisplayName,
Duration = this.Duration,
ErrorMessage = this.ErrorMessage,
ErrorStackTrace = this.ErrorStackTrace,
Outcome = UnitTestOutcomeHelper.ToTestOutcome(this.Outcome, currentSettings),
StartTime = startTime,
EndTime = endTime
};
testResult.SetPropertyValue<Guid>(Constants.ExecutionIdProperty, this.ExecutionId);
testResult.SetPropertyValue<Guid>(Constants.ParentExecIdProperty, this.ParentExecId);
testResult.SetPropertyValue<int>(Constants.InnerResultsCountProperty, this.InnerResultsCount);
if (!string.IsNullOrEmpty(this.StandardOut))
{
TestResultMessage message = new TestResultMessage(TestResultMessage.StandardOutCategory, this.StandardOut);
testResult.Messages.Add(message);
}
if (!string.IsNullOrEmpty(this.StandardError))
{
TestResultMessage message = new TestResultMessage(TestResultMessage.StandardErrorCategory, this.StandardError);
testResult.Messages.Add(message);
}
if (!string.IsNullOrEmpty(this.DebugTrace))
{
string debugTraceMessagesinStdOut = string.Format(CultureInfo.InvariantCulture, "\n\n{0}\n{1}", Resource.DebugTraceBanner, this.DebugTrace);
TestResultMessage debugTraceMessage = new TestResultMessage(TestResultMessage.StandardOutCategory, debugTraceMessagesinStdOut);
testResult.Messages.Add(debugTraceMessage);
}
if (!string.IsNullOrEmpty(this.TestContextMessages))
{
string testContextMessagesInStdOut = string.Format(CultureInfo.InvariantCulture, "\n\n{0}\n{1}", Resource.TestContextMessageBanner, this.TestContextMessages);
TestResultMessage testContextMessage = new TestResultMessage(TestResultMessage.StandardOutCategory, testContextMessagesInStdOut);
testResult.Messages.Add(testContextMessage);
}
if (this.ResultFiles != null && this.ResultFiles.Count > 0)
{
AttachmentSet attachmentSet = new AttachmentSet(Constants.ExecutorUri, Resource.AttachmentSetDisplayName);
foreach (var resultFile in this.ResultFiles)
{
string pathToResultFile = PlatformServiceProvider.Instance.FileOperations.GetFullFilePath(resultFile);
UriDataAttachment attachment = new UriDataAttachment(new Uri(pathToResultFile), resultFile);
attachmentSet.Attachments.Add(attachment);
}
testResult.Attachments.Add(attachmentSet);
}
return testResult;
}
}
}