-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnunit3.js
348 lines (348 loc) · 10.9 KB
/
nunit3.js
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
"use strict";
var _this = this;
exports.__esModule = true;
var os_1 = require("os");
var fs_1 = require("fs");
function nUnitTime(time) {
return time.toISOString().split(".")[0].replace("T", " ") + "Z";
}
;
var TestRun = /** @class */ (function () {
function TestRun() {
// "2" is a hard-coded value by convention.
this.id = "2";
this.testcasecount = 0;
this.result = "Passed";
this.total = 0;
this.passed = 0;
this.failed = 0;
this.inconclusive = 0;
this.skipped = 0;
this.asserts = 0;
// Version of NUnit this is being based off
this["engine-version"] = "3.6.1.0";
// Represents the version of Intern being used.
this["clr-version"] = "4.X";
this["start-time"] = nUnitTime(new Date());
this["end-time"] = nUnitTime(new Date());
this.duration = 0;
this.suites = [];
}
TestRun.prototype.props = function () {
return [
"id",
"testcasecount",
"result",
"total",
"passed",
"failed",
"inconclusive",
"skipped",
"asserts",
"engine-version",
"clr-version",
"start-time",
"end-time",
"duration"
];
};
;
;
TestRun.prototype.toString = function () {
var _this = this;
var COMMANDLINE = "<command-line><![CDATA[npx intern]]></command-line>";
// Builds a string like <test-run prop1=val1 prop2=val2>inner</test-run>
var outer = "<test-run ";
outer += this.props().map(function (propertyName) {
return propertyName + "=\"" + _this[propertyName] + "\"";
}).join(" ");
outer += ">";
outer += COMMANDLINE;
outer += this.suites.map(function (suite) {
return suite.toString();
}).join("");
outer += "</test-run>";
return outer;
};
;
return TestRun;
}());
;
var Settings = /** @class */ (function () {
function Settings() {
}
Settings.prototype.toString = function () {
var outer = "<settings>";
outer += "</settings>";
return outer;
};
return Settings;
}());
var TestCase = /** @class */ (function () {
function TestCase(testCase) {
var time = new Date();
var endTime = nUnitTime(time);
time.setMilliseconds(time.getMilliseconds() - testCase.timeElapsed || 0);
var startTime = nUnitTime(time);
this.id = "0-0";
this.name = testCase.name;
this.fullname = testCase.name; /* redundant */
this.classname = "unspecified";
this.runstate = "unspecified";
this.seed = "null";
this.result = "" + (testCase.error ? "Failed" : "Passed");
this.label = "" + (testCase.error ? testCase.error : "unspecified");
this.site = "Test";
this["start-time"] = startTime;
;
this["end-time"] = endTime;
// Traditionally duration is reported in seconds
this.duration = (testCase.timeElapsed || 0) / 1000;
this.asserts = 1;
}
;
TestCase.prototype.properties = function () {
return [
"id",
"name",
"fullname",
"methodname",
"classname",
"runstate",
"seed",
"result",
"label",
"site",
"start-time",
"end-time",
"duration",
"asserts"
];
};
;
TestCase.prototype.toString = function () {
var _this = this;
// Builds a string like <test-case prop1=val1 prop2=val2/>
var outer = "<test-case ";
outer += this.properties().map(function (propertyName) {
return propertyName + "=\"" + _this[propertyName] + "\"";
}).join(" ");
outer += "/>";
return outer;
};
;
return TestCase;
}());
var Environment = /** @class */ (function () {
function Environment() {
this["framework-version"] = "3.5.0.0";
this["clr-version"] = "2.0.50727.8784";
this["os-version"] = "Microsoft Windows NT 10.0.15063.0";
this.platform = "Win32NT";
this.cwd = process.cwd();
this["machine-name"] = os_1.hostname();
this.user = os_1.userInfo().username;
this["user-domain"] = this.user;
this.culture = "en-GB";
this.uiculture = "en-US";
this["os-architecture"] = os_1.arch();
}
;
Environment.prototype.props = function () {
return [
"framework-version",
"clr-version",
"cwd",
"machine-name",
"user",
"user-domain",
"culture",
"uiculture",
"os-architecture"
];
};
Environment.prototype.toString = function () {
var _this = this;
// Builds a string like <environment prop1=val1 prop2=val2/>
var outer = "<environment ";
outer += this.props().map(function (propertyName) {
if (_this[propertyName]) {
return propertyName + "=\"" + _this[propertyName] + "\"";
}
else {
return "";
}
}).join(" ");
outer += "/>";
return outer;
};
;
return Environment;
}());
;
var TestSuite = /** @class */ (function () {
function TestSuite(suite) {
var time = new Date();
var endTime = nUnitTime(time);
time.setMilliseconds(time.getMilliseconds() - suite.timeElapsed);
var startTime = nUnitTime(time);
// For whatever reason using "TestSuite" results in this not being able to be reported in VSTS.
this.type = "Assembly";
this.id = suite.id;
this.name = suite.name;
this.fullname = suite.name; /* Redundant */
this.classname = suite.name; /* Redundant */
this.testcasecount = suite.numTests;
this.runstate = "" + (suite.skipped ? "Skipped" : "Runnable");
this.result = "" + (suite.numFailedTests == 0 ? "Passed" : "Failed");
this.label = "unspecified";
this.site = "unspecified";
this["start-time"] = startTime;
this["end-time"] = endTime;
// Traditionally duration is reported in seconds
this.duration = (suite.timeElapsed || 0) / 1000;
this.total = suite.numTests;
this.passed = suite.numPassedTests;
this.failed = suite.numFailedTests;
this.inconclusive = suite.tests.length - this.failed - this.passed - this.skipped;
this.skipped = suite.numSkippedTests;
// TODO: This is not actually right. Right now there isn't anyway to distinguish
// between the amount of tests and the amount of assertions. A test can have multiple
// assertions
this.asserts = suite.numTests;
// @ts-ignore
if (suite.tests[0].constructor.name === "Suite") {
this["test-suites"] = suite.tests.map(function (suite, index) {
var newTestSuite = new TestSuite(suite);
newTestSuite.id = "0-" + (index + 1);
return newTestSuite;
});
// @ts-ignore
}
else if (suite.tests[0].constructor.name === "Test") {
this["test-cases"] = suite.tests.map(function (testCase, index) {
var newTestCase = new TestCase(testCase);
newTestCase.id = "0-" + (index + 1);
return newTestCase;
});
}
this.environment = new Environment();
this.settings = new Settings();
// Later, these items should contain various metadata that can later be
// included in VSTS or similar reporting system.
this.properties = null;
this.reason = null;
this.failure = null;
this.output = null;
this.attachments = null;
}
;
TestSuite.prototype.inners = function () {
return [
"test-cases",
"test-suites",
"environment",
"settings"
// TODO: Optional and currently not included.
// "properties",
// "reason",
// "failure",
// "output",
// "attachments"
];
};
;
TestSuite.prototype.props = function () {
return [
"type",
"id",
"name",
"fullname",
"classname",
"testcasecount",
"runstate",
"result",
"label",
"site",
"start-time",
"end-time",
"duration",
"total",
"passed",
"failed",
"inconclusive",
"skipped",
"asserts"
];
};
;
TestSuite.prototype.toString = function () {
var _this = this;
// Builds a string like <test-suite prop1=val1 prop2=val2>inner</test-suite>
var outer = "<test-suite ";
outer += this.props().map(function (propertyName) {
if (_this[propertyName]) {
return propertyName + "=\"" + _this[propertyName] + "\"";
}
else {
return "";
}
}).join(" ");
outer += ">";
var inner = "";
inner += this.inners().map(function (propertyName) {
if (_this[propertyName] && _this[propertyName].length) {
return _this[propertyName].map(function (val) {
return val.toString();
}).join("");
}
else if (_this[propertyName]) {
return _this[propertyName].toString();
}
else {
return;
}
}).join("");
outer += inner;
outer += "</test-suite>";
return outer;
};
;
return TestSuite;
}());
;
var HEADER = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>";
intern.on("runStart", function () {
_this.testRun = new TestRun();
_this.start = new Date().getMilliseconds();
});
intern.on("runEnd", function () {
_this.testRun["end-time"] = nUnitTime(new Date());
_this.testRun.duration = Math.abs(_this.start - new Date().getMilliseconds());
fs_1.writeFile("nunit3.xml", HEADER + _this.testRun.toString(), function (error) {
if (error) {
throw error;
}
console.log("nunit3.xml saved");
});
});
intern.on("testEnd", function (test) {
_this.testRun.testcasecount += 1;
_this.testRun.total += 1; // TODO: Modify this to account for skipped runs.
if (test.error) {
_this.testRun.result = "Failed";
_this.testRun.failed += 1;
}
else if (test.skipped) {
_this.testRun.skipped += 1;
}
else {
_this.testRun.passed += 1;
}
});
intern.on("suiteEnd", function (suite) {
if (suite.name == "node") {
return;
}
_this.testRun.suites.push(new TestSuite(suite));
});