return result;
}
// Step 2:使用断言比较预期与实际结果
void factorial_test()
{
int ret = factorial(3); // 调用函数获取结果
std::thread::id this_id = std::this_thread::get_id();
std::ostringstream oss;
oss << this_id;
std::string this_id_str = oss.str();
long int thread_id = atol(this_id_str.c_str());
printf(“running thread…: %ld\n”, thread_id); // 输出当前线程的id
EXPECT_EQ(ret, 6);
}
// GTEST_RUN_TASK(TestFunction)多线程启动函数,参数为自定义函数。
// 未调用SET_THREAD_NUM()时,默认线程数10个。
HWTEST_F(AAFwkBaseObjectTest, Factorial_test_001, TestSize.Level1)
{
SET_THREAD_NUM(4); // 设置线程数量,同一测试套中可动态设置线程数。
printf(“Factorial_test_001 BEGIN\n”);
GTEST_RUN_TASK(factorial_test); // 启动factorial_test任务的多线程执行
printf(“Factorial_test_001 END\n”);
}
// HWMTEST_F(TEST_SUITE, TEST_TC, TEST_LEVEL, THREAD_NUM)
// THREAD_NUM可设置用例执行的线程数量。
// HWMTEST_F会创建指定数量的线程并执行被测函数。
HWMTEST_F(AAFwkBaseObjectTest, Factorial_test_002, TestSize.Level1, 6)
{
printf(“Factorial_test_002 BEGIN\n”);
factorial_test();
printf(“Factorial_test_002 END\n”);
}
// 新增多线程接口MTEST_ADD_TASK(THREAD_ID,ThreadTestFunc),注册多线程,但不在该用例中执行,之后统一执行,适合多个用例组合场景下的多线程测试。
// THREAD_ID从0开始定义区别不同的线程,也可以使用随机THREAD_ID,即传入RANDOM_THREAD_ID,此场景下THREAD_ID是不会重复的。
// 新增多线程接口MTEST_POST_RUN(),统一执行之前注册的多线程用例。
注意: 用例注释与单线程用例标准一致。
在编写用例时,我们提供了四种用例模板供您选择。
类型 | 描述 |
---|---|
HWTEST(A,B,C) | 用例执行不依赖Setup&Teardown时,可选取 |
HWTEST_F(A,B,C) | 用例执行(不含参数)依赖于Setup&Teardown时,可选取 |
HWMTEST_F(A,B,C,D) | 多线程用例执行依赖于Setup&Teardown时,可选取 |
HWTEST_P(A,B,C) | 用例执行(含参数)依赖于Set&Teardown时,可选取 |
其中,参数A,B,C,D的含义如下:
- 参数A为测试套名。
- 参数B为测试用例名,其命名必须遵循[功能点]_[编号]的格式,编号为3位数字,从001开始。
- 参数C为测试用例等级,具体分为门禁level0 以及非门禁level1-level4共五个等级,其中非门禁level1-level4等级的具体选取规则为:测试用例功能越重要,level等级越低。
- 参数D为多线程用例执行的线程数量设置。
注意:
- 测试用例的预期结果必须有对应的断言。
- 测试用例必须填写用例等级。
- 测试体建议按照模板分步实现。
- 用例描述信息按照标准格式@tc.xxx value书写,注释信息必须包含用例名称,用例描述,用例类型,需求编号四项。其中用例测试类型@tc.type参数的选取,可参考下表。
- 如使用HWMTEST_F编写多线程执行用例,必须填线程数量。
测试类型名称 | 类型编码 |
---|---|
功能测试 | FUNC |
性能测试 | PERF |
可靠性测试 | RELI |
安全测试 | SECU |
模糊测试 | FUZZ |
TDD测试(JS)
- 用例源文件命名规范
测试用例原文件名称采用大驼峰风格,以TEST结尾,具体格式为:[功能][子功能]TEST,子功能支持向下细分。 示例:
AppInfoTest.js
- 用例示例
/*
- Copyright © 2021 XXXX Device Co., Ltd.
- Licensed under the Apache License, Version 2.0 (the “License”);
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an “AS IS” BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
*/
import app from ‘@system.app’
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from ‘deccjsunit/index’
describe(“AppInfoTest”, function () {
beforeAll(function() {
// input testsuit setup step,setup invoked before all testcases
console.info(‘beforeAll caled’)
})
afterAll(function() {
// input testsuit teardown step,teardown invoked after all testcases
console.info(‘afterAll caled’)
})
beforeEach(function() {
// input testcase setup step,setup invoked before each testcases
console.info(‘beforeEach caled’)
})
afterEach(function() {
// input testcase teardown step,teardown invoked after each testcases
console.info(‘afterEach caled’)
})
/*
- @tc.name:appInfoTest001
- @tc.desc:verify app info is not null
- @tc.type: FUNC
- @tc.require: issueNumber
*/
it(“appInfoTest001”, 0, function () {
//step 1:调用函数获取结果
var info = app.getInfo()
//Step 2:使用断言比较预期与实际结果
expect(info != null).assertEqual(true)
})
})
详细内容介绍:
- 添加测试用例文件头注释信息
/*
- Copyright © 2021 XXXX Device Co., Ltd.
- Licensed under the Apache License, Version 2.0 (the “License”);
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an “AS IS” BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
*/
- 导入被测api和jsunit测试库
import app from ‘@system.app’
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from ‘deccjsunit/index’
- 定义测试套(测试类)
describe(“AppInfoTest”, function () {
beforeAll(function() {
// input testsuit setup step,setup invoked before all testcases
console.info(‘beforeAll caled’)
})
afterAll(function() {
// input testsuit teardown step,teardown invoked after all testcases
console.info(‘afterAll caled’)
})
beforeEach(function() {
// input testcase setup step,setup invoked before each testcases
console.info(‘beforeEach caled’)
})
afterEach(function() {
// input testcase teardown step,teardown invoked after each testcases
console.info(‘afterEach caled’)
})
- 测试用例实现
/*
- @tc.name:appInfoTest001
- @tc.desc:verify app info is not null
- @tc.type: FUNC
- @tc.require: issueNumber
*/
it(“appInfoTest001”, 0, function () {
//step 1:调用函数获取结果
var info = app.getInfo()
//Step 2:使用断言比较预期与实际结果
expect(info != null).assertEqual(true)
})
注意: @tc.require: 格式必须以AR/SR或issue开头: 如:issueI56WJ7
测试用例编译文件编写
根据测试用例目录规划,当执行某一用例时,测试框架会根据编译文件逐层查找,最终找到所需用例进行编译。下面通过不同示例来讲解gn文件如何编写。
TDD测试
针对不同语言,下面提供不同的编译模板以供参考。
- C++用例编译配置示例
Copyright © 2021 XXXX Device Co., Ltd.
import(“//build/test.gni”)
module_output_path = “developer_test/calculator”
config(“module_private_config”) {
visibility = [ “😗” ]
include_dirs = [ “…/…/…/include” ]
}
ohos_unittest(“CalculatorSubTest”) {
module_out_path = module_output_path
sources = [
“…/…/…/include/calculator.h”,
“…/…/…/src/calculator.cpp”,
]
sources += [ “calculator_sub_test.cpp” ]
configs = [ “:module_private_config” ]
deps = [ “//third_party/googletest:gtest_main” ]
}
group(“unittest”) {
testonly = true
deps = [“:CalculatorSubTest”]
}
详细内容如下:
- 添加文件头注释信息
Copyright © 2021 XXXX Device Co., Ltd.
- 导入编译模板文件
import(“//build/test.gni”)
- 指定文件输出路径
module_output_path = “developer_test/calculator”
说明: 此处输出路径为部件/模块名。
- 配置依赖包含目录
config(“module_private_config”) {
visibility = [ “😗” ]
include_dirs = [ “…/…/…/include” ]
}
说明: 一般在此处对相关配置进行设置,在测试用例编译脚本中可直接引用。
- 指定测试用例编译目标输出的文件名称
ohos_unittest(“CalculatorSubTest”) {
}
- 编写具体的测试用例编译脚本(添加需要参与编译的源文件、配置和依赖)
ohos_unittest(“CalculatorSubTest”) {
module_out_path = module_output_path
sources = [
“…/…/…/include/calculator.h”,
“…/…/…/src/calculator.cpp”,
“…/…/…/test/calculator_sub_test.cpp”
]
sources += [ “calculator_sub_test.cpp” ]
configs = [ “:module_private_config” ]
deps = [ “//third_party/googletest:gtest_main” ]
}
- 对目标测试用例文件进行条件分组
ohos_unittest(“CalculatorSubTest”) {
module_out_path = module_output_path
sources = [
“…/…/…/include/calculator.h”,
“…/…/…/src/calculator.cpp”,
“…/…/…/test/calculator_sub_test.cpp”
]
sources += [ “calculator_sub_test.cpp” ]
configs = [ “:module_private_config” ]
deps = [ “//third_party/googletest:gtest_main” ]
}
说明: 进行条件分组的目的在于执行用例时可以选择性的执行某一种特定类型的用例。
- ** FA模型JavaScript用例编译配置示例**
Copyright © 2021 XXXX Device Co., Ltd.
import(“//build/test.gni”)
module_output_path = “developer_test/app_info”
ohos_js_unittest(“GetAppInfoJsTest”) {
module_out_path = module_output_path
hap_profile = “./config.json”
certificate_profile = “//test/developer_test/signature/openharmony_sx.p7b”
}
group(“unittest”) {
testonly = true
deps = [ “:GetAppInfoJsTest” ]
}
详细内容如下:
- 添加文件头注释信息
Copyright © 2021 XXXX Device Co., Ltd.
- 导入编译模板文件
import(“//build/test.gni”)
- 指定文件输出路径
module_output_path = “developer_test/app_info”
说明: 此处输出路径为部件/模块名。
- 指定测试用例编译目标输出的文件名称
ohos_js_unittest(“GetAppInfoJsTest”) {
}
说明:
- 使用模板ohos_js_unittest定义js测试套,注意与C++用例区分。
- js测试套编译输出文件为hap类型,hap名为此处定义的测试套名,测试套名称必须以JsTest结尾。
- 指定hap包配置文件config.json和签名文件,两个配置为必选项
ohos_js_unittest(“GetAppInfoJsTest”) {
module_out_path = module_output_path
hap_profile = “./config.json”
certificate_profile = “//test/developer_test/signature/openharmony_sx.p7b”
}
config.json为hap编译所需配置文件,需要开发者根据被测sdk版本配置“target”项,其余项可默认,具体如下所示:
{
“app”: {
“bundleName”: “com.example.myapplication”,
“vendor”: “example”,
“version”: {
“code”: 1,
“name”: “1.0”
},
“apiVersion”: {
“compatible”: 4,
“target”: 5 // 根据被测sdk版本进行修改,此例为sdk5
}
},
“deviceConfig”: {},
“module”: {
“package”: “com.example.myapplication”,
“name”: “.MyApplication”,
“deviceType”: [
“phone”
],
“distro”: {
“deliveryWithInstall”: true,
“moduleName”: “entry”,
“moduleType”: “entry”
},
“abilities”: [
{
“skills”: [
{
“entities”: [
“entity.system.home”
],
“actions”: [
“action.system.home”
]
}
],
“name”: “com.example.myapplication.MainAbility”,
“icon”: “
m
e
d
i
a
:
i
c
o
n
"
,
"
d
e
s
c
r
i
p
t
i
o
n
"
:
"
media:icon", "description": "
media:icon","description":"string:mainability_description”,
“label”: “MyApplication”,
“type”: “page”,
“launchType”: “standard”
}
],
“js”: [
{
“pages”: [
“pages/index/index”
],
“name”: “default”,
“window”: {
“designWidth”: 720,
“autoDesignWidth”: false
}
}
]
}
}
- 对目标测试用例文件进行条件分组
group(“unittest”) {
testonly = true
deps = [ “:GetAppInfoJsTest” ]
}
说明: 进行条件分组的目的在于执行用例时可以选择性的执行某一种特定类型的用例。
- stage模型ets用例编译配置示例
Copyright © 2022 XXXX Device Co., Ltd.
import(“//build/test.gni”)
want_output_path = “developer_test/stage_test”
ohos_js_stage_unittest(“ActsBundleMgrStageEtsTest”) {
hap_profile = “entry/src/main/module.json”
deps = [
“:actbmsstageetstest_js_assets”,
“:actbmsstageetstest_resources”,
]
ets2abc = true
certificate_profile = “signature/openharmony_sx.p7b”
hap_name = “ActsBundleMgrStageEtsTest”
subsystem_name = “developer_test”
part_name = “stage_test” // 部件名称
module_out_path = want_output_path // 必须定义输出路径
}
ohos_app_scope(“actbmsstageetstest_app_profile”) {
app_profile = “AppScope/app.json”
sources = [ “AppScope/resources” ]
}
ohos_js_assets(“actbmsstageetstest_js_assets”) {
source_dir = “entry/src/main/ets”
}
ohos_resources(“actbmsstageetstest_resources”) {
sources = [ “entry/src/main/resources” ]
deps = [ “:actbmsstageetstest_app_profile” ]
hap_profile = “entry/src/main/module.json”
}
group(“unittest”) {
testonly = true
deps = []
deps += [ “:ActsBundleMgrStageEtsTest” ]
}
说明: 进行条件分组的目的在于执行用例时可以选择性的执行某一种特定类型的用例。
编译入口配置文件bundle.json
当完成用例编译配置文件编写后,需要进一步编写部件编译配置文件,以关联到具体的测试用例。
“build”: {
“sub_component”: [
“//test/testfwk/developer_test/examples/app_info:app_info”,
“//test/testfwk/developer_test/examples/detector:detector”,
“//test/testfwk/developer_test/examples/calculator:calculator”
],
“inner_list”: [
{
“header”: {
“header_base”: “test/testfwk/developer_test/examples/detector/include”,
“header_files”: [
“detector.h”
]
},
“name”: “//test/testfwk/developer_test/examples/detector:detector”
}
],
“test”: [ //配置模块calculator下的test
“//test/testfwk/developer_test/examples/app_info/test:unittest”,
“//test/testfwk/developer_test/examples/calculator/test:unittest”,
“//test/testfwk/developer_test/examples/calculator/test:fuzztest”
}
说明: test_list中配置的是对应模块的测试用例。
测试用例资源配置
测试依赖资源主要包括测试用例在执行过程中需要的图片文件,视频文件、第三方库等对外的文件资源,目前只支持静态资源的配置。
依赖资源文件配置步骤如下:
- 在部件的test目录下创建resource目录,在resource目录下创建对应的模块,在模块目录中存放该模块所需要的资源文件
- 在resource目录下对应的模块目录中创建一个ohos_test.xml文件,文件内容格式如下:
- 在测试用例的编译配置文件中定义resource_config_file进行指引,用来指定对应的资源文件ohos_test.xml
ohos_unittest(“CalculatorSubTest”) {
resource_config_file = “//system/subsystem/partA/test/resource/calculator/ohos_test.xml”
}
说明:
- target_name: 测试套的名称,定义在测试目录的BUILD.gn中。preparer: 表示该测试套执行前执行的动作。
- src=“res”: 表示测试资源位于test目录下的resource目录下,src=“out”:表示位于out/release/$(部件)目录下。
测试用例执行
在执行测试用例之前,针对用例使用设备的不同,需要对相应配置进行修改,修改完成即可执行测试用例。
user_config.xml配置
<user_config>
false
false
true
cmd 115200 8 1 1**说明:**在执行测试用例之前,若使用HDC连接设备,用例仅需配置设备IP和端口号即可,其余信息均默认不修改。
Windows环境执行
测试用例编译
由于Windows环境下无法实现用例编译,因此执行用例前需要在Linux环境下进行用例编译,用例编译命令:
./build.sh --product-name {product_name} --build-target make_test
说明:
- product-name:指定编译产品名称。
- build-target:指定所需编译用例,make_test表示指定全部用例,实际开发中可指定特定用例。
编译完成后,测试用例将自动保存在out/ohos-arm-release/packages/phone/tests目录下。
搭建执行环境
- 在Windows环境创建测试框架目录Test,并在此目录下创建testcase目录
- 从Linux环境拷贝测试框架developer_test和xdevice到创建的Test目录下,拷贝编译好的测试用例到testcase目录下
说明: 将测试框架及测试用例从Linux环境移植到Windows环境,以便后续执行。
- 修改user_config.xml
说明:
<testcase>
标签表示是否需要编译用例;<dir>
标签表示测试用例查找路径。
执行用例
- 启动测试框架
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数HarmonyOS鸿蒙开发工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年HarmonyOS鸿蒙开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上HarmonyOS鸿蒙开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新
如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注鸿蒙获取)
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
后所存放的路径 -->
说明:
<testcase>
标签表示是否需要编译用例;<dir>
标签表示测试用例查找路径。
执行用例
- 启动测试框架
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数HarmonyOS鸿蒙开发工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年HarmonyOS鸿蒙开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-DZJMas2X-1712691868558)]
[外链图片转存中…(img-AX9EdvPI-1712691868559)]
[外链图片转存中…(img-BOJ45JvK-1712691868559)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上HarmonyOS鸿蒙开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新
如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注鸿蒙获取)
[外链图片转存中…(img-B91Tk880-1712691868559)]
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!