-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrxProcessor.cs
71 lines (62 loc) · 2.37 KB
/
TrxProcessor.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
using System.Xml.Linq;
namespace DotnetCtrfJsonReporter
{
public class TrxProcessor
{
public TestResultsModel ProcessTrxFile(string filePath)
{
var trxReport = XDocument.Load(filePath);
var testResults = trxReport.Descendants().Where(d => d.Name.LocalName == "UnitTestResult");
var timesElement = trxReport.Descendants().FirstOrDefault(d => d.Name.LocalName == "Times");
var totals = new SummaryModel();
if (timesElement != null)
{
totals.Start = ConvertToEpochMilliseconds(ParseDateTime(timesElement.Attribute("start")?.Value));
totals.Stop = ConvertToEpochMilliseconds(ParseDateTime(timesElement.Attribute("finish")?.Value));
}
foreach (var testResult in testResults)
{
totals.Tests++;
var nunitOutcome = testResult.Attribute("outcome")?.Value;
var ctrfOutcome = StatusMapper.MapToCtrfStatus(nunitOutcome);
switch (ctrfOutcome)
{
case "passed":
totals.Passed++;
break;
case "failed":
totals.Failed++;
break;
case "skipped":
totals.Skipped++;
break;
case "other":
totals.Other++;
break;
}
}
var testProcessor = new TestProcessor();
var tests = testProcessor.ProcessTests(trxReport);
return new TestResultsModel
{
Results = new ResultsModel
{
Tool = new ToolModel { Name = "nunit" },
Summary = totals,
Tests = tests
}
};
}
private DateTime ParseDateTime(string dateTimeStr)
{
return DateTime.TryParse(dateTimeStr, out DateTime dateTime)
? dateTime
: DateTime.MinValue;
}
private long ConvertToEpochMilliseconds(DateTime dateTime)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return (long)(dateTime.ToUniversalTime() - epoch).TotalMilliseconds;
}
}
}