1/* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import SysTestKit from "../kit/SysTestKit"; 17import {collectCoverageData} from '../coverage/coverageCollect'; 18 19class OhReport { 20 constructor(attr) { 21 this.delegator = attr.delegator; 22 this.abilityDelegatorArguments = attr.abilityDelegatorArguments; 23 this.id = 'report'; 24 this.index = 0; 25 this.duration = 0; 26 } 27 28 init(coreContext) { 29 this.coreContext = coreContext; 30 this.suiteService = this.coreContext.getDefaultService('suite'); 31 this.specService = this.coreContext.getDefaultService('spec'); 32 } 33 34 taskStart() { 35 } 36 37 async taskDone() { 38 if (this.abilityDelegatorArguments !== null) { 39 this.taskDoneTime = new Date().getTime(); 40 let summary = this.suiteService.getSummary(); 41 const configService = this.coreContext.getDefaultService('config'); 42 if (configService['coverage'] === 'true') { 43 await collectCoverageData(); 44 } 45 let message = '\n' + 'OHOS_REPORT_RESULT: stream=Tests run: ' + summary.total + ', Failure: ' + summary.failure; 46 message += ', Error: ' + summary.error; 47 message += ', Pass: ' + summary.pass; 48 message += ', Ignore: ' + summary.ignore; 49 message += '\n' + 'OHOS_REPORT_CODE: ' + (summary.failure > 0 ? -1 : 0) + '\n'; 50 let isHasError = summary.failure > 0 || summary.error > 0; 51 let config = this.coreContext.getDefaultService('config'); 52 if (config.isBreakOnError() && isHasError) { 53 // 未执行全部说明 54 message += '\n' + 'OHOS_REPORT_RESULT: breakOnError model, Stopping whole test suite if one specific test case failed or error' + '\n'; 55 } 56 message += 'OHOS_REPORT_STATUS: taskconsuming=' + summary.duration + '\n'; 57 console.info(message); 58 await SysTestKit.print(message); 59 } 60 console.info('report print success'); 61 this.delegator.finishTest('your test finished!!!', 0, () => { }); 62 } 63 64 incorrectFormat() { 65 if (this.coreContext.getDefaultService('config').filterValid.length !== 0) { 66 var value = this.coreContext.getDefaultService('config').filterValid; 67 var message = 'this param ' + value.join(',') + ' is invalid' + '\n'; 68 this.delegator.finishTest(message, 0, () => { 69 }); 70 } 71 } 72 73 async suiteStart() { 74 if (this.abilityDelegatorArguments !== null) { 75 let message = '\n' + 'OHOS_REPORT_SUM: ' + this.suiteService.getCurrentRunningSuite().getSpecsNum(); 76 message += '\n' + 'OHOS_REPORT_STATUS: class=' + this.suiteService.getCurrentRunningSuite().description + '\n'; 77 console.info(message); 78 await SysTestKit.print(message); 79 console.info(this.suiteService.getCurrentRunningSuite().description + ' suiteStart print success'); 80 } 81 } 82 83 async suiteDone() { 84 if (this.abilityDelegatorArguments !== null) { 85 let message = '\n' + 'OHOS_REPORT_STATUS: class=' + this.suiteService.getCurrentRunningSuite().description; 86 message += '\n' + 'OHOS_REPORT_STATUS: suiteconsuming=' + this.suiteService.getCurrentRunningSuite().duration + '\n'; 87 console.info(message); 88 await SysTestKit.print(message); 89 console.info(this.suiteService.getCurrentRunningSuite().description + ' suiteDone print success'); 90 } 91 } 92 93 async specStart() { 94 if (this.abilityDelegatorArguments !== null) { 95 let message = '\n' + 'OHOS_REPORT_STATUS: class=' + this.suiteService.getCurrentRunningSuite().description; 96 message += '\n' + 'OHOS_REPORT_STATUS: current=' + (++this.index); 97 message += '\n' + 'OHOS_REPORT_STATUS: id=JS'; 98 message += '\n' + 'OHOS_REPORT_STATUS: numtests=' + this.specService.getTestTotal(); 99 message += '\n' + 'OHOS_REPORT_STATUS: stream='; 100 message += '\n' + 'OHOS_REPORT_STATUS: test=' + this.specService.currentRunningSpec.description; 101 message += '\n' + 'OHOS_REPORT_STATUS_CODE: 1' + '\n'; 102 console.info(message); 103 await SysTestKit.print(message); 104 console.info(this.specService.currentRunningSpec.description + ' specStart start print success'); 105 } 106 } 107 108 async specDone() { 109 if (this.abilityDelegatorArguments !== null) { 110 let message = '\n' + 'OHOS_REPORT_STATUS: class=' + this.suiteService.getCurrentRunningSuite().description; 111 message += '\n' + 'OHOS_REPORT_STATUS: current=' + (this.index); 112 message += '\n' + 'OHOS_REPORT_STATUS: id=JS'; 113 message += '\n' + 'OHOS_REPORT_STATUS: numtests=' + this.specService.getTestTotal(); 114 let errorMsg = ''; 115 if (this.specService.currentRunningSpec.error) { 116 message += '\n' + 'OHOS_REPORT_STATUS: stack=' + this.specService.currentRunningSpec.error.message; 117 message += '\n' + 'OHOS_REPORT_STATUS: stream='; 118 message += '\n' + 'Error in ' + this.specService.currentRunningSpec.description; 119 message += '\n' + this.specService.currentRunningSpec.error.message; 120 message += '\n' + 'OHOS_REPORT_STATUS: test=' + this.specService.currentRunningSpec.description; 121 message += '\n' + 'OHOS_REPORT_STATUS_CODE: -1' + '\n'; 122 } else if (this.specService.currentRunningSpec.result) { 123 if (this.specService.currentRunningSpec.result.failExpects.length > 0) { 124 this.specService.currentRunningSpec.result.failExpects.forEach(failExpect => { 125 errorMsg = failExpect.message || ('expect ' + failExpect.actualValue + ' ' + failExpect.checkFunc + ' ' + (failExpect.expectValue)); 126 }); 127 message += '\n' + 'OHOS_REPORT_STATUS: stack=' + errorMsg; 128 message += '\n' + 'OHOS_REPORT_STATUS: stream='; 129 message += '\n' + 'Error in ' + this.specService.currentRunningSpec.description; 130 message += '\n' + errorMsg + '\n' + 'OHOS_REPORT_STATUS: test=' + this.specService.currentRunningSpec.description; 131 message += '\n' + 'OHOS_REPORT_STATUS_CODE: -2' + '\n'; 132 } else { 133 message += '\n' + 'OHOS_REPORT_STATUS: stream='; 134 message += '\n' + 'OHOS_REPORT_STATUS: test=' + this.specService.currentRunningSpec.description; 135 message += '\n' + 'OHOS_REPORT_STATUS_CODE: 0' + '\n'; 136 } 137 } else { 138 message += '\n'; 139 } 140 message += 'OHOS_REPORT_STATUS: consuming=' + this.specService.currentRunningSpec.duration + '\n'; 141 console.info(message); 142 await SysTestKit.print(message); 143 console.info(this.specService.currentRunningSpec.description + ' specDone end print success'); 144 } 145 } 146} 147 148export default OhReport; 149