暂无描述
当前项目代码仓暂无内容
XTS子系统
简介
XTS子系统是OpenHarmony生态认证测试套件的集合,当前包括ACTS(application compatibility test suite)应用兼容性测试组件,后续将拓展DCTS(distributed compatibility test suite)分布式兼容性测试组件、HATS(hardware abstract test suite) 硬件抽象层兼容性测试组件。
XTS子系统当前包括ACTS 与Tools 软件包:
- ACTS,存放ACTS 相关测试案例源码与配置文件,其目的是帮助终端设备制造商尽早发现软件与OpenHarmony的不兼容问题,确保软件在整个开发周期内符合OpenHarmony的兼容性标准。
- Tools,存放ACTS 相关测试案例开发框架。
系统类型
OpenHarmony支持如下几种系统类型:
-
轻量系统(mini system)
面向MCU类处理器例如Arm Cortex-M、RISC-V 32位的设备,硬件资源极其有限,支持的设备最小内存为128KiB,可以提供多种轻量级网络协议,轻量级的图形框架,以及丰富的IOT总线读写部件等。可支撑的产品如智能家居领域的连接类模组、传感器设备、穿戴类设备等。
-
小型系统(small system)
面向应用处理器例如Arm Cortex-A的设备,支持的设备最小内存为1MiB,可以提供更高的安全能力、标准的图形框架、视频编解码的多媒体能力。可支撑的产品如智能家居领域的IP Camera、电子猫眼、路由器以及智慧出行领域的行车记录仪等。
-
标准系统(standard system)
面向应用处理器例如Arm Cortex-A的设备,支持的设备最小内存为128MiB,可以提供增强的交互能力、3D GPU以及硬件合成能力、更多控件以及动画效果更丰富的图形能力、完整的应用框架。可支撑的产品如高端的冰箱显示屏。
目录
/test/xts
├── acts # 测试代码存放目录
│ └── subsystem # 标准系统子系统测试用例源码存放目录
│ └── subsystem_lite # 轻量系统、小型系统子系统测试用例源码存放目录
│ └── BUILD.gn # 标准系统测试用例编译配置
│ └── build_lite # 轻量系统、小型系统测试用例编译配置存放目录
│ └── BUILD.gn # 轻量系统、小型系统测试用例编译配置
└── tools # 测试工具代码存放目录
约束
轻量系统用例开发语言是C,小型系统用例开发语言是C++。
使用说明
表 1 用例级别说明
表 2 用例粒度说明
表 3 测试类型说明
用例开发指导
根据测试系统选择测试框架和对应测试用例语言。
表 4 系统和测试框架、开发语言对应关系
C语言用例开发编译指导(适用于轻量系统产品用例开发)
示例:轻量系统测试用例开发
当前使用的测试框架是hctest,hctest测试框架支持使用C语言编写测试用例,是在开源测试框架unity的基础上进行增强和适配。
-
用例目录规范:测试用例存储到test/xts/acts仓中
├── acts │ └──subsystem_lite │ │ └── module_hal │ │ │ └── BUILD.gn │ │ │ └── src │ └──build_lite │ │ └── BUILD.gn -
src目录下用例编写样例。
1.引用测试框架
#include "hctest.h"- 使用宏定义LITE_TEST_SUIT定义子系统、模块、测试套件名称
/** * @brief register a test suit named "IntTestSuite" * @param test subsystem name * @param example module name * @param IntTestSuite test suit name */ LITE_TEST_SUIT(test, example, IntTestSuite);- 定义Setup与TearDown
命名方式:测试套件名称+Setup,测试套件名称+TearDown。
Setup与TearDown必须存在,可以为空函数。
- 使用宏定义LITE_TEST_CASE写测试用例
包括三个参数:测试套件名称,测试用例名称,用例属性(测试类型、用例粒度、用例级别)。
LITE_TEST_CASE(IntTestSuite, TestCase001, Function | MediumTest | Level1) { //do something };- 使用宏定义 RUN_TEST_SUITE注册测试套件
RUN_TEST_SUITE(IntTestSuite); -
测试模块的配置文件(BUILD.gn)样例:
在每个测试模块目录下新建BUILD.gn编译文件,用于指定编译后静态库的名称、依赖的头文件、依赖的库等;具体写法如下:
import("//test/xts/tools/lite/build/suite_lite.gni") hctest_suite("ActsDemoTest") { suite_name = "acts" sources = [ "src/test_demo.c", ] include_dirs = [ ] cflags = [ "-Wno-error" ] } -
acts下BUILD.gn增加编译选项。
需要将测试模块加入到acts目录下的编译脚本中,编译脚本路径:test/xts/acts/build_lite/BUILD.gn。
lite_component("acts") { ... if(board_name == "liteos_m") { features += [ ... "//xts/acts/subsystem_lite/module_hal:ActsDemoTest" ] } } -
测试套件编译命令_两种编译方式。
方式一:
./test/xts/tools/lite/build.sh product=wifiiot xts=acts方式二:
hb set 选择 设备类型 hb build --gn-args build_xts=true (注):若不追加--gn-args build_xts=true,不会编译acts测试套件。
说明:
acts测试套件编译中间件为静态库,最终链接到版本镜像中 。
C语言用例执行指导(适用于轻量系统产品用例开发)
示例:轻量系统测试用例执行
将版本镜像烧录进开发板。
测试步骤
- 使用串口工具登录开发板,并保存串口打印信息。
- 重启设备,查看串口日志。
测试结果分析指导
基于串口打印日志进行分析;
每个测试套件执行以Start to run test suite开始,以xx Tests xx Failures xx Ignored结束。
C++语言用例开发编译指导(适用于小型系统、标准系统用例开发)
示例:小型系统测试用例开发(标准系统参考具体样例目录:global/i18n_standard)
当前使用的测试框架是hcpptest,hcpptest测试框架是在开源的googletest测试框架的基础上进行的增强和适配。
-
规范用例目录:测试用例存储到test/xts/acts仓中。
├── acts │ └──subsystem_lite │ │ └── module_posix │ │ │ └── BUILD.gn │ │ │ └── src │ └──build_lite │ │ └── BUILD.gn -
测试模块src下用例编写样例:
- 引用测试框架:
需要引用gtest.h 如:#include "gtest/gtest.h"
#include "gtest/gtest.h"- 定义Setup与TearDown
using namespace std; using namespace testing::ext; class TestSuite: public testing::Test { protected: // Preset action of the test suite, which is executed before the first test case static void SetUpTestCase(void){ } // Test suite cleanup action, which is executed after the last test case static void TearDownTestCase(void){ } // Preset action of the test case virtual void SetUp() { } // Cleanup action of the test case virtual void TearDown() { } };- 使用宏定义HWTEST或HWTEST_F写测试用例
普通测试用例的定义:HWTEST(测试套名称, 测试用例名称, 用例标注)。
包含SetUp和TearDown的测试用例的定义 :HWTEST_F(测试套名称, 测试用例名称,用例标注)。
宏定义包括三个参数:测试套件名称,测试用例名称,用例属性(测试类型、用例粒度、用例级别)。
HWTEST_F(TestSuite, TestCase_0001, Function | MediumTest | Level1) { // do something } -
测试模块下用例配置文件(BUILD.gn)样例:
每个测试模块目录下新建BUILD.gn编译文件,用于指定编译后可执行文件的名称、依赖的头文件、依赖的库等;具体写法如下。每个测试模块将独立编译成.bin可执行文件, 该文件可直接push到单板上进行测试。
举例:
import("//test/xts/tools/lite/build/suite_lite.gni") hcpptest_suite("ActsDemoTest") { suite_name = "acts" sources = [ "src/TestDemo.cpp" ] include_dirs = [ "src", ... ] deps = [ ... ] cflags = [ "-Wno-error" ] } -
acts目录下增加编译选项(BUILD.gn)样例:
将测试模块加入到acts目录下的编译脚本中,编译脚本为:test/xts/acts/build_lite/BUILD.gn。
lite_component("acts") { ... else if(board_name == "liteos_a") { features += [ ... "//xts/acts/subsystem_lite/module_posix:ActsDemoTest" ] } } -
测试套件编译命令_两种编译方式。
L1_LiteOS:
方式一: python3 build.py -p ipcamera_hispark_taurus@hisilicon --gn-args build_xts=true 方式二: hb set 选择 设备类型 hb build --gn-args build_xts=true (注):若不追加--gn-args build_xts=true,不会编译acts测试套件。L1_Linux:
方式一: python3 build.py -p ipcamera_hispark_taurus_linux@hisilicon --gn-args build_xts=true 方式二: hb set 选择 设备类型 hb build --gn-args build_xts=true (注):若不追加--gn-args build_xts=true,不会编译acts测试套件。
说明:
小型系统acts独立编译成可执行文件(bin格式), 在编译产物的suites\acts目录下归档。
C++语言用例执行指导(适用于小型系统、标准系统用例开发)
示例:小型系统测试用例执行
目前的用例执行采用nfs共享的方式,mount到单板去执行。
环境搭建
-
使用网线或无线网络将开发板与PC进行连接。
-
开发板配置IP、子网掩码、网关,确保开发板与PC处于同一个网段。
-
PC安装nfs服务器并完成注册,启动nfs服务。
-
开发板配置mount命令,确保开发板可以访问PC端的nfs共享文件。
格式:mount [nfs服务器IP]:[/nfs共享目录] [/开发板目录] nfs
举例:
mount 192.168.1.10:/nfs /nfs nfs
用例执行
测试套件执行 ActsDemoTest.bin 触发用例执行,基于串口打印日志进行分析。
JS语言用例开发指导(适用于标准系统)
当前使用的测试框架是HJSUnit,用于支撑OpenHarmony application测试(特指基于JS应用框架使用 Javascript 语言开发的 APP)进行自动化测试。
用例编写基础语法
测试用例为 js 语言,必须满足 JavaScript 语言编程规范:
表 5
用例编写语法采用 jasmine 的标准语法,格式支持ES6格式。
以FA 模式为例:
-
规范用例目录:测试用例存储到 src/main/js/test目录。
├── BUILD.gn ├── Test.json # 资源依赖hap不需要Test.json文件 ├── signature │ └──openharmony_sx.p7b # 签名工具 └──src │ └──main │ │ └──js │ │ │ └──MainAbility │ │ │ │ └──app.js │ │ │ │ └──pages │ │ │ │ │ └──index │ │ │ │ │ │ └──index.js │ │ │ └──test # 测试代码存放目录 │ │ │ │ │ └──List.test.js │ │ │ │ │ └──Ability.test.js │ │ │ └──TestAbility # 测试框架入口模板文件,添加后无需修改 │ │ │ │ └──app.js │ │ │ │ └──pages │ │ │ │ │ └──index │ │ │ │ │ │ └──index.js │ │ │ └──TestRunner # 测试框架入口模板文件,添加后无需修改 │ │ │ │ └──OpenHarmonyTestRunner.js │ └── resources # hap资源存放目录 │ └── config.json # hap配置文件 -
OpenHarmonyTestRunner.js 示例
//加载js 测试框架 import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry' ... export default { ... onRun() { console.log('OpenHarmonyTestRunner onRun run') var abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() var abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() var testAbilityName = abilityDelegatorArguments.parameters['-p'] + '.MainAbility' var cmd = 'aa start -d 0 -a ' + testAbilityName + ' -b ' + abilityDelegatorArguments.bundleName ... } }; -
index.js示例
export default { ... onShow() { console.info('onShow finish!') }, ... } -
app.js示例
//加载测试用例 import { Hypium } from '@ohos/hypium' import testsuite from '../test/List.test' export default { onCreate() { console.info('TestApplication onCreate'); var abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() var abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() console.info('start run testcase!!!') Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite) }, ... }; -
单元测试用例示例
// Example1: 使用HJSUnit进行单元测试 describe('appInfoTest', function () { it('app_info_test_001', 0, function () { var info = app.getInfo() expect(info.versionName).assertEqual('1.0') expect(info.versionCode).assertEqual('3') }) })
FA_JS 模式测试模块下用例配置文件(BUILD.gn)样例:
import("//test/xts/tools/build/suite.gni")
ohos_js_hap_suite("ActsDemoTest") {
hap_profile = "./src/main/config.json"
deps = [
":hjs_demo_js_assets",
":hjs_demo_resources",
]
certificate_profile = "./signature/openharmony_sx.p7b" //签名文件
hap_name = "ActsDemoTest" //测试套件,以Acts开头,以Test结尾,采用驼峰式命名
part_name = "..." //部件
subsystem_name = "..." //子系统
}
ohos_js_assets("hjs_demo_js_assets") {
js2abc = true
hap_profile = "./src/main/config.json"
source_dir = "./src/main/js"
}
ohos_resources("hjs_demo_resources") {
sources = [ "./src/main/resources" ]
hap_profile = "./src/main/config.json"
}
FA_TS 模式测试模块下用例配置文件(BUILD.gn)样例:
import("//test/xts/tools/build/suite.gni")
ohos_js_hap_suite("ActsDemoTest") {
hap_profile = "./src/main/config.json"
deps = [
":ace_demo_ets_assets",
":ace_demo_ets_resources",
":ace_demo_ets_test_assets",
]
ets2abc = true
certificate_profile = "./signature/openharmony_sx.p7b" //签名文件
hap_name = "ActsDemoTest" //测试套件,以Acts开头,以Test结尾,采用驼峰式命名
part_name = "..." //部件
subsystem_name = "..." //子系统
}
ohos_js_assets("ace_demo_ets_assets") {
source_dir = "./src/main/ets/MainAbility"
}
ohos_js_assets("ace_demo_ets_test_assets") {
source_dir = "./src/main/ets/TestAbility"
}
ohos_resources("ace_demo_ets_resources") {
sources = [ "./src/main/resources" ]
hap_profile = "./src/main/config.json"
}
FA_JS 模式适配指导请参考
FA_TS 模式适配指导请参考
以Stage 模式为例:
-
规范用例目录:测试用例存储到 src/main/js/test目录。
├── BUILD.gn # 配置文件 ├── Test.json # 资源依赖hap不需要Test.json文件 ├── signature │ └──openharmony_sx.p7b # 签名工具 ├── AppScope │ └──resource │ └──app.json ├── entry │ └──src │ │ └──main │ │ │ └──ets │ │ │ │ └──test # 测试代码存放目录 │ │ │ │ │ └──List.test.ets │ │ │ │ │ └──Ability.test.ets │ │ │ │ └──MainAbility │ │ │ │ │ └──MainAbility.ts │ │ │ │ │ └──pages │ │ │ │ │ │ └──index │ │ │ │ │ │ │ └──index.ets │ │ │ │ └──TestAbility │ │ │ │ │ └──TestAbility.ts # 测试用例启动入口 ability │ │ │ │ │ └──pages │ │ │ │ │ │ └──index.ets │ │ │ │ └──Application │ │ │ │ │ └──AbilityStage.ts │ │ │ │ └──TestRunner # 测试框架入口模板文件,添加后无需修改 │ │ │ │ │ └──OpenHarmonyTestRunner.js │ │ └── resources # hap资源存放目录 │ │ └── module.json # hap配置文件 -
OpenHarmonyTestRunner.ts 示例
【注】在TestRunner目录下的 OpenHarmonyTestRunner.ts 文件中的 async onRun() 方法下存在拉起测试套入口xxxAbility的cmd 命令:
例如:
var cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName
需与module.json中 "abilities" 下的 "name" 字段保持一致,保证拉起的是我们需要的测试入口。
import TestRunner from '@ohos.application.testRunner' import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry' ... export default class OpenHarmonyTestRunner implements TestRunner { ... async onRun() { console.log('OpenHarmonyTestRunner onRun run') abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() var testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility' let lMonitor = { abilityName: testAbilityName, onAbilityCreate: onAbilityCreateCallback, }; abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback) var cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName ... } }; -
index.ets示例
import router from '@ohos.router'; @Entry @Component struct Index { aboutToAppear(){ console.info("start run testcase!!!!") } build() { ... } } -
app.js示例
//加载测试用例 import Ability from '@ohos.app.ability.UIAbility' import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry' import { Hypium } from '@ohos/hypium' import testsuite from '../test/List.test' export default class TestAbility extends Ability { onCreate(want, launchParam) { console.log('TestAbility onCreate') var abilityDelegator: any abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() var abilityDelegatorArguments: any abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() console.info('start run testcase!!!') Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite) } ... };
Stage 模式测试模块下用例配置文件(BUILD.gn)样例:
import("//test/xts/tools/build/suite.gni")
ohos_js_hap_suite("ActsDemoTest") {
hap_profile = "/src/main/module.json"
js_build_mode = "debug"
deps = [
":edm_js_assets",
":edm_resources",
]
ets2abc = true
certificate_profile = "./signature/openharmony_sx.p7b" //签名文件
hap_name = "ActsDemoTest" //测试套件,以Acts开头,以Test结尾,采用驼峰式命名
subsystem_name = "customization" //子系统
part_name = "enterprise_device_management" //部件
}
ohos_app_scope("edm_app_profile") {
app_profile = "AppScope/app.json"
sources = [ "AppScope/resources" ]
}
ohos_js_assets("edm_js_assets") {
source_dir = "/src/main/ets"
}
ohos_resources("edm_resources") {
sources = [ "/src/main/resources" ]
deps = [ ":edm_app_profile" ]
hap_profile = "/src/main/module.json"
}
Stage 模式适配指导请参考
JS语言用例编译打包指导(适用于标准系统)
hap包编译请参考 标准系统 JS用例源码编译Hap包指导。
JS语言用例开发编译执行指导(适用于小型系统)
当前使用的测试框架是 jsunit_lite,用于支撑小型系统(small system)JS 接口自动化测试。jsunit_lite 适配 JerryScript 引擎能力限制,提供测试用例编写、执行和结果输出能力。
目录结构
新建 JS 测试用例目录结构如下,开发者需将 your_test_suite 替换为实际测试套名称:
test/xts/acts/<your_test_suite>/
├── BUILD.gn # 编译配置(使用 jsunit_lite_suite 模板)
├── Test.json # xdevice 测试配置
├── cert/
│ └── openharmony_sx.p7b # 签名文件
└── entry/src/main/
├── config.json # 应用配置
├── resources/base/media/
│ └── icon.png # 应用图标
└── js/
├── MainAbility/
│ ├── app.js # 应用入口(初始化框架 + 执行测试)
│ ├── manifest.json # 应用 manifest
│ └── pages/index/
│ ├── index.js # UI 页面
│ └── index.hml # UI 模板
└── test/
├── List.test.js # 测试套聚合入口
└── Assert.test.js # 断言用例(示例)
用例编写
1. 编写测试用例文件
// entry/src/main/js/test/MyTest.test.js
import { describe, it, expect, beforeAll, afterAll, Level } from 'jsunit_lite/index';
export default function myTest() {
describe('MyTestSuite', function() {
/**
* @tc.name my_first_test
* @tc.number SUB_XXXX_0001
* @tc.desc Verify assertEqual works
*/
it('my_first_test', Level.LEVEL0, function() {
expect(1).assertEqual(1);
});
/**
* @tc.name my_string_test
* @tc.number SUB_XXXX_0002
* @tc.desc Verify assertContain works
*/
it('my_string_test', Level.LEVEL0, function() {
expect('hello world').assertContain('world');
});
});
}
2. 编写 List.test.js 聚合入口
// entry/src/main/js/test/List.test.js
import { describe, it, expect } from 'jsunit_lite/index';
import myTest from './MyTest.test';
export default function testsuite() {
myTest();
// 如有多个测试文件,在此依次调用
}
3. 编写 app.js 应用入口
// entry/src/main/js/MainAbility/app.js
import Core from 'jsunit_lite/src/core';
import liteReport from 'jsunit_lite/src/module/report/liteReport';
import { describe, it, expect } from 'jsunit_lite/index';
import testsuite from '../test/List.test';
export default {
data: {},
onCreate: function() {
console.info('[MyTest] onCreate start');
var core = Core.getInstance();
var reportInstance = new liteReport({ id: 'default' });
core.addService('report', reportInstance);
core.init();
testsuite();
core.execute();
},
onDestroy: function() {
console.info('[MyTest] onDestroy');
}
};
4. 编写 config.json
以下为小型系统测试应用的 config.json 模板,开发者需根据实际测试套修改 bundleName、package、name、label 等字段:
{
"app": {
"bundleName": "com.ohos.yourtest",
"vendor": "ohos",
"version": {
"code": 1,
"name": "1.0.0"
},
"apiVersion": {
"compatible": 3,
"target": 4
}
},
"deviceConfig": {
"default": {}
},
"module": {
"package": "com.ohos.yourtest",
"name": ".YourTestAbilityPackage",
"deviceType": [
"smartVision"
],
"distro": {
"deliveryWithInstall": true,
"moduleName": "entry",
"moduleType": "entry"
},
"abilities": [
{
"name": "JsunitAbility",
"srcLanguage": "js",
"srcPath": "default",
"icon": "assets/entry/resources/base/media/icon.png",
"description": "Your test ability",
"label": "YourTest",
"type": "page",
"visible": true,
"launchType": "standard"
}
],
"js": [
{
"pages": [
"pages/index/index"
],
"name": "JsunitAbility"
}
]
}
}
编译配置
以下为编译配置模板,开发者需将 hap_name、hap_dir、extra_sources 等替换为实际值:
BUILD.gn
import("//test/xts/tools/lite/build/jsunit_lite_suite.gni")
jsunit_lite_suite("acts_your_test") {
hap_name = "ActsYourTest"
hap_dir = "your_test_suite"
config_path = "entry/src/main/config.json"
test_json_path = "Test.json"
suite_name = "acts"
cert_profile = "cert/openharmony_sx.p7b"
resources_path = "entry/src/main/resources"
js_source_dir = "entry/src/main/js/MainAbility"
extra_sources = [
"entry/src/main/js/MainAbility/pages/index/index.js",
"entry/src/main/js/test/List.test.js",
"entry/src/main/js/test/MyTest.test.js",
]
}
Test.json
以下为 xdevice 测试配置模板,开发者需将 bundle-name、hap-file-name、source 路径等替换为实际值:
{
"description": "Your test suite",
"environment": [
{
"type": "device",
"label": "ipcamera",
"device": {
"label": "ipcamera",
"type": "usb"
}
}
],
"kits": [
{
"type": "MountKit",
"server": "NfsServer",
"type_kernel": "linux",
"mount": [
{
"source": "$testcases/your_test_suite",
"target": "/test_root"
}
]
}
],
"driver": {
"type": "JSUnitTestLite",
"ability-name": "JsunitAbility",
"bundle-name": "com.ohos.yourtest",
"hap-file-name": "ActsYourTest.hap",
"timeout": "600000",
"cleanup": "true",
"reboot-after-cleanup": "true"
}
}
编译命令
在 OpenHarmony 源码根目录执行:
# Linux 内核设备(如 HiSpark Taurus Linux 版)
./build.sh --product-name ipcamera_hispark_taurus_linux --build-xts=true
# LiteOS 内核设备
./build.sh --product-name ipcamera_hispark_taurus --build-xts=true
编译产物路径:
out/hispark_taurus/ipcamera_hispark_taurus_linux/suites/acts/testcases/<your_test_suite>/<HapName>.hap
app.js 体积限制:编译后的 app.js(webpack 打包后)不能超过 48KB,否则设备加载失败。如遇超限,需精简测试代码或减少导入的框架模块。
执行测试(xdevice)
通过 xdevice 配置 Test.json 实现全自动化执行。执行前需配置 config/user_config.xml:
<!-- config/user_config.xml 示例 -->
<user_config>
<environment>
<device type="com" label="ipcamera">
<serial>
<com>com4</com> <!-- 修改:串口号 -->
<type>cmd</type>
<baud_rate>115200</baud_rate>
</serial>
</device>
</environment>
<testcases>
<server label="NfsServer">
<ip>192.168.1.100</ip> <!-- 修改:NFS 服务器 IP -->
<dir>D:\test</dir> <!-- 修改:NFS 共享目录 -->
<remote>false</remote> <!-- true=远程 SFTP,false=本地目录 -->
</server>
</testcases>
</user_config>
| 字段 | 说明 |
|---|---|
com |
设备串口号(Windows: com4,Linux: /dev/ttyUSB0) |
ip |
NFS 服务器 IP,需与设备同网段 |
dir |
NFS 共享目录,NFS 挂载到设备 /test_root |
-alldirs |
NFS exports 配置需添加 -alldirs 选项,支持按设备 serial 挂载子目录(多设备隔离) |
remote |
false 时用本地 shutil.copy,true 时用 SFTP |
配置完成后执行:
xdevice run test/xts/acts/<your_test_suite>/Test.json
xdevice 自动完成:NFS 挂载 → hap 推送 → 安装 → 启动测试 → 串口日志采集 → 结果解析 → 报告生成。
结果解析
测试结果通过串口输出,JSUnitParserLite 解析以下标记:
| 标记 | 含义 |
|---|---|
[start] start run suites |
测试任务开始 |
[suite start] xxx |
测试套开始 |
[start] xxx |
单条用例开始 |
[pass] xxx |
用例通过 |
[fail] xxx |
用例失败(后跟错误堆栈) |
[ignore] xxx |
用例跳过(SkipError 或 xit) |
[suite end] |
测试套结束 |
[end] run suites end |
测试任务结束 |
xdevice 生成的报告位于 reports/ 目录,包含 JUnit XML + HTML 格式。
小型系统限制注意事项
| 限制 | 影响 | 建议 |
|---|---|---|
| 不支持 async/await | 不能用 async function |
使用同步函数 |
| Promise 微任务不可靠 | .then() 回调可能不执行 |
避免依赖 Promise 链 |
| app.js ≤ 48KB | 框架代码+用例代码打包后不能超限 | 精简用例,避免导入不必要的模块 |
| 无 hdc | 不能用 hdc shell/hdc file send |
使用串口(pyserial)或 NFS |
| 无 globalThis | 不能用 globalThis 全局变量 |
框架已通过 Core 实例属性注入 |
Python用例开发编译指导
XTS当前支持执行Python测试用例,可用于系统黑盒测试,并提供了对设备进行操作的接口,包括执行Shell命令、文件传输、应用安装卸载等。
用例开发指导
Python用例开发有两种模式,分别是测试用例模式和测试套模式。
- 测试用例模式
测试用例由一个测试用例json配置文件和一个Python测试脚本py文件组成。
用例目录示例:
├── BUILD.gn # 编译配置文件
├── TestCase.json # 测试用例配置文件
├── testcase1.py # 测试用例Python脚本
json配置文件描述测试用例的配置信息,如环境设备信息、测试驱动信息、测试所需kit等。其中需指定测试用例执行驱动类型为 'DeviceTest',并指定Python测试脚本路径(测试用例模式仅可指定一个py文件)。测试用例配置文件示例:
// TestCase.json
{
"description": "Configuration for Openharmony device test case",
// environment 字段描述测试套执行所需环境信息,如设备数量、类型等
"environment": [
{
"type": "device", // device 表示 OpenHarmony 设备
"label": "phone"
}
],
// driver 字段描述测试用例执行所需驱动、测试用例文件路径等
"driver": {
"type": "DeviceTest",
"py_file": ["testcase1.py"]
},
// kits 字段描述测试用例需要的测试公共kit,如 pushkit 、 shellkit 等
"kits": [
]
}
Python测试用例继承'TestCase',需重写三个生命周期函数。测试用例Python脚本示例如下:
| 生命周期函数 | 说明 |
|---|---|
| setup | 测试用例前置步骤,用于执行测试用例的预置动作 |
| process | 测试用例实际操作步骤,用于执行测试用例实际测试步骤 |
| teardown | 测试用例清理步骤,用于执行测试用例的环境清理等操作 |
// testcase1.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from devicetest.core.test_case import TestCase, Step
class testcase1(TestCase):
def __init__(self, controllers):
self.TAG = self.__class__.__name__
super().__init__(self.TAG, controllers)
def setup(self):
Step("Setup")
def process(self):
Step("Process")
# driver1 表示 TestCase.json 中定义的环境设备
res = self.device1.execute_shell_command("ls -l")
# 使用该方式打印日志,可将输出保留在测试报告中
self.log.info(res)
# 使用断言判断测试通过与否
assert res != ""
def teardown(self):
Step("Teardown")
- 测试套模式
测试套由一个测试套json配置文件、一个测试套py文件、多个Python测试脚本py文件组成。
用例目录示例:
├── BUILD.gn # 编译配置文件
├── TestSuite.json # 测试套json配置文件
├── TestSuite.py # 测试套py文件
├── testcase1.py # Python测试脚本py文件
├── testcase2.py
├── testcase3.py
json配置文件中需指定测试套执行驱动类型为 'DeviceTestSuite',并指定测试套py文件和Python测试脚本路径。测试套配置文件示例如下:
// TestSuite.json
{
"description": "Configuration for Openharmony device test suite",
// environment 字段描述测试套执行所需环境信息,如设备类型等
"environment": [
{
"type": "device", // device 表示 OpenHarmony 设备
"label": "phone"
}
],
// driver 字段描述测试套执行所需驱动、测试套py文件路径、所包含所有测试脚本路径等
"driver": {
"type": "DeviceTestSuite",
"testsuite": "TestSuite.py",
"suitecases": [
"./testcase1.py",
"./testcase2.py",
"./testcase3.py"
]
},
// kits 字段描述测试套需要的测试公共kit,如 pushkit 、 shellkit 等
"kits": [
]
}
注: Python测试脚本若与测试套py文件在同一目录下且以'TC_'开头,则无需在json配置文件中配置 'suitecases',框架将自动扫描执行所有符合条件的测试用例。(若已配置 'suitecases' 且不为空,框架则不再自动扫描)
测试套需继承'TestSuite',并重写两个生命周期函数。 测试套py文件示例如下:
| 生命周期函数 | 说明 |
|---|---|
| setup | 整个测试套的前置步骤,在测试套运行前先执行该函数 |
| teardown | 整个测试套的清理步骤,在测试套运行后执行该函数 |
// TestSuite.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from devicetest.core.test_case import Step
from devicetest.core.suite.test_suite import TestSuite
class TestSuite(TestSuite):
def setup(self):
Step("Setup")
def teardown(self):
Step("Teardown")
接口参考
接口定义 |
参数说明 | 返回值 | 接口说明 |
|---|---|---|---|
| execute_shell_command(command, timeout) | 1. command:必选,shell命令字符串 2. timeout:可选,命令执行超时时间,单位 ms,执行超时抛出异常 |
shell命令执行输出 | 执行shell命令 |
| reboot() | 无 | 无 | 设备重启 |
| install_package(package_path) | package_path:必选,应用包路径 | bm安装命令执行输出 | 安装应用 |
| uninstall_package(package_name) | package_name:必选,应用包名 | bm卸载命令执行输出 | 卸载应用 |
| pull_file(remote, local) | 1. remote:必选,设备文件路径 2. local:必选,本地文件路径 |
无 | 设备pull文件到本地 |
| push_file(local, remote) | 1. local:必选,本地文件路径 2. remote:必选,设备文件路径 |
无 | 本地push文件到设备 |
注:
install_package()、push_file()等以本地路径作为参数的接口,需要将本地文件放置到 resource 目录下并通过get_resource_path()获取本地文件路径,示例如下:from devicetest.utils.file_util import get_resource_path path = get_resource_path('test.hap') // resource目录下的相对路径 res = self.device1.install_package(path)
用例编译指导
编译配置文件 BUILD.gn 主要将测试用例py文件及json文件拷贝到xts输出目录下。编译配置文件示例如下(以测试用例模式为例):
// BUILD.gn
import("//build/ohos.gni")
group("ActsDemoTest") {
testonly = true
deps = [ ":PyTestCase" ]
}
ohos_copy("PyTestCase") {
subsystem_name = "..."
part_name = "..."
sources = [
"./TestCase.json",
"./testcase1.py",
]
outputs =
[ root_out_dir + "/suites/acts/acts/testcases/{{source_file_part}}" ]
}
Python脚本用例执行指导
测试用例模式:
run -l TestCase
测试套模式:
全量执行:run -l TestSuite
部分执行:run -l TestSuite -ta class:testcase1,testcase3
部分不执行:run -l TestSuite -ta notClass:testcase1,testcase3
全量编译指导(适用于标准系统)
-
全量编译 test/xts/acts目录下执行编译命令:
./build.sh product_name=rk3568 system_size=standard -
单个子系统编译
test/xts/acts目录下执行编译命令:
./build.sh product_name=rk3568 system_size=standard target_subsystem=×××× -
单模块编译
test/xts/acts目录下执行编译命令:
./build.sh product_name=rk3568 system_size=standard suite=xxx suite 后面添加的是BUILD.gn 中ohos_js_hap_suite模板的命名测试用例输出目录:out/rk3568/suites/acts/testcases
测试框架&用例整体输出目录:out/rk3568/suites/acts(编译用例时会同步编译测试套执行框架)
全量用例执行指导(适用于小型系统、标准系统)
搭建测试环境Windows工作台下安装python3.7及以上版本,确保工作台和测试设备正常连接。
注:高版本python不带 easy_install,可以安装低于 52.0.0 的 setuptools,解决该问题。
pip install setuptools==50.0.0
测试执行目录(对应编译生成的out/release/suites/acts目录)
├── testcase # 测试套文件存放目录
│ └──xxx.hap # 测试套可执行hap文件
│ └──xxx.json # 测试套对应执行配置文件
├── tools # 测试框架工具目录
├── run.bat # window平台测试套启动执行文件
├── report # 测试报告生成目录
用例执行
-
在Windows工作台上,找到从Linux服务器上拷贝下来的测试套件用例目录,在Windows命令窗口进入对应目录,直接执行acts\run.bat。
-
界面启动后,输入用例执行指令。
全量执行:
run acts模块执行(具体模块可以查看\acts\testcases):
run –l ActsSamgrTest单包执行(具体模块可以查看\acts\testcases):(适用于OH驱动)
run -l uitestActs -ta class:UiTestCase#testChecked uitestActs: 测试hap UiTestCase: testsuite testChecked: testcase -
查看测试报告。 进入acts\reports\,获取当前的执行记录,打开“summary_report.html”可以获取到测试报告。
相关仓
xts_acts