forked from microsoft/vstest
/
HtmlLogger.cs
441 lines (379 loc) · 16.5 KB
/
HtmlLogger.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// 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.Extensions.HtmlLogger
{
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using ObjectModel;
using Utilities;
using Utilities.Helpers;
using Utilities.Helpers.Interfaces;
using HtmlResource = Resources.Resources;
using HtmlLoggerConstants = Constants;
using NuGet.Frameworks;
/// <summary>
/// Logger for generating Html.
/// </summary>
[FriendlyName(HtmlLoggerConstants.FriendlyName)]
[ExtensionUri(HtmlLoggerConstants.ExtensionUri)]
public class HtmlLogger : ITestLoggerWithParameters
{
private readonly IFileHelper fileHelper;
private readonly XmlObjectSerializer xmlSerializer;
private readonly IHtmlTransformer htmlTransformer;
private Dictionary<string, string> parametersDictionary;
public HtmlLogger()
: this(new FileHelper(), new HtmlTransformer(), new DataContractSerializer(typeof(TestRunDetails)))
{
}
public HtmlLogger(IFileHelper fileHelper, IHtmlTransformer htmlTransformer,
XmlObjectSerializer dataContractSerializer)
{
this.fileHelper = fileHelper;
this.htmlTransformer = htmlTransformer;
this.xmlSerializer = dataContractSerializer;
}
/// <summary>
/// Gets the directory under which default html file and test results attachments should be saved.
/// </summary>
public string TestResultsDirPath { get; private set; }
/// <summary>
/// Total results are stored in sequential order
/// </summary>
/// <returns></returns>
public ConcurrentDictionary<Guid, ObjectModel.TestResult> Results { get; private set; }
/// <summary>
///
/// </summary>
public ConcurrentDictionary<string, TestResultCollection> ResultCollectionDictionary { get; private set; }
/// <summary>
/// Test results stores all the summary and the details of every results in hierarchical order.
/// </summary>
public TestRunDetails TestRunDetails { get; private set; }
/// <summary>
/// Total passed tests in the test results.
/// </summary>
public int PassedTests { get; private set; }
/// <summary>
/// Total failed tests in the test results.
/// </summary>
public int FailedTests { get; private set; }
/// <summary>
/// Total tests in the results.
/// </summary>
public int TotalTests { get; private set; }
/// <summary>
/// Total skipped tests in the results.
/// </summary>
public int SkippedTests { get; private set; }
/// <summary>
/// Path to the xml file.
/// </summary>
public string XmlFilePath { get; private set; }
/// <summary>
/// path to html file.
/// </summary>
public string HtmlFilePath { get; private set; }
/// <inheritdoc/>
public void Initialize(TestLoggerEvents events, string testResultsDirPath)
{
if (events == null)
{
throw new ArgumentNullException(nameof(events));
}
if (string.IsNullOrEmpty(testResultsDirPath))
{
throw new ArgumentNullException(nameof(testResultsDirPath));
}
// Register for the events.
events.TestRunMessage += TestMessageHandler;
events.TestResult += TestResultHandler;
events.TestRunComplete += TestRunCompleteHandler;
TestResultsDirPath = testResultsDirPath;
TestRunDetails = new TestRunDetails();
Results = new ConcurrentDictionary<Guid, ObjectModel.TestResult>();
ResultCollectionDictionary = new ConcurrentDictionary<string, TestResultCollection>();
}
/// <inheritdoc/>
public void Initialize(TestLoggerEvents events, Dictionary<string, string> parameters)
{
if (parameters == null)
{
throw new ArgumentNullException(nameof(parameters));
}
if (parameters.Count == 0)
{
throw new ArgumentException("No default parameters added", nameof(parameters));
}
parametersDictionary = parameters;
if (parameters.TryGetValue(HtmlLoggerConstants.LogFilePrefixKey, out string logFilePrefixValue) && parameters.TryGetValue(HtmlLoggerConstants.LogFileNameKey, out string logFileNameValue))
{
var htmlParameterErrorMsg = string.Format(CultureInfo.CurrentCulture, HtmlResource.PrefixAndNameProvidedError);
EqtTrace.Error(htmlParameterErrorMsg);
throw new ArgumentException(htmlParameterErrorMsg);
}
this.Initialize(events, parameters[DefaultLoggerParameterNames.TestRunDirectory]);
}
/// <summary>
/// Handles the message level information like warnings, errors etc..
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void TestMessageHandler(object sender, TestRunMessageEventArgs e)
{
ValidateArg.NotNull(sender, "sender");
ValidateArg.NotNull(e, "e");
switch (e.Level)
{
case TestMessageLevel.Informational:
if (TestRunDetails.RunLevelMessageInformational == null)
{
TestRunDetails.RunLevelMessageInformational = new List<string>();
}
TestRunDetails.RunLevelMessageInformational.Add(e.Message);
break;
case TestMessageLevel.Warning:
case TestMessageLevel.Error:
if (TestRunDetails.RunLevelMessageErrorAndWarning == null)
{
TestRunDetails.RunLevelMessageErrorAndWarning = new List<string>();
}
TestRunDetails.RunLevelMessageErrorAndWarning.Add(e.Message);
break;
default:
EqtTrace.Info("htmlLogger.TestMessageHandler: The test message level is unrecognized: {0}",
e.Level.ToString());
break;
}
}
/// <summary>
/// Handles the result coming from vs test and store it in test results.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void TestResultHandler(object sender, TestResultEventArgs e)
{
ValidateArg.NotNull(sender, "sender");
ValidateArg.NotNull(e, "e");
var testResult = new ObjectModel.TestResult
{
DisplayName = e.Result.DisplayName ?? e.Result.TestCase.FullyQualifiedName,
FullyQualifiedName = e.Result.TestCase.FullyQualifiedName,
ErrorStackTrace = e.Result.ErrorStackTrace,
ErrorMessage = e.Result.ErrorMessage,
TestResultId = e.Result.TestCase.Id,
Duration = GetFormattedDurationString(e.Result.Duration),
ResultOutcome = e.Result.Outcome
};
var executionId = GetExecutionId(e.Result);
var parentExecutionId = GetParentExecutionId(e.Result);
ResultCollectionDictionary.TryGetValue(e.Result.TestCase.Source, out var testResultCollection);
if (testResultCollection == null)
{
testResultCollection = new TestResultCollection(e.Result.TestCase.Source)
{
ResultList = new List<ObjectModel.TestResult>(),
FailedResultList = new List<ObjectModel.TestResult>(),
};
ResultCollectionDictionary.TryAdd(e.Result.TestCase.Source, testResultCollection);
TestRunDetails.ResultCollectionList.Add(testResultCollection);
}
TotalTests++;
switch (e.Result.Outcome)
{
case TestOutcome.Failed:
FailedTests++;
break;
case TestOutcome.Passed:
PassedTests++;
break;
case TestOutcome.Skipped:
SkippedTests++;
break;
default:
break;
}
Results.TryAdd(executionId, testResult);
// Check for parent execution id to store the test results in hierarchical way
if (parentExecutionId == Guid.Empty)
{
if (e.Result.Outcome == TestOutcome.Failed)
{
testResultCollection.FailedResultList.Add(testResult);
}
testResultCollection.ResultList.Add(testResult);
}
else
{
AddToParentResult(parentExecutionId, testResult);
}
}
private void AddToParentResult(Guid parentExecutionId, ObjectModel.TestResult testResult)
{
if (Results.TryGetValue(parentExecutionId, out var parentTestResult))
{
if (parentTestResult.InnerTestResults == null)
parentTestResult.InnerTestResults = new List<ObjectModel.TestResult>();
parentTestResult.InnerTestResults.Add(testResult);
}
}
/// <summary>
/// Creates a summary of tests and populates the html file by transforming the xml file with help of xslt file.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e)
{
TestRunDetails.Summary = new TestRunSummary
{
FailedTests = FailedTests,
PassedTests = PassedTests,
TotalTests = TotalTests,
SkippedTests = SkippedTests,
PassPercentage = (PassedTests * 100) / TotalTests,
TotalRunTime = GetFormattedDurationString(e.ElapsedTimeInRunningTests),
};
if (this.parametersDictionary.TryGetValue(HtmlLoggerConstants.LogFilePrefixKey, out string logFilePrefixValue) && !string.IsNullOrWhiteSpace(logFilePrefixValue))
{
var framework = this.parametersDictionary[DefaultLoggerParameterNames.TargetFramework];
if (framework != null)
{
framework = NuGetFramework.Parse(framework).GetShortFolderName();
logFilePrefixValue = logFilePrefixValue + "_" + framework;
}
logFilePrefixValue = logFilePrefixValue + DateTime.Now.ToString("_yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo) + $".{HtmlLoggerConstants.HtmlFileExtension}";
this.HtmlFilePath = Path.Combine(TestResultsDirPath, logFilePrefixValue);
}
else
{
if (parametersDictionary.TryGetValue(HtmlLoggerConstants.LogFileNameKey, out string logFileNameValue) && !string.IsNullOrWhiteSpace(logFileNameValue))
{
this.HtmlFilePath = Path.Combine(TestResultsDirPath, logFileNameValue);
}
}
PopulateHtmlFile();
}
private void PopulateHtmlFile()
{
try
{
var fileName = string.Format(CultureInfo.CurrentCulture, "{0}_{1}_{2}",
Environment.GetEnvironmentVariable("UserName"), Environment.MachineName,
FormatDateTimeForRunName(DateTime.Now));
XmlFilePath = GetFilePath(HtmlLoggerConstants.XmlFileExtension, fileName);
using (var xmlStream = fileHelper.GetStream(XmlFilePath, FileMode.Create))
{
xmlSerializer.WriteObject(xmlStream, TestRunDetails);
}
if (string.IsNullOrEmpty(HtmlFilePath))
{
HtmlFilePath = GetFilePath(HtmlLoggerConstants.HtmlFileExtension, fileName);
}
htmlTransformer.Transform(XmlFilePath, HtmlFilePath);
}
catch (Exception ex)
{
EqtTrace.Error("HtmlLogger : Failed to populate html file. Exception : {0}",
ex.ToString());
ConsoleOutput.Instance.Error(false, string.Concat(HtmlResource.HtmlLoggerError), ex.Message);
return;
}
finally
{
if (XmlFilePath != null)
{
this.fileHelper.Delete(XmlFilePath);
}
}
var htmlFilePathMessage = string.Format(CultureInfo.CurrentCulture, HtmlResource.HtmlFilePath, HtmlFilePath);
EqtTrace.Info(htmlFilePathMessage);
ConsoleOutput.Instance.Information(false, htmlFilePathMessage);
}
private string GetFilePath(string fileExtension, string fileName)
{
var fullFileFormat = $".{fileExtension}";
return Path.Combine(TestResultsDirPath, string.Concat("TestResult_", fileName, fullFileFormat));
}
private string FormatDateTimeForRunName(DateTime timeStamp)
{
return timeStamp.ToString("yyyyMMdd_HHmmss", DateTimeFormatInfo.InvariantInfo);
}
/// <summary>
/// Gives the parent execution id of a TestResult.
/// </summary>
/// <param name="testResult"></param>
/// <returns></returns>
private Guid GetParentExecutionId(TestPlatform.ObjectModel.TestResult testResult)
{
var parentExecutionIdProperty = testResult.Properties.FirstOrDefault(property =>
property.Id.Equals(HtmlLoggerConstants.ParentExecutionIdPropertyIdentifier));
return parentExecutionIdProperty == null
? Guid.Empty
: testResult.GetPropertyValue(parentExecutionIdProperty, Guid.Empty);
}
/// <summary>
/// Gives the execution id of a TestResult.
/// </summary>
/// <param name="testResult"></param>
/// <returns></returns>
private Guid GetExecutionId(TestPlatform.ObjectModel.TestResult testResult)
{
var executionIdProperty = testResult.Properties.FirstOrDefault(property =>
property.Id.Equals(HtmlLoggerConstants.ExecutionIdPropertyIdentifier));
var executionId = Guid.Empty;
if (executionIdProperty != null)
{
executionId = testResult.GetPropertyValue(executionIdProperty, Guid.Empty);
}
return executionId.Equals(Guid.Empty) ? Guid.NewGuid() : executionId;
}
/// <summary>
/// Converts the time span format to readable string.
/// </summary>
/// <param name="duration"></param>
/// <returns></returns>
internal string GetFormattedDurationString(TimeSpan duration)
{
if (duration == default(TimeSpan))
{
return null;
}
var time = new List<string>();
if (duration.Days > 0)
{
time.Add("> 1d");
}
else
{
if (duration.Hours > 0)
{
time.Add(duration.Hours + "h");
}
if (duration.Minutes > 0)
{
time.Add(duration.Minutes + "m");
}
if (duration.Hours == 0)
{
if (duration.Seconds > 0)
{
time.Add(duration.Seconds + "s");
}
if (duration.Milliseconds > 0 && duration.Minutes == 0)
{
time.Add(duration.Milliseconds + "ms");
}
}
}
return time.Count == 0 ? "< 1ms" : string.Join(" ", time);
}
}
}