鸿蒙开发进阶(OpenHarmony)应用自动化测试开发原理

  鸿蒙NEXT开发实战往期必看文章:

一分钟了解”纯血版!鸿蒙HarmonyOS Next应用开发!

“非常详细的” 鸿蒙HarmonyOS Next应用开发学习路线!(从零基础入门到精通)

HarmonyOS NEXT应用开发案例实践总结合(持续更新......)

HarmonyOS NEXT应用开发性能优化实践总结(持续更新......)


背景

本章的主要内容为介绍OpenHarmony的应用自动化测试怎么进行,包括如何写,如何测,如何检视覆盖率和高级命令

新建工程
  1. 新建工程自带的UT:使用步骤就是点开Ability.test.ets然后点击abilityTest下的那个绿色箭头就可以看到下面的输出,默认工程会建立一个用例,所以执行结果就是Tests passed: 1

#星计划#OpenHarmony4.0的应用自动化测试开发原理-鸿蒙开发者社区

  1. 运行日志可通过hilog查看

#星计划#OpenHarmony4.0的应用自动化测试开发原理-鸿蒙开发者社区

  1. 命令行执行

    aa test -b com.example.myapplication -m entry_test -s unittest /ets/testrunner/OpenHarmonyTestRunner -s class ActsAbilityTest -s timeout 15000
    
    OHOS_REPORT_SUM: 1
    OHOS_REPORT_STATUS: class=ActsAbilityTest
    
    
    OHOS_REPORT_STATUS: class=ActsAbilityTest
    OHOS_REPORT_STATUS: current=1
    OHOS_REPORT_STATUS: id=JS
    OHOS_REPORT_STATUS: numtests=1
    OHOS_REPORT_STATUS: stream=
    OHOS_REPORT_STATUS: test=assertContain
    OHOS_REPORT_STATUS_CODE: 1
    
    
    OHOS_REPORT_STATUS: class=ActsAbilityTest
    OHOS_REPORT_STATUS: current=1
    OHOS_REPORT_STATUS: id=JS
    OHOS_REPORT_STATUS: numtests=1
    OHOS_REPORT_STATUS: stream=
    OHOS_REPORT_STATUS: test=assertContain
    OHOS_REPORT_STATUS_CODE: 0
    OHOS_REPORT_STATUS: consuming=6
    
    
    OHOS_REPORT_STATUS: class=ActsAbilityTest
    OHOS_REPORT_STATUS: suiteconsuming=6
    
    
    OHOS_REPORT_RESULT: stream=Tests run: 1, Failure: 0, Error: 0, Pass: 1, Ignore: 0
    OHOS_REPORT_CODE: 0
    OHOS_REPORT_STATUS: taskconsuming=6
    
    TestFinished-ResultCode: 0
    TestFinished-ResultMsg: your test finished!!!
    user test finished.
    

以上内容就是入门级使用说明,后面讲点理论


ArtTS单元测试框架

OpenHarmony自动化测试框架代码部件仓arkXtest,包含单元测试框架(JsUnit)和Ui测试框架(UiTest)。单元测试框架(JsUnit)提供单元测试用例执行能力,提供用例编写基础接口,生成对应报告,用于测试系统或应用接口。Ui测试框架(UiTest)通过简洁易用的API提供查找和操作界面控件能力,支持用户开发基于界面操作的自动化测试脚本。


目录
arkXtest 
  |-----jsunit  单元测试框架
  |-----uitest  Ui测试框架
单元测试框架功能特性(jsunit)
No. 特性 功能说明
1 基础流程 支持编写及异步执行基础用例。
2 断言库 判断用例实际期望值与预期值是否相符。
3 Mock能力 支持函数级mock能力,对定义的函数进行mock后修改函数的行为,使其返回指定的值或者执行某种动作。
4 数据驱动 提供数据驱动能力,支持复用同一个测试脚本,使用不同输入数据驱动执行。
5 专项能力 支持测试套与用例筛选、随机执行、压力测试、超时设置、遇错即停模式等。
使用说明
  1. 基础流程(jsunit)

    测试用例采用业内通用语法,describe代表一个测试套, it代表一条用例。

    No. API 功能说明
    1 describe 定义一个测试套,支持两个参数:测试套名称和测试套函数。
    2 beforeAll 在测试套内定义一个预置条件,在所有测试用例开始前执行且仅执行一次,支持一个参数:预置动作函数。
    3 beforeEach 在测试套内定义一个单元预置条件,在每条测试用例开始前执行,执行次数与it定义的测试用例数一致,支持一个参数:预置动作函数。
    4 afterEach 在测试套内定义一个单元清理条件,在每条测试用例结束后执行,执行次数与it定义的测试用例数一致,支持一个参数:清理动作函数。
    5 afterAll 在测试套内定义一个清理条件,在所有测试用例结束后执行且仅执行一次,支持一个参数:清理动作函数。
    6 beforeItSpecified 在测试套内定义一个单元预置条件,仅在指定测试用例开始前执行,支持两个参数:单个用例名称或用例名称数组、预置动作函数。
    7 afterItSpecified 在测试套内定义一个单元清理条件,仅在指定测试用例结束后执行,支持两个参数:单个用例名称或用例名称数组、清理动作函数
    8 it 定义一条测试用例,支持三个参数:用例名称,过滤参数和用例函数。
    9 expect 支持bool类型判断等多种断言方法。
    10 getDescribeName 获取当前正在执行测试套的名称
    11 getItName 获取当前正在执行测试用例的名称
    12 getItAttribute 获取当前正在执行测试用例的级别、粒度、测试类型
  2. 示例代码(jsunit)

    以下就是默认工程里的测试用例:describe就是定义一个测试套;it就是一个测试用例;

    什么是测试套?:简单理解就是一组测试用例我们可以定为测试套,比如我要测一个页面的功能,播放,暂停,等等,就可以定义为一个测试套。当然测试套可以可以理解是个大范围,比如完整的一系列功能,比如,所有的arkui的控件测试套这种;

    什么是测试用例?:可以理解为一个测试项目,比如说我点了“保存”按钮,会存手机名称这样;再简单点就是点了spinbutton,就会选中;很多测试用例组成一个测试套;

    import hilog from '@ohos.hilog';
    import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
    
    export default function abilityTest() {
      describe('ActsAbilityTest', () => {
        // Defines a test suite. Two parameters are supported: test suite name and test suite function.
        beforeAll(() => {
          // Presets an action, which is performed only once before all test cases of the test suite start.
          // This API supports only one parameter: preset action function.
        })
        beforeEach(() => {
          // Presets an action, which is performed before each unit test case starts.
          // The number of execution times is the same as the number of test cases defined by **it**.
          // This API supports only one parameter: preset action function.
        })
        afterEach(() => {
          // Presets a clear action, which is performed after each unit test case ends.
          // The number of execution times is the same as the number of test cases defined by **it**.
          // This API supports only one parameter: clear action function.
        })
        afterAll(() => {
          // Presets a clear action, which is performed after all test cases of the test suite end.
          // This API supports only one parameter: clear action function.
        })
        it('assertContain', 0, () => {
          // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
          hilog.info(0x0000, 'testTag', '%{public}s', 'it begin');
          let a = 'abc';
          let b = 'b';
          // Defines a variety of assertion methods, which are used to declare expected boolean conditions.
          expect(a).assertContain(b);
          expect(a).assertEqual(a);
        })
      })
    }
    
  3. 断言

    断言功能列表:

    No. API 功能说明
    1 assertClose 检验actualvalue和expectvalue(0)的接近程度是否是expectValue(1)。
    2 assertContain 检验actualvalue中是否包含expectvalue。
    3 assertEqual 检验actualvalue是否等于expectvalue[0]。
    4 assertFail 抛出一个错误。
    5 assertFalse 检验actualvalue是否是false。
    6 assertTrue 检验actualvalue是否是true。
    7 assertInstanceOf 检验actualvalue是否是expectvalue类型,支持基础类型。
    8 assertLarger 检验actualvalue是否大于expectvalue。
    9 assertLess 检验actualvalue是否小于expectvalue。
    10 assertNull 检验actualvalue是否是null。
    11 assertThrowError 检验actualvalue抛出Error内容是否是expectValue。
    12 assertUndefined 检验actualvalue是否是undefined。
    13 assertNaN @since1.0.4 检验actualvalue是否是一个NAN
    14 assertNegUnlimited @since1.0.4 检验actualvalue是否等于Number.NEGATIVE_INFINITY
    15 assertPosUnlimited @since1.0.4 检验actualvalue是否等于Number.POSITIVE_INFINITY
    16 assertDeepEquals @since1.0.4 检验actualvalue和expectvalue是否完全相等
    17 assertPromiseIsPending @since1.0.4 判断promise是否处于Pending状态。
    18 assertPromiseIsRejected @since1.0.4 判断promise是否处于Rejected状态。
    19 assertPromiseIsRejectedWith @since1.0.4 判断promise是否处于Rejected状态,并且比较执行的结果值。
    20 assertPromiseIsRejectedWithError @since1.0.4 判断promise是否处于Rejected状态并有异常,同时比较异常的类型和message值。
    21 assertPromiseIsResolved @since1.0.4 判断promise是否处于Resolved状态。
    22 assertPromiseIsResolvedWith @since1.0.4 判断promise是否处于Resolved状态,并且比较执行的结果值。
    23 not @since1.0.4 断言取反,支持上面所有的断言功能

    以上都是jsunit的断言


单元测试框架功能特性(uitest)
No. 特性 功能说明
1 Driver Ui测试的入口,提供查找控件,检查控件存在性以及注入按键能力。
2 On 用于描述目标控件特征(文本、id、类型等),Driver根据On描述的控件特征信息来查找控件。
3 Component Driver查找返回的控件对象,提供查询控件属性,滑动查找等触控和检视能力。
4 UiWindow Driver查找返回的窗口对象,提供获取窗口属性、操作窗口的能力。
使用说明
  1. 引入uitest框架

    import {Driver,ON,Component,Uiwindow,MatchPattern} from '@ohos.UiTest'
    
  2. 引入ability

    import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
    
  3. 添加启动应用准备

    beforeAll(async (done: Function) => {
      // Presets an action, which is performed only once before all test cases of the test suite start.
      // This API supports only one parameter: preset action function.
      let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
      try {
        await abilityDelegator.startAbility({
          bundleName: 'com.example.myapplication',
          abilityName: 'EntryAbility'
        });
      } catch (exception) {
        console.info(TAG, `StartAbility_001 exception = ${JSON.stringify(exception)}`);
        expect().assertFail();
      }
      done();
    })
    
  4. 添加测试用例

    it('StartAbility_001', 0, async (done: Function) => 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值