在多模块相互跳转的场景下,只需要取最后退出的模块下生成的覆盖率数据json文件,但特殊场景下如多模块无跳转关系,则需要取每个独立模块下生成的覆盖率数据json文件。
DevEco Studio支持黑盒覆盖率测试,不需要开发测试用例,将编译插桩的HAP包推到设备上,然后对该应用模拟用户操作,退出应用后即可生成覆盖率报告,当前仅支持Stage模型。
hvigorw --mode module -p module={moduleName@productName} -p product={productName} -p buildMode=test -p ohos-test-coverage=true -p coverage-mode=black assembleHap --parallel --incremental --daemon
hdc uninstall {bundleName}
hdc install {SignedHapPath}
hdc file recv data/app/el2/100/base/{bundleName}/haps/{moduleName}/cache {LocalPath}
在多模块相互跳转的场景下,只需要取最后退出的模块下生成的覆盖率数据json文件,但特殊场景下如多模块无跳转关系,则需要取每个独立模块下生成的覆盖率数据json文件。
hvigorw collectCoverage -p projectPath={projectPath} -p reportPath={reportPath} -p coverageFile={projectPath}/{moduleName}/.test/default/intermediates/ohosTest/init_coverage.json#{黑盒覆盖率文件保存路径}
在多模块相互跳转的场景下,需要取各模块的init_coverage.json文件路径,与黑盒覆盖率文件保存路径通过#拼接生成coverageFile参数。
测试覆盖率报告有四个测量维度,分别是:
以下是关于四个测量维度的细节说明:
常见的流程控制语句有if、while、do...while、switch、for等等,以及三目运算符(condition ? exprIfTrue : exprIfFalse),需要确保流程控制的每个边界情况(即分支)都被执行。
function doTheThing () // +0 { // +0 const num = 1; // +1 console.log(num); // +1 } // +0
示例如下:
import { window } from '@kit.ArkUI'; // +0 import导入 const path = 'path'; let filePath :string; // +0 const fileName = 'a.txt'; // +1 不仅是声明,还有赋值 export function doTheThing () // +0 { // +0 const str = 'aaa'; // +1 console.log(str); // +1 } class Person { // +0 name: string = '' // +1 constructor (n:string) { // +0 this.name = n; // +1 } // +0 static sayHello () { // +0 console.log('hello'); // +1 } // +0 walk () {} // +0 } let person = new Person("zhangsan"); Person.sayHello(); person.walk(); 'use strict'; for // +1 ( // +0 let i=0; // +1 i < 10; // +0 i++ // +0 ) // +0 { // +0 } // +0 function func ():object { // +0 return Object({ // +1 一个语句被拆分为多行 a: 1, // +0 b: 2, // +0 }) // +0 } // +0 func(); function foo(n:number, m:number){} // +0 function bar():number{ // +0 return 1; // +1 } foo(1, bar()); // +1 foo(1, // +1 嵌套语句横跨多行 可执行行的数目仅+1 bar()); // +0
一般情况下,如果我们遵守良好的代码规范,可执行代码行和语句的表现是一致的。然而当我们将两个语句放一行时,就会得到不同的结果。如下所示,第一段代码是2 lines、2 statements,第二段代码是1 line、2 statements。
// 2 lines、2 statements const x = 1; console.log(x); // 1 line、2 statements const x = 1; console.log(x);
代码中的某些分支可能很难、甚至无法测试,Deveco Studio提供了instrument ignore * 语法来进行忽略,使得某些代码不计入覆盖率。
使用时需先清除缓存,点击菜单栏Build -> Clean Project。
import {testA} from './Index' // instrument ignore file 忽略整个文件 // instrument ignore next 忽略代码块 export function sum(a:number,b:number){ return a+b; } sum(1,2); let a = 1; // instrument ignore else 忽略else分支 if (a!=1) { // do something console.log('BBB'); }else { console.log('AAA'); } // instrument ignore if 忽略if分支 if (a==1) { // do something console.log('BBB'); }else { console.log('AAA'); }