Skip to content

Commit 1f4e83f

Browse files
authored
Merge pull request #2 from mvtm-dn/1-scenario-as-testcase
Created "scenario as test case" case.
2 parents 42c9e96 + 431c439 commit 1f4e83f

File tree

4 files changed

+938
-241
lines changed

4 files changed

+938
-241
lines changed

index.js

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,17 @@ let createProperty=(name, value)=>{
3838
convertNameToId=(obj)=>{
3939
return obj.replace(/ /g, '-').toLowerCase();
4040
};
41-
42-
4341

4442
class JUnitFormatter extends Formatter {
4543
/** @param {Options} options */
4644
constructor(options) {
4745
super(options);
4846
this._result=[];
49-
50-
options.eventBroadcaster.on('test-run-started', ()=>{
51-
this._result=[];
52-
});
53-
54-
options.eventBroadcaster.on('test-case-finished', ({ sourceLocation })=>{
47+
48+
let scenarioAsSuite=({ sourceLocation })=>{
5549
const { gherkinDocument, pickle, testCase } = options.eventDataCollector.getTestCaseData(sourceLocation);
5650
let testSuiteId=`${convertNameToId(gherkinDocument.feature.name)};${convertNameToId(pickle.name)}`;
57-
let attr={name:testSuiteId,tests:0,failures:0,skipped:0,errors:0,time:((testCase.result.duration||0)/1000).toFixed(3)},
51+
let attr={name:testSuiteId,tests:0,failures:0,skipped:0,errors:0,time:testCase.result.duration||0},
5852
testSuite=[{_attr:attr}];
5953

6054
if (options.withPackage) {
@@ -102,6 +96,10 @@ class JUnitFormatter extends Formatter {
10296
testCase.push({skipped: []});
10397
attr.skipped+=1;
10498
break;
99+
case 'ambiguous':
100+
testCase.push(createFailureOrError("error",result.exception));
101+
attr.errors+=1;
102+
break;
105103
default:
106104
break;
107105
// testCase.push(createFailure(`Unknown status - ${step.result.status}`));
@@ -112,10 +110,82 @@ class JUnitFormatter extends Formatter {
112110
});
113111
this._result.push({testsuite:testSuite});
114112

113+
};
114+
115+
let scenarioAsStep=({ sourceLocation })=>{
116+
const { gherkinDocument, pickle, testCase } = options.eventDataCollector.getTestCaseData(sourceLocation);
117+
let testSuiteId=`${convertNameToId(gherkinDocument.feature.name)}`,
118+
testCaseId=`${convertNameToId(pickle.name)}`;
119+
let attr,testSuite;
120+
if (this._result.length && this._result[this._result.length-1].testsuite[0]._attr.name===testSuiteId) {
121+
testSuite=this._result[this._result.lenght-1].testsuite;
122+
attr=testSuite[0]._attr;
123+
}
124+
else {
125+
attr={name:testSuiteId,tests:0,failures:0,skipped:0,errors:0,time:0};
126+
testSuite=[{_attr:attr}];
127+
this._result.push({testsuite:testSuite});
128+
}
129+
attr.time+=testCase.result.duration;
130+
attr.tests+=1;
131+
let testCaseTag=[
132+
{
133+
_attr:{
134+
classname:testCaseId,
135+
name:pickle.name,
136+
time:((testCase.result.duration ||0)/1000).toFixed(3)
137+
}
138+
}
139+
];
140+
141+
testCase.steps.every((step,index)=>{
142+
const {gherkinKeyword, pickleStep } = options.eventDataCollector.getTestStepData({testCase:testCase,index:index});
143+
if (gherkinKeyword || (step.result && step.result.status==='failed')) {
144+
let result=step.result || {};
145+
switch (result.status) {
146+
case 'passed':
147+
break;
148+
case 'failed':
149+
testCaseTag.push(createFailureOrError(gherkinKeyword?"failure":"error",result.exception));
150+
attr[gherkinKeyword?"failures":"errors"]+=1;
151+
return false;
152+
case 'pending':
153+
case 'undefined':
154+
testCaseTag.push(createFailure(result.status === 'pending' ? 'Pending'
155+
: `Undefined step. Implement with the following snippet:\n ${gherkinKeyword.trim()}(/^${pickleStep.text}$/, function(callback) {\n // Write code here that turns the phrase above into concrete actions\n callback(null, 'pending');\n });`
156+
));
157+
attr.failures+=1;
158+
return false;
159+
case 'skipped':
160+
testCaseTag.push({skipped: []});
161+
attr.skipped+=1;
162+
return false;
163+
case 'ambiguous':
164+
testCaseTag.push(createFailureOrError("error",result.exception));
165+
attr.errors+=1;
166+
return false;
167+
default:
168+
break;
169+
// testCase.push(createFailure(`Unknown status - ${step.result.status}`));
170+
}
171+
}
172+
return true;
173+
});
174+
testSuite.push({testcase:testCaseTag});
175+
176+
};
177+
178+
options.eventBroadcaster.on('test-run-started', ()=>{
179+
this._result=[];
115180
});
116181

182+
options.eventBroadcaster.on('test-case-finished', options.scenarioAsStep?scenarioAsStep:scenarioAsSuite);
183+
117184

118185
options.eventBroadcaster.on('test-run-finished', ()=>{
186+
this._result.forEach((testSuite)=>{
187+
testSuite.testsuite[0]._attr.time=(testSuite.testsuite[0]._attr.time/1000).toFixed(3);
188+
});
119189
this.log(xml({ testsuites: this._result }, {indent:' ',declaration: { encoding: 'UTF-8' }}));
120190
});
121191
}

test/test.js

Lines changed: 0 additions & 232 deletions
This file was deleted.

0 commit comments

Comments
 (0)