forked from Inedo/bmx-nunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNUnitAppAction.cs
173 lines (146 loc) · 6.99 KB
/
NUnitAppAction.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
using System;
using System.Globalization;
using System.IO;
using System.Xml;
using Inedo.BuildMaster;
using Inedo.BuildMaster.Extensibility.Actions;
using Inedo.BuildMaster.Extensibility.Actions.Testing;
using Inedo.BuildMaster.Extensibility.Agents;
using Inedo.BuildMaster.Web;
namespace Inedo.BuildMasterExtensions.NUnit
{
/// <summary>
/// Action that runs NUnit unit tests on a specified project, assembly, or NUnit file.
/// </summary>
[ActionProperties(
"Execute NUnit Tests",
"Runs NUnit unit tests on a specified project, assembly, or NUnit file.")]
[Tag(Tags.UnitTests)]
[CustomEditor(typeof(NUnitActionEditor))]
[RequiresInterface(typeof(IFileOperationsExecuter))]
public sealed class NUnitAppAction : UnitTestActionBase
{
/// <summary>
/// Initializes a new instance of the <see cref="NUnitAppAction"/> class.
/// </summary>
public NUnitAppAction()
{
this.TreatInconclusiveAsFailure = true;
}
/// <summary>
/// Gets or sets the test runner exe path
/// </summary>
[Persistent]
public string ExePath { get; set; }
/// <summary>
/// Gets or sets the file nunit will test against (could be dll, proj, or config file based on test runner)
/// </summary>
[Persistent]
public string TestFile { get; set; }
/// <summary>
/// Gets or sets the .NET Framework version to run against.
/// </summary>
[Persistent]
public string FrameworkVersion { get; set; }
/// <summary>
/// Gets or sets the additional arguments.
/// </summary>
[Persistent]
public string AdditionalArguments { get; set; }
/// <summary>
/// Gets or sets the path of the output XML generated by NUnit.
/// </summary>
[Persistent]
public string CustomXmlOutputPath { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to treat inconclusive tests as failures.
/// </summary>
[Persistent]
public bool TreatInconclusiveAsFailure { get; set; }
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
/// <remarks>
/// This should return a user-friendly string describing what the Action does
/// and the state of its important persistent properties.
/// </remarks>
public override string ToString()
{
return string.Format("Run NUnit Unit Tests on {0}{1}", this.TestFile, Util.ConcatNE(" with the additional arguments: ", this.AdditionalArguments));
}
/// <summary>
/// Runs a unit test against the target specified in the action.
/// After the test is run, use the <see cref="M:RecordResult" /> method
/// to save the test results to the database.
/// </summary>
protected override void RunTests()
{
var doc = new XmlDocument();
var agent = this.Context.Agent;
{
var fileOps = agent.GetService<IFileOperationsExecuter>();
string nunitExePath = this.GetNUnitExePath(fileOps);
string tmpFileName = this.GetXmlOutputPath(fileOps);
this.ExecuteCommandLine(
nunitExePath,
string.Format("\"{0}\" /xml:\"{1}\" {2}", this.TestFile, tmpFileName, this.AdditionalArguments),
this.Context.SourceDirectory
);
using (var stream = new MemoryStream(fileOps.ReadFileBytes(tmpFileName), false))
{
doc.Load(stream);
}
}
var testStart = DateTime.Parse(doc.SelectSingleNode("//test-results").Attributes["time"].Value);
var nodeList = doc.SelectNodes("//test-case");
foreach (XmlNode node in nodeList)
{
string testName = node.Attributes["name"].Value;
// skip tests that weren't actually run
if (string.Equals(node.Attributes["executed"].Value, "false", StringComparison.OrdinalIgnoreCase))
{
LogInformation(String.Format("NUnit Test: {0} (skipped)", testName));
continue;
}
bool nodeResult = node.Attributes["success"].Value.Equals("True", StringComparison.OrdinalIgnoreCase) ||
(!this.TreatInconclusiveAsFailure && node.Attributes["result"].Value.Equals("inconclusive", StringComparison.OrdinalIgnoreCase));
var numberStyles = NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign |NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent;
double testLength = 0;
if (!double.TryParse(node.Attributes["time"].Value, numberStyles, CultureInfo.InvariantCulture, out testLength))
{
this.LogWarning("Error parsing " + node.Attributes["time"].Value + " as a number.");
};
this.LogInformation(string.Format("NUnit Test: {0}, Result: {1}, Test Length: {2} secs",
testName,
nodeResult,
testLength));
this.RecordResult(
testName,
nodeResult,
node.OuterXml,
testStart,
testStart.AddSeconds(testLength)
);
testStart = testStart.AddSeconds(testLength);
}
}
private string GetNUnitExePath(IFileOperationsExecuter fileOps)
{
if (!string.IsNullOrWhiteSpace(this.ExePath))
return fileOps.GetWorkingDirectory(this.Context.ApplicationId, this.Context.DeployableId ?? 0, this.ExePath);
var configurer = (NUnitConfigurer)this.GetExtensionConfigurer();
if (string.IsNullOrWhiteSpace(configurer.NUnitConsoleExePath))
throw new InvalidOperationException("The path to NUnit was not specified in either the action or the selected NUnit extension's configuration.");
return fileOps.GetWorkingDirectory(this.Context.ApplicationId, this.Context.DeployableId ?? 0, configurer.NUnitConsoleExePath);
}
private string GetXmlOutputPath(IFileOperationsExecuter fileOps)
{
if (string.IsNullOrWhiteSpace(this.CustomXmlOutputPath))
return fileOps.CombinePath(this.Context.TempDirectory, Guid.NewGuid().ToString() + ".xml");
return fileOps.CombinePath(this.Context.SourceDirectory, this.CustomXmlOutputPath);
}
}
}