|
| 1 | +/** Module for cucumber-junit-formatter before cucumber 6.0.0 */ |
| 2 | + |
| 3 | +"use strict;"; |
| 4 | + |
| 5 | +const {formatterHelpers} = require('cucumber'); |
| 6 | +const {createProperty,convertNameToId}=require("../util"); |
| 7 | + |
| 8 | +/** |
| 9 | +* Create failure tag definition |
| 10 | +* @param {String} message failure message |
| 11 | +* @return {Object} failure tag |
| 12 | +*/ |
| 13 | +const createFailure=(message)=>{ |
| 14 | + return { |
| 15 | + failure: [ |
| 16 | + { _attr: { message: message.split("\n").shift() } }, |
| 17 | + message |
| 18 | + ] |
| 19 | + }; |
| 20 | +}, |
| 21 | + |
| 22 | +/** |
| 23 | +* Create failure or error tag |
| 24 | +* @param {String} type tag name |
| 25 | +* @param {Error} exception exception |
| 26 | +* @return {Object} generated tag |
| 27 | +*/ |
| 28 | +createFailureOrError=(type,exception)=>{ |
| 29 | + let {name}=exception, |
| 30 | + ret ={}; |
| 31 | + ret[type]=[ |
| 32 | + { _attr: { message: name } }, |
| 33 | + formatterHelpers.formatError(exception) |
| 34 | + ]; |
| 35 | + return ret; |
| 36 | +}; |
| 37 | + |
| 38 | + |
| 39 | +module.exports={ |
| 40 | + scenarioAsSuite:(options,result)=>{return ({sourceLocation})=>{ |
| 41 | + const { gherkinDocument, pickle, testCase } = options.eventDataCollector.getTestCaseData(sourceLocation); |
| 42 | + let testSuiteId=`${convertNameToId(gherkinDocument.feature.name)};${convertNameToId(pickle.name)}`; |
| 43 | + let attr={name:testSuiteId,tests:0,failures:0,skipped:0,errors:0,time:testCase.result.duration||0}, |
| 44 | + testSuite=[{_attr:attr}]; |
| 45 | + |
| 46 | + if (options.withPackage) { |
| 47 | + attr.package=convertNameToId(pickle.name); |
| 48 | + } |
| 49 | + |
| 50 | + if (pickle.tags.length) { |
| 51 | + testSuite.push({properties:pickle.tags.map(tag=>createProperty("tag",tag.name))}); |
| 52 | + } |
| 53 | + |
| 54 | + testCase.steps.forEach((step,index)=>{ |
| 55 | + const {gherkinKeyword, pickleStep } = options.eventDataCollector.getTestStepData({testCase:testCase,index:index}); |
| 56 | + if (gherkinKeyword || (step.result && step.result.status==='failed')) { |
| 57 | + let result=step.result || {}; |
| 58 | + let testCaseId=convertNameToId(gherkinKeyword?pickleStep.text:`${pickle.name} ${index?'after':'before'}`); |
| 59 | + let testCase=[ |
| 60 | + { |
| 61 | + _attr:{ |
| 62 | + classname:testCaseId, |
| 63 | + name:gherkinKeyword?pickleStep.text:(pickle.name+(index?' after':' before')), |
| 64 | + time:((result.duration || 0)/1000).toFixed(3) |
| 65 | + } |
| 66 | + } |
| 67 | + ]; |
| 68 | + switch (result.status) { |
| 69 | + case 'passed': |
| 70 | + break; |
| 71 | + case 'failed': |
| 72 | + testCase.push(createFailureOrError(gherkinKeyword?"failure":"error",result.exception)); |
| 73 | + if (gherkinKeyword) { |
| 74 | + attr.failures+=1; |
| 75 | + } |
| 76 | + else { |
| 77 | + attr.errors+=1; |
| 78 | + } |
| 79 | + break; |
| 80 | + case 'pending': |
| 81 | + case 'undefined': |
| 82 | + testCase.push(createFailure(result.status === 'pending' ? 'Pending' |
| 83 | + : `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 });` |
| 84 | + )); |
| 85 | + attr.failures+=1; |
| 86 | + break; |
| 87 | + case 'skipped': |
| 88 | + testCase.push({skipped: []}); |
| 89 | + attr.skipped+=1; |
| 90 | + break; |
| 91 | + case 'ambiguous': |
| 92 | + testCase.push(createFailureOrError("error",result.exception)); |
| 93 | + attr.errors+=1; |
| 94 | + break; |
| 95 | + default: |
| 96 | + break; |
| 97 | + // testCase.push(createFailure(`Unknown status - ${step.result.status}`)); |
| 98 | + } |
| 99 | + attr.tests+=1; |
| 100 | + testSuite.push({testcase:testCase}); |
| 101 | + } |
| 102 | + }); |
| 103 | + result.push({testsuite:testSuite}); |
| 104 | + |
| 105 | + }; |
| 106 | + }, |
| 107 | + |
| 108 | + scenarioAsStep:(options,result)=>{return ({sourceLocation})=>{ |
| 109 | + const { gherkinDocument, pickle, testCase } = options.eventDataCollector.getTestCaseData(sourceLocation); |
| 110 | + let testSuiteId=`${convertNameToId(gherkinDocument.feature.name)}`, |
| 111 | + testCaseId=`${convertNameToId(pickle.name)}`; |
| 112 | + let attr,testSuite; |
| 113 | + if (result.length && result[result.length-1].testsuite[0]._attr.name===testSuiteId) { |
| 114 | + testSuite=result[result.length-1].testsuite; |
| 115 | + attr=testSuite[0]._attr; |
| 116 | + } |
| 117 | + else { |
| 118 | + attr={name:testSuiteId,tests:0,failures:0,skipped:0,errors:0,time:0}; |
| 119 | + testSuite=[{_attr:attr}]; |
| 120 | + result.push({testsuite:testSuite}); |
| 121 | + } |
| 122 | + attr.time+=testCase.result.duration; |
| 123 | + attr.tests+=1; |
| 124 | + let testCaseTag=[ |
| 125 | + { |
| 126 | + _attr:{ |
| 127 | + classname:testCaseId, |
| 128 | + name:pickle.name, |
| 129 | + time:((testCase.result.duration ||0)/1000).toFixed(3) |
| 130 | + } |
| 131 | + } |
| 132 | + ]; |
| 133 | + |
| 134 | + if (pickle.tags.length && options.propertiesInTestcase) { |
| 135 | + testCaseTag.push({properties:pickle.tags.map(tag=>createProperty("tag",tag.name))}); |
| 136 | + } |
| 137 | + |
| 138 | + testCase.steps.every((step,index)=>{ |
| 139 | + const {gherkinKeyword, pickleStep } = options.eventDataCollector.getTestStepData({testCase:testCase,index:index}); |
| 140 | + if (gherkinKeyword || (step.result && step.result.status==='failed')) { |
| 141 | + let result=step.result || {}; |
| 142 | + switch (result.status) { |
| 143 | + case 'passed': |
| 144 | + break; |
| 145 | + case 'failed': |
| 146 | + testCaseTag.push(createFailureOrError(gherkinKeyword?"failure":"error",result.exception)); |
| 147 | + attr[gherkinKeyword?"failures":"errors"]+=1; |
| 148 | + return false; |
| 149 | + case 'pending': |
| 150 | + case 'undefined': |
| 151 | + testCaseTag.push(createFailure(result.status === 'pending' ? 'Pending' |
| 152 | + : `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 });` |
| 153 | + )); |
| 154 | + attr.failures+=1; |
| 155 | + return false; |
| 156 | + case 'skipped': |
| 157 | + testCaseTag.push({skipped: []}); |
| 158 | + attr.skipped+=1; |
| 159 | + return false; |
| 160 | + case 'ambiguous': |
| 161 | + testCaseTag.push(createFailureOrError("error",result.exception)); |
| 162 | + attr.errors+=1; |
| 163 | + return false; |
| 164 | + default: |
| 165 | + break; |
| 166 | + // testCase.push(createFailure(`Unknown status - ${step.result.status}`)); |
| 167 | + } |
| 168 | + } |
| 169 | + return true; |
| 170 | + }); |
| 171 | + testSuite.push({testcase:testCaseTag}); |
| 172 | + }; |
| 173 | + } |
| 174 | + |
| 175 | +}; |
0 commit comments