diff --git a/zh-cn/application-dev/application-test/arkxtest-guidelines.md b/zh-cn/application-dev/application-test/arkxtest-guidelines.md index 7239e6ec7ad07f588cba75c6a6ec8a5dc403180b..f79e8b5cce3c66e0c16b140c64c16326d76aade3 100644 --- a/zh-cn/application-dev/application-test/arkxtest-guidelines.md +++ b/zh-cn/application-dev/application-test/arkxtest-guidelines.md @@ -23,8 +23,8 @@ OpenHarmony自动化测试框架arkxtest,作为OpenHarmony工具集的重要 ![](figures/UnitTest.PNG) - 单元测试脚本的基础运行流程如下图所示,依赖aa test命令作为执行入口,该命令可具体参考。 - + 单元测试脚本的基础运行流程如下图所示,依赖aa test命令作为执行入口。 + ![](figures/TestFlow.PNG) - UI测试框架 @@ -58,13 +58,13 @@ DevEco Studio可参考其官网介绍进行[下载](https://developer.harmonyos. ## 新建测试脚本 1. 在DevEco Studio中新建应用开发工程,其中ohos目录即为测试脚本所在的目录。 -2. 在工程目录下打开待测试模块下的ets文件,将光标置于代码中任意位置,单击**右键 > Show Context Actions** **> Create Ohos Test**或快捷键**Alt+enter** **> Create Ohos Test**创建测试类,更多指导请参考DevEco Studio中[指导](https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ohos-openharmony-test-framework-0000001267284568)。 +2. 在工程目录下打开待测试模块下的ets文件,将光标置于代码中任意位置,单击**右键 > Show Context Actions** **> Create Ohos Test**或快捷键**Alt+enter** **> Create Ohos Test**创建测试类,更多指导请参考DevEco Studio中[指导](https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ohos-openharmony-test-framework-0000001263160453)。 ## 编写单元测试脚本 ```TS -import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium' -import abilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry' +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; +import abilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; const delegator = abilityDelegatorRegistry.getAbilityDelegator() export default function abilityTest() { @@ -72,7 +72,7 @@ export default function abilityTest() { it('testUiExample',0, async function (done) { console.info("uitest: TestUiExample begin"); //start tested ability - await delegator.executeShellCommand('aa start -b com.ohos.uitest -a MainAbility').then(result =>{ + await delegator.executeShellCommand('aa start -b com.ohos.uitest -a EntryAbility').then(result =>{ console.info('Uitest, start ability finished:' + result) }).catch(err => { console.info('Uitest, start ability failed: ' + err) @@ -81,7 +81,7 @@ export default function abilityTest() { //check top display ability await delegator.getCurrentTopAbility().then((Ability)=>{ console.info("get top ability"); - expect(Ability.context.abilityInfo.name).assertEqual('MainAbility'); + expect(Ability.context.abilityInfo.name).assertEqual('EntryAbility'); }) done(); }) @@ -119,7 +119,7 @@ export default function abilityTest() { it('testUiExample',0, async function (done) { console.info("uitest: TestUiExample begin"); //start tested ability - await delegator.executeShellCommand('aa start -b com.ohos.uitest -a MainAbility').then(result =>{ + await delegator.executeShellCommand('aa start -b com.ohos.uitest -a EntryAbility').then(result =>{ console.info('Uitest, start ability finished:' + result) }).catch(err => { console.info('Uitest, start ability failed: ' + err) @@ -128,7 +128,7 @@ export default function abilityTest() { //check top display ability await delegator.getCurrentTopAbility().then((Ability)=>{ console.info("get top ability"); - expect(Ability.context.abilityInfo.name).assertEqual('MainAbility'); + expect(Ability.context.abilityInfo.name).assertEqual('EntryAbility'); }) //ui test code //init uidriver @@ -154,7 +154,9 @@ export default function abilityTest() { ## 执行测试脚本 -执行测试脚本可以直接在DevEco Studio中通过点击按钮执行,当前支持以下执行方式: +### DevEco Studio执行 + +通过点击按钮执行,当前支持以下执行方式: 1、测试包级别执行即执行测试包内的全部用例。 @@ -164,12 +166,168 @@ export default function abilityTest() { ![](figures/Execute.PNG) -## 查看测试结果 +**查看测试结果** 测试执行完毕后可直接在DevEco Studio中查看测试结果,如下图示例所示: ![](figures/TestResult.PNG) +### CMD执行 + +通过在cmd窗口中输入aa命令执行触发用例执行,并通过设置执行参数触发不同功能。 + +**aa test命令执行配置参数** + +| 执行参数全写 | 执行参数缩写 | 执行参数含义 | 执行参数示例 | +| ------------- | ------------ | -------------------------------------- | ---------------------------------- | +| --bundleName | -b | 应用Bundle名称 | - b com.test.example | +| --packageName | -p | 应用模块名,适用于FA模型应用 | - p com.test.example.entry | +| --moduleName | -m | 应用模块名,适用于STAGE模型应用 | -m entry | +| NA | -s | 特定参数,以键值对方式传入 | - s unittest OpenHarmonyTestRunner | + +框架当前支持多种用例执行方式,通过上表中的-s参数后的配置键值对参数传入触发,如下表所示。 + +| 配置参数值 | 配置参数含义 | 配置参数有值 | 配置参数示例 | +| ------------ | ----------------------------------------------------------------------------- | ------------------------------------------------------------ | ----------------------------------------- | +| unittest | 用例执行所使用OpenHarmonyTestRunner对象 | OpenHarmonyTestRunner或用户自定义runner名称 | - s unittest OpenHarmonyTestRunner | +| class | 指定要执行的测试套或测试用例 | {describeName}#{itName},{describeName} | -s class attributeTest#testAttributeIt | +| notClass | 指定不需要执行的测试套或测试用例 | {describeName}#{itName},{describeName} | -s notClass attributeTest#testAttributeIt | +| itName | 指定要执行的测试用例 | {itName} | -s itName testAttributeIt | +| timeout | 测试用例执行的超时时间 | 正整数(单位ms),如不设置默认为 5000 | -s timeout 15000 | +| breakOnError | 遇错即停模式,当执行用例断言失败或者发生错误时,退出测试执行流程 | true/false(默认值) | -s breakOnError true | +| testType | 指定要执行用例的用例类型 | function,performance,power,reliability, security,global,compatibility,user,standard,safety,resilience' | -s testType function | +| level | 指定要执行用例的用例级别 | 0,1,2,3,4 | -s level 0 | +| size | 指定要执行用例的用例规模 | small,medium,large | -s size small +| stress | 指定要执行用例的执行次数 | 正整数 | -s stress 1000 | + +**通过在cmd窗口直接执行命令。** + +> 使用cmd的方式,需要配置好hdc相关的环境变量 + +- 打开cmd窗口 +- 执行 aa test 命令 + +**示例代码1**:执行所有测试用例。 + +```shell + hdc shell aa test -b xxx -p xxx -s unittest OpenHarmonyTestRunner +``` + +**示例代码2**:执行指定的describe测试套用例,指定多个需用逗号隔开。 + +```shell + hdc shell aa test -b xxx -p xxx -s unittest OpenHarmonyTestRunner -s class s1,s2 +``` + +**示例代码3**:执行指定测试套中指定的用例,指定多个需用逗号隔开。 + +```shell + hdc shell aa test -b xxx -p xxx -s unittest OpenHarmonyTestRunner -s class testStop#stop_1,testStop1#stop_0 +``` + +**示例代码4**:执行指定除配置以外的所有的用例,设置不执行多个测试套需用逗号隔开。 + +```shell + hdc shell aa test -b xxx -p xxx -s unittest OpenHarmonyTestRunner -s notClass testStop +``` + +**示例代码5**:执行指定it名称的所有用例,指定多个需用逗号隔开。 + +```shell + hdc shell aa test -b xxx -p xxx -s unittest OpenHarmonyTestRunner -s itName stop_0 +``` + +**示例代码6**:用例执行超时时长配置。 + +```shell + hdc shell aa test -b xxx -p xxx -s unittest OpenHarmonyTestRunner -s timeout 15000 +``` + +**示例代码7**:用例以breakOnError模式执行用例。 + +```shell + hdc shell aa test -b xxx -p xxx -s unittest OpenHarmonyTestRunner -s breakOnError true +``` + +**示例代码8**:执行测试类型匹配的测试用例。 + +```shell + hdc shell aa test -b xxx -p xxx -s unittest OpenHarmonyTestRunner -s testType function +``` + +**示例代码9**:执行测试级别匹配的测试用例。 + +```shell + hdc shell aa test -b xxx -p xxx -s unittest OpenHarmonyTestRunner -s level 0 +``` + +**示例代码10**:执行测试规模匹配的测试用例。 + +```shell + hdc shell aa test -b xxx -p xxx -s unittest OpenHarmonyTestRunner -s size small +``` + +**示例代码11**:执行测试用例指定次数。 + +```shell + hdc shell aa test -b xxx -p xxx -s unittest OpenHarmonyTestRunner -s stress 1000 +``` + +**查看测试结果** + +- cmd模式执行过程,会打印如下相关日志信息。 + +``` +OHOS_REPORT_STATUS: class=testStop +OHOS_REPORT_STATUS: current=1 +OHOS_REPORT_STATUS: id=JS +OHOS_REPORT_STATUS: numtests=447 +OHOS_REPORT_STATUS: stream= +OHOS_REPORT_STATUS: test=stop_0 +OHOS_REPORT_STATUS_CODE: 1 + +OHOS_REPORT_STATUS: class=testStop +OHOS_REPORT_STATUS: current=1 +OHOS_REPORT_STATUS: id=JS +OHOS_REPORT_STATUS: numtests=447 +OHOS_REPORT_STATUS: stream= +OHOS_REPORT_STATUS: test=stop_0 +OHOS_REPORT_STATUS_CODE: 0 +OHOS_REPORT_STATUS: consuming=4 +``` + +| 日志输出字段 | 日志输出字段含义 | +| ------- | -------------------------| +| OHOS_REPORT_SUM | 当前测试套用例总数 | +| OHOS_REPORT_STATUS: class | 当前执行用例测试套名称| +| OHOS_REPORT_STATUS: id | 用例执行语言,默认JS | +| OHOS_REPORT_STATUS: numtests | 测试包中测试用例总数 | +| OHOS_REPORT_STATUS: stream | 当前用例发生错误时,记录错误信息 | +| OHOS_REPORT_STATUS: test| 当前用例执行的it name | +| OHOS_REPORT_STATUS_CODE | 当前用例执行结果状态 0 (pass) 1(error) 2(fail) | +| OHOS_REPORT_STATUS: consuming | 当前用例执行消耗的时长 | + +- cmd执行完成后,会打印如下相关日志信息。 + +``` +OHOS_REPORT_RESULT: stream=Tests run: 447, Failure: 0, Error: 1, Pass: 201, Ignore: 245 +OHOS_REPORT_CODE: 0 + +OHOS_REPORT_RESULT: breakOnError model, Stopping whole test suite if one specific test case failed or error +OHOS_REPORT_STATUS: taskconsuming=16029 + +``` +| 日志输出字段 | 日志输出字段含义 | +| ------------------| -------------------------| +| run | 当前测试包用例总数 | +| Failure | 当前测试失败用例个数 | +| Error | 当前执行用例发生错误用例个数 | +| Pass | 当前执行用例通过用例个数 | +| Ignore | 当前未执行用例个数 | +| taskconsuming| 执行当前测试用例总耗时 | + +> 当处于breakOnError模式,用例发生错误时,注意查看Ignore以及中断说明 + ## 常见问题 ### 单元测试用例常见问题 @@ -180,11 +338,11 @@ export default function abilityTest() { 用例中增加的日志打印信息,没有在用例执行过程中出现,而是在用例执行结束之后才出现。 - **可能原因** +**可能原因** 此类情况只会存在于用例中有调用异步接口的情况,原则上用例中所有的日志信息均在用例执行结束之前打印。 - **解决方法** +**解决方法** 当被调用的异步接口多于一个时,建议将接口调用封装成Promise方式调用。 @@ -214,12 +372,15 @@ export default function abilityTest() { 2.用例调用函数耗时过长,超过用例执行设置的超时时间。 +3.用例调用函数中断言失败,抛出失败异常,导致用例执行一直没有结束,直到超时结束。 + **解决方法** 1.检查用例代码逻辑,确保即使断言失败场景认可走到done函数,保证用例执行结束。 -2.可在IDE中Run/Debug Configurations中修改用例执行超时配置参数,避免用例执行超时。 +2.可在IDE中Run/Debug Configurations中修改用例执行超时配置参数,避免用例执行超时。 +3.检查用例代码逻辑,断言结果,确保断言Pass。 ### UI测试用例常见问题 **1、失败日志有“Get windows failed/GetRootByWindow failed”错误信息** @@ -258,11 +419,11 @@ UI测试用例执行失败,查看hilog日志发现日志中有“uitest-api do 2.避免多进程执行UI测试用例。 -**3、失败日志有“dose not exist on current UI! Check if the UI has changed after you got the widget object”错误信息** +**3、失败日志有“does not exist on current UI! Check if the UI has changed after you got the widget object”错误信息** **问题描述** -UI测试用例执行失败,查看hilog日志发现日志中有“dose not exist on current UI! Check if the UI has changed after you got the widget object”错误信息。 +UI测试用例执行失败,查看hilog日志发现日志中有“does not exist on current UI! Check if the UI has changed after you got the widget object”错误信息。 **可能原因** diff --git a/zh-cn/application-dev/application-test/figures/Execute.PNG b/zh-cn/application-dev/application-test/figures/Execute.PNG index 49155c9b3406ea477e08273818e52fe026a62737..7c9d9be0d39c8b07c32f351622423085a4086584 100644 Binary files a/zh-cn/application-dev/application-test/figures/Execute.PNG and b/zh-cn/application-dev/application-test/figures/Execute.PNG differ diff --git a/zh-cn/application-dev/application-test/figures/TestResult.PNG b/zh-cn/application-dev/application-test/figures/TestResult.PNG index 300266842efab6da7a4f7469ab8c9e890f238b89..c2938e7f8fbd4ec112506db5b9b9ac03a7b42397 100644 Binary files a/zh-cn/application-dev/application-test/figures/TestResult.PNG and b/zh-cn/application-dev/application-test/figures/TestResult.PNG differ