1# 开发自测试执行框架使用指南 2 3 4## 概述 5 6OpenHarmony为开发者提供了一套全面的开发自测试框架developer_test,作为OpenHarmony测试工具集的一部分,提供给开发者自测试使用。开发者可根据测试需求开发相关测试用例,开发阶段提前发现缺陷,大幅提高代码质量。 7 8本文从基础环境构建,用例开发,编译以及执行等方面介绍OpenHarmony开发自测试执行框架如何运行和使用。 9 10 11### 简介 12 13OpenHarmony系统开发人员在新增或修改代码之后,希望可以快速的验证修改代码的功能的正确性,而系统中已经存在了大量的已有功能的自动化测试用例,比如TDD用例或XTS用例等。开发者自测试执行框架目的就是为了提升开发者的自测试执行效率,方便开发人员可以快速便捷的执行指定的自动化测试用例,将测试活动左移到开发阶段。 14 15 16### 约束与限制 17 18框架使用时必须提前连接OpenHarmony设备方可执行测试用例。 19 20 21## 环境准备 22 23开发自测试框架依赖于python运行环境,python版本为3.7.5及以上版本,在使用测试框架之前可参见以下方式进行配置。 24 25源码获取可[参考此链接](https://gitee.com/openharmony/docs/blob/master/zh-cn/device-dev/get-code/sourcecode-acquire.md)。 26 27### 自测试框架基础环境依赖 28 29| 环境依赖 | 版本型号 | 详细说明 | 30| ----------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | 31| 操作系统 | Ubuntu20.04及以上 | 代码编译环境 | 32| Linux系统扩展组件 | libreadline-dev | 命令行读取插件 | 33| python | 3.7.5版本及以上 | 测试框架语言 | 34| python插件 | pyserial 3.3及以上、paramiko2.7.1及以上、setuptools40.8.0及以上、rsa4.0及以上 | pserial:支持python的串口通信;paramiko:支持python使用SSH协议;setuptools:支持python方便创建和分发python包;rsa:支持python rsa加密 | 35| NFS Server | haneWIN NFS Server 1.2.50及以上或者 NFS v4及以上 | 支持设备通过串口连接,使用轻量、小型设备 | 36| HDC | 1.2.0a | 支持设备通过HDC连接 | 37 38 39 401. 安装Ubuntu的步骤如下: 41 42 参考此文[OpenHarmony-标准设备系统代码操作梳理](https://blog.csdn.net/nanzhanfei/article/details/121951919),按照第二章“WSL2安装”步骤操作,即可完成Ubuntu的安装。由于Ubuntu20已自带python3.8.5,无需再次安装python。 43 442. 安装Linux扩展组件readline,安装命令如下: 45 46 ```bash 47 sudo apt-get install libreadline-dev 48 ``` 49 安装成功提示如下: 50 ``` 51 Reading package lists... Done 52 Building dependency tree 53 Reading state information... Done 54 libreadline-dev is already the newest version (7.0-3). 55 0 upgraded, 0 newly installed, 0 to remove and 11 not upgraded. 56 ``` 57 582. 安装setuptools插件,安装命令如下: 59 ```bash 60 pip3 install setuptools 61 ``` 62 安装成功提示如下: 63 ``` 64 Requirement already satisfied: setuptools in d:\programs\python37\lib\site-packages (41.2.0) 65 ``` 66 673. 安装paramiko插件,安装命令如下: 68 ```bash 69 pip3 install paramiko 70 ``` 71 安装成功提示如下: 72 ``` 73 Installing collected packages: pycparser, cffi, pynacl, bcrypt, cryptography, paramiko 74 Successfully installed bcrypt-3.2.0 cffi-1.14.4 cryptography-3.3.1 paramiko-2.7.2 pycparser-2.20 pynacl-1.4.0 75 ``` 76 774. 安装python的rsa插件,安装命令如下: 78 ```bash 79 pip3 install rsa 80 ``` 81 安装成功提示如下: 82 ``` 83 Installing collected packages: pyasn1, rsa 84 Successfully installed pyasn1-0.4.8 rsa-4.7 85 ``` 86 875. 安装串口插件pyserial,安装命令如下: 88 ```bash 89 pip3 install pyserial 90 ``` 91 安装成功提示如下: 92 ``` 93 Requirement already satisfied: pyserial in d:\programs\python37\lib\site-packages\pyserial-3.4-py3.7.egg (3.4) 94 ``` 95 966. 如果设备仅支持串口输出测试结果,则需要安装NFS Server 97 98 > **说明:** 99 > 100 > 此操作针对小型或轻量设备,不针对标准设备。 101 102 - Windows环境下安装,安装haneWIN NFS Server1.2.50软件包。 103 - Linux环境下安装,安装命令如下: 104 ```bash 105 sudo apt install nfs-kernel-server 106 ``` 107 安装成功提示如下: 108 ``` 109 Reading package lists... Done 110 Building dependency tree 111 Reading state information... Done 112 nfs-kernel-server is already the newest version (1:1.3.4-2.1ubuntu5.3). 113 0 upgraded, 0 newly installed, 0 to remove and 11 not upgraded. 114 ``` 115 1167. 如果设备支持HDC连接,则需要安装HDC工具,安装流程请参考[HDC-OpenHarmony设备连接器](https://gitee.com/openharmony/developtools_hdc/blob/master/README_zh.md)。 117 118 119### 环境依赖检查 120 121| 检查项 | 操作 | 满足环境 | 122| -------------------------------------------------- | --------------------------------------------------- | ------------------------- | 123| 检查python安装成功 | 命令行窗口执行命令:python --version | 版本不小于3.7.5即可 | 124| 检查python扩展插件安装成功 | 打开test/developertest目录,执行start.bat或start.sh | 可进入提示符“>>>”界面即可 | 125| 检查NFS Server启动状态(被测设备仅支持串口时检测) | 通过串口登录开发板,执行mount命令挂载NFS | 可正常挂载文件目录即可 | 126| 检查HDC安装成功 | 命令行窗口执行命令:hdc -v | 版本不小于1.2.0a即可 | 127 128 129## 编写测试用例 130 131本测试框架支持多种类型测试,针对不同测试类型提供了不同的用例编写模板以供参考。 132 133**TDD测试(C++)** 134 135- 用例源文件命名规范 136 137 测试用例源文件名称和测试套内容保持一致,文件命名采用全小写+下划线格式,以test结尾,具体格式为:[功能]_[子功能]_test,子功能支持向下细分。 138 139 140- 单线程用例示例及编写步骤,以calculator_sub_test.cpp为例进行说明: 141 ``` 142 /* 143 * Copyright (c) 2023 XXXX Device Co., Ltd. 144 * Licensed under the Apache License, Version 2.0 (the "License"); 145 * you may not use this file except in compliance with the License. 146 * You may obtain a copy of the License at 147 * 148 * http://www.apache.org/licenses/LICENSE-2.0 149 * 150 * Unless required by applicable law or agreed to in writing, software 151 * distributed under the License is distributed on an "AS IS" BASIS, 152 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 153 * See the License for the specific language governing permissions and 154 * limitations under the License. 155 */ 156 157 #include "calculator.h" 158 #include <gtest/gtest.h> 159 160 using namespace testing::ext; 161 162 class CalculatorSubTest : public testing::Test { 163 public: 164 static void SetUpTestCase(void); 165 static void TearDownTestCase(void); 166 void SetUp(); 167 void TearDown(); 168 }; 169 170 void CalculatorSubTest::SetUpTestCase(void) 171 { 172 // input testsuit setup step,setup invoked before all testcases 173 } 174 175 void CalculatorSubTest::TearDownTestCase(void) 176 { 177 // input testsuit teardown step,teardown invoked after all testcases 178 } 179 180 void CalculatorSubTest::SetUp(void) 181 { 182 // input testcase setup step,setup invoked before each testcases 183 } 184 185 void CalculatorSubTest::TearDown(void) 186 { 187 // input testcase teardown step,teardown invoked after each testcases 188 } 189 190 /** 191 * @tc.name: integer_sub_001 192 * @tc.desc: Verify the sub function. 193 * @tc.type: FUNC 194 * @tc.require: issueNumber 195 */ 196 HWTEST_F(CalculatorSubTest, integer_sub_001, TestSize.Level1) 197 { 198 // step 1:调用函数获取结果 199 int actual = Sub(4,0); 200 201 // Step 2:使用断言比较预期与实际结果 202 EXPECT_EQ(4, actual); 203 } 204 ``` 205 详细内容介绍: 206 1. 添加测试用例文件头注释信息 207 ``` 208 /* 209 * Copyright (c) 2023 XXXX Device Co., Ltd. 210 * Licensed under the Apache License, Version 2.0 (the "License"); 211 * you may not use this file except in compliance with the License. 212 * You may obtain a copy of the License at 213 * 214 * http://www.apache.org/licenses/LICENSE-2.0 215 * 216 * Unless required by applicable law or agreed to in writing, software 217 * distributed under the License is distributed on an "AS IS" BASIS, 218 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 219 * See the License for the specific language governing permissions and 220 * limitations under the License. 221 */ 222 ``` 223 2. 引用测试框架头文件和命名空间 224 ``` 225 #include <gtest/gtest.h> 226 227 using namespace testing::ext; 228 ``` 229 3. 添加被测试类的头文件 230 ``` 231 #include "calculator.h" 232 ``` 233 4. 定义测试套(测试类) 234 > **注意:** 在定义测试套时,测试套名称应与编译目标保持一致,采用大驼峰风格。 235 ``` 236 class CalculatorSubTest : public testing::Test { 237 public: 238 static void SetUpTestCase(void); 239 static void TearDownTestCase(void); 240 void SetUp(); 241 void TearDown(); 242 }; 243 244 void CalculatorSubTest::SetUpTestCase(void) 245 { 246 // input testsuit setup step,setup invoked before all testcases 247 } 248 249 void CalculatorSubTest::TearDownTestCase(void) 250 { 251 // input testsuit teardown step,teardown invoked after all testcases 252 } 253 254 void CalculatorSubTest::SetUp(void) 255 { 256 // input testcase setup step,setup invoked before each testcases 257 } 258 259 void CalculatorSubTest::TearDown(void) 260 { 261 // input testcase teardown step,teardown invoked after each testcases 262 } 263 ``` 264 265 5. 测试用例实现,包含用例注释和逻辑实现 266 ``` 267 /** 268 * @tc.name: integer_sub_001 269 * @tc.desc: Verify the sub function. 270 * @tc.type: FUNC 271 * @tc.require: issueNumber 272 */ 273 HWTEST_F(CalculatorSubTest, integer_sub_001, TestSize.Level1) 274 { 275 //step 1:调用函数获取结果 276 int actual = Sub(4,0); 277 278 //Step 2:使用断言比较预期与实际结果 279 EXPECT_EQ(4, actual); 280 } 281 ``` 282 > **注意:** @tc.require的取值必须以AR/SR或issue开头,如:issueI56WJ7。 283 284- 多线程用例示例及编写步骤,以base_object_test.cpp为例进行说明: 285 ``` 286 // 测试用例文件头注释信息及用例注释同单线程用例示例。 287 288 #include "base_object.h" 289 #include <gtest/gtest.h> 290 #include <gtest/hwext/gtest-multithread.h> 291 #include <unistd.h> 292 293 using namespace testing::ext; 294 using namespace testing::mt; 295 296 namespace OHOS { 297 namespace AAFwk { 298 class AAFwkBaseObjectTest : public testing::Test {......} 299 300 // Step 1:待测函数,返回阶乘结果 301 int factorial(int n) 302 { 303 int result = 1; 304 for (int i = 1; i <= n; i++) { 305 result *= i; 306 } 307 printf("Factorial Function Result : %d! = %d\n", n, result); 308 return result; 309 } 310 311 // Step 2:使用断言比较预期与实际结果 312 void factorial_test() 313 { 314 int ret = factorial(3); // 调用函数获取结果 315 std::thread::id this_id = std::this_thread::get_id(); 316 std::ostringstream oss; 317 oss << this_id; 318 std::string this_id_str = oss.str(); 319 long int thread_id = atol(this_id_str.c_str()); 320 printf("running thread...: %ld\n", thread_id); // 输出当前线程的id 321 EXPECT_EQ(ret, 6); 322 } 323 324 HWTEST_F(AAFwkBaseObjectTest, Factorial_test_001, TestSize.Level1) 325 { 326 SET_THREAD_NUM(4); 327 printf("Factorial_test_001 BEGIN\n"); 328 GTEST_RUN_TASK(factorial_test); 329 printf("Factorial_test_001 END\n"); 330 } 331 332 HWMTEST_F(AAFwkBaseObjectTest, Factorial_test_002, TestSize.Level1, 6) 333 { 334 printf("Factorial_test_002 BEGIN\n"); 335 factorial_test(); 336 printf("Factorial_test_002 END\n"); 337 } 338 339 } // namespace AAFwk 340 } // namespace OHOS 341 342 ``` 343 详细内容介绍: 344 1. 添加测试用例文件头注释信息 345 346 > **注意:** 与单线程用例标准一致。 347 348 2. 引用测试框架头文件和命名空间 349 ``` 350 #include <gtest/gtest.h> 351 #include <gtest/hwext/gtest-multithread.h> 352 #include <unistd.h> 353 using namespace testing::ext; 354 using namespace testing::mt; 355 ``` 356 3. 添加被测试类的头文件 357 ``` 358 #include "base_object.h" 359 ``` 360 4. 定义测试套(测试类) 361 ``` 362 class AAFwkBaseObjectTest : public testing::Test {......} 363 364 ``` 365 > **注意:** 与单线程用例标准一致。 366 367 5. 测试用例实现,包含用例注释和逻辑实现 368 369 ``` 370 // Step 1:待测函数,返回阶乘结果 371 int factorial(int n) 372 { 373 int result = 1; 374 for (int i = 1; i <= n; i++) { 375 result *= i; 376 } 377 printf("Factorial Function Result : %d! = %d\n", n, result); 378 return result; 379 } 380 381 // Step 2:使用断言比较预期与实际结果 382 void factorial_test() 383 { 384 int ret = factorial(3); // 调用函数获取结果 385 std::thread::id this_id = std::this_thread::get_id(); 386 std::ostringstream oss; 387 oss << this_id; 388 std::string this_id_str = oss.str(); 389 long int thread_id = atol(this_id_str.c_str()); 390 printf("running thread...: %ld\n", thread_id); // 输出当前线程的id 391 EXPECT_EQ(ret, 6); 392 } 393 394 // GTEST_RUN_TASK(TestFunction)多线程启动函数,参数为自定义函数。 395 // 未调用SET_THREAD_NUM()时,默认线程数10个。 396 HWTEST_F(AAFwkBaseObjectTest, Factorial_test_001, TestSize.Level1) 397 { 398 SET_THREAD_NUM(4); // 设置线程数量,同一测试套中可动态设置线程数。 399 printf("Factorial_test_001 BEGIN\n"); 400 GTEST_RUN_TASK(factorial_test); // 启动factorial_test任务的多线程执行 401 printf("Factorial_test_001 END\n"); 402 } 403 404 // HWMTEST_F(TEST_SUITE, TEST_TC, TEST_LEVEL, THREAD_NUM) 405 // THREAD_NUM可设置用例执行的线程数量。 406 // HWMTEST_F会创建指定数量的线程并执行被测函数。 407 HWMTEST_F(AAFwkBaseObjectTest, Factorial_test_002, TestSize.Level1, 6) 408 { 409 printf("Factorial_test_002 BEGIN\n"); 410 factorial_test(); 411 printf("Factorial_test_002 END\n"); 412 } 413 // 新增多线程接口MTEST_ADD_TASK(THREAD_ID,ThreadTestFunc),注册多线程,但不在该用例中执行,之后统一执行,适合多个用例组合场景下的多线程测试。 414 // THREAD_ID从0开始定义区别不同的线程,也可以使用随机THREAD_ID,即传入RANDOM_THREAD_ID,此场景下THREAD_ID是不会重复的。 415 // 新增多线程接口MTEST_POST_RUN(),统一执行之前注册的多线程用例。 416 ``` 417 > **注意:** 多线程用例注释与单线程用例标准一致。 418 419- C++用例模板说明: 420 421 在编写用例时,我们提供了四种用例模板供您选择。 422 423 | 类型 | 描述 | 424 | ------------| ------------| 425 | HWTEST(A,B,C)| 用例执行不依赖Setup&Teardown时,可选取| 426 | HWTEST_F(A,B,C)| 用例执行(不含参数)依赖于Setup&Teardown时,可选取| 427 | HWMTEST_F(A,B,C,D)| 多线程用例执行依赖于Setup&Teardown时,可选取| 428 | HWTEST_P(A,B,C)| 用例执行(含参数)依赖于Set&Teardown时,可选取| 429 430 431 其中,参数A,B,C的含义如下: 432 433 - 参数A为测试套名。 434 435 - 参数B为测试用例名,其命名必须遵循[功能点]_[编号]的格式,编号为3位数字,从001开始。 436 437 - 参数C为测试用例等级,具体分为门禁level0 以及非门禁level1-level4共五个等级,其中非门禁level1-level4等级的具体选取规则为:测试用例功能越重要,level等级越低。 438 439 - 参数D为多线程用例执行的线程数量设置。 440 441 **注意:** 442 443 - 测试用例的预期结果必须有对应的断言。 444 445 - 测试用例必须填写用例等级。 446 447 - 测试体建议按照模板分步实现。 448 449 - 用例描述信息按照标准格式@tc.xxx value书写,注释信息必须包含用例名称,用例描述,用例类型,需求编号四项。其中用例测试类型@tc.type参数的选取,可参考下表。 450 451 452 | 测试类型名称 | 类型编码 | 453 | ------------ | -------- | 454 | 功能测试 | FUNC | 455 | 性能测试 | PERF | 456 | 可靠性测试 | RELI | 457 | 安全测试 | SECU | 458 | 模糊测试 | FUZZ | 459 460**TDD测试(JavaScript)** 461 462- 用例源文件命名规范 463 464 465 测试用例原文件名称采用大驼峰风格,以Test结尾,具体格式为:[功能][子功能]Test,子功能支持向下细分。 466 示例: 467 ``` 468 AppInfoTest.js 469 ``` 470 471- 用例示例 472 473 ```js 474 /* 475 * Copyright (C) 2023 XXXX Device Co., Ltd. 476 * Licensed under the Apache License, Version 2.0 (the "License"); 477 * you may not use this file except in compliance with the License. 478 * You may obtain a copy of the License at 479 * 480 * http://www.apache.org/licenses/LICENSE-2.0 481 * 482 * Unless required by applicable law or agreed to in writing, software 483 * distributed under the License is distributed on an "AS IS" BASIS, 484 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 485 * See the License for the specific language governing permissions and 486 * limitations under the License. 487 */ 488 import app from '@system.app' 489 490 import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' 491 492 describe("AppInfoTest", function () { 493 beforeAll(function() { 494 // input testsuit setup step,setup invoked before all testcases 495 console.info('beforeAll caled') 496 }) 497 498 afterAll(function() { 499 // input testsuit teardown step,teardown invoked after all testcases 500 console.info('afterAll caled') 501 }) 502 503 beforeEach(function() { 504 // input testcase setup step,setup invoked before each testcases 505 console.info('beforeEach caled') 506 }) 507 508 afterEach(function() { 509 // input testcase teardown step,teardown invoked after each testcases 510 console.info('afterEach caled') 511 }) 512 513 /* 514 * @tc.name:appInfoTest001 515 * @tc.desc:verify app info is not null 516 * @tc.type: FUNC 517 * @tc.require: issueNumber 518 */ 519 it("appInfoTest001", 0, function () { 520 //step 1:调用函数获取结果 521 var info = app.getInfo() 522 523 //Step 2:使用断言比较预期与实际结果 524 expect(info != null).assertEqual(true) 525 }) 526 }) 527 ``` 528 详细内容介绍: 529 1. 添加测试用例文件头注释信息 530 ``` 531 /* 532 * Copyright (C) 2023 XXXX Device Co., Ltd. 533 * Licensed under the Apache License, Version 2.0 (the "License"); 534 * you may not use this file except in compliance with the License. 535 * You may obtain a copy of the License at 536 * 537 * http://www.apache.org/licenses/LICENSE-2.0 538 * 539 * Unless required by applicable law or agreed to in writing, software 540 * distributed under the License is distributed on an "AS IS" BASIS, 541 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 542 * See the License for the specific language governing permissions and 543 * limitations under the License. 544 */ 545 ``` 546 2. 导入被测api和jsunit测试库 547 ```js 548 import app from '@system.app' 549 550 import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' 551 ``` 552 3. 定义测试套(测试类) 553 ```js 554 describe("AppInfoTest", function () { 555 beforeAll(function() { 556 // input testsuit setup step,setup invoked before all testcases 557 console.info('beforeAll caled') 558 }) 559 560 afterAll(function() { 561 // input testsuit teardown step,teardown invoked after all testcases 562 console.info('afterAll caled') 563 }) 564 565 beforeEach(function() { 566 // input testcase setup step,setup invoked before each testcases 567 console.info('beforeEach caled') 568 }) 569 570 afterEach(function() { 571 // input testcase teardown step,teardown invoked after each testcases 572 console.info('afterEach caled') 573 }) 574 ``` 575 4. 测试用例实现 576 ```JS 577 /* 578 * @tc.name:appInfoTest001 579 * @tc.desc:verify app info is not null 580 * @tc.type: FUNC 581 * @tc.require: issueNumber 582 */ 583 it("appInfoTest001", 0, function () { 584 //step 1:调用函数获取结果 585 var info = app.getInfo() 586 587 //Step 2:使用断言比较预期与实际结果 588 expect(info != null).assertEqual(true) 589 }) 590 ``` 591 > **注意:** @tc.require的取值必须以AR/SR或issue开头,如:issueI56WJ7。 592 593**TDD测试(Rust)** 594 595- 用例源文件命名规范 596 597 测试用例源文件名称和测试套内容保持一致,测试套命名采用全小写rust_+下划线格式,以test结尾,具体格式为:rust_[功能]_[子功能]_test,子功能支持向下细分。 598 599 600- Rust用例示例及编写步骤,以rust_add_test.rs为例进行说明: 601 ```Rust 602 /* 603 * Copyright (c) 2023 XXXX Device Co., Ltd. 604 * Licensed under the Apache License, Version 2.0 (the "License"); 605 * you may not use this file except in compliance with the License. 606 * You may obtain a copy of the License at 607 * 608 * http://www.apache.org/licenses/LICENSE-2.0 609 * 610 * Unless required by applicable law or agreed to in writing, software 611 * distributed under the License is distributed on an "AS IS" BASIS, 612 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 613 * See the License for the specific language governing permissions and 614 * limitations under the License. 615 */ 616 617 /// pub add 618 pub fn add(a: i32, b: i32) -> i32 { 619 a + b 620 } 621 622 #[allow(dead_code)] 623 pub fn bad_add(a: i32, b: i32) -> i32 { 624 a - b 625 } 626 627 #[cfg(test)] 628 mod tests { 629 use super::*; 630 #[test] 631 fn test_add() { 632 assert_eq!(add(1, 2), 3); 633 } 634 #[test] 635 fn test_bad_add() { 636 assert_eq!(bad_add(1, 2), 3); 637 } 638 } 639 ``` 640 详细内容介绍: 641 1. 添加测试用例文件头注释信息 642 ``` 643 /* 644 * Copyright (c) 2023 XXXX Device Co., Ltd. 645 * Licensed under the Apache License, Version 2.0 (the "License"); 646 * you may not use this file except in compliance with the License. 647 * You may obtain a copy of the License at 648 * 649 * http://www.apache.org/licenses/LICENSE-2.0 650 * 651 * Unless required by applicable law or agreed to in writing, software 652 * distributed under the License is distributed on an "AS IS" BASIS, 653 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 654 * See the License for the specific language governing permissions and 655 * limitations under the License. 656 */ 657 ``` 658 2. 定义测试类 659 ```Rust 660 /// pub add 661 pub fn add(a: i32, b: i32) -> i32 { 662 a + b 663 } 664 665 #[allow(dead_code)] 666 pub fn bad_add(a: i32, b: i32) -> i32 { 667 a - b 668 } 669 ``` 670 3. 测试用例实现 671 ```Rust 672 #[cfg(test)] 673 mod tests { 674 use super::*; 675 676 #[test] 677 fn test_add() { 678 assert_eq!(add(1, 2), 3); 679 } 680 681 #[test] 682 fn test_bad_add() { 683 assert_eq!(bad_add(1, 2), 3); 684 } 685 } 686 ``` 687 > **注意:** #[cfg(test)]属性将测试代码标记为测试模块,并使用#[test]属性标记测试函数,这样才能被Rust的测试框架识别为测试函数 688 689**Fuzzing安全测试** 690 691[Fuzzing安全测试用例编写规范](https://gitee.com/openharmony/testfwk_developer_test/blob/master/libs/fuzzlib/README_zh.md) 692 693 694**Benchmark测试** 695 696[Benchmark用例编写规范](https://gitee.com/openharmony/testfwk_developer_test/blob/master/libs/benchmark/README_zh.md) 697 698## 编译测试用例 699 700根据测试用例目录规划,当执行某一用例时,测试框架会根据编译文件逐层查找,最终找到所需用例进行编译。下面通过不同示例来讲解gn文件如何编写。 701 702**TDD测试** 703 704针对不同语言,下面提供不同的编译模板以供参考。 705 706- **C++用例编译配置示例** 707 708 ``` 709 # Copyright (c) 2023 XXXX Device Co., Ltd. 710 711 import("//build/test.gni") 712 713 module_output_path = "developertest/calculator" 714 715 config("module_private_config") { 716 visibility = [ ":*" ] 717 718 include_dirs = [ "../../../include" ] 719 } 720 721 ohos_unittest("CalculatorSubTest") { 722 module_out_path = module_output_path 723 724 sources = [ 725 "../../../include/calculator.h", 726 "../../../src/calculator.cpp", 727 ] 728 729 sources += [ "calculator_sub_test.cpp" ] 730 731 configs = [ ":module_private_config" ] 732 733 deps = [ "//third_party/googletest:gtest_main" ] 734 } 735 736 group("unittest") { 737 testonly = true 738 deps = [":CalculatorSubTest"] 739 } 740 ``` 741 详细内容如下: 742 743 1. 添加文件头注释信息 744 ``` 745 # Copyright (c) 2023 XXXX Device Co., Ltd. 746 ``` 747 2. 导入编译模板文件 748 ``` 749 import("//build/test.gni") 750 ``` 751 3. 指定文件输出路径 752 ``` 753 module_output_path = "developertest/calculator" 754 ``` 755 > **说明:** 756 > 757 > 此处输出路径为部件/模块名。 758 759 4. 配置依赖包含目录 760 761 ``` 762 config("module_private_config") { 763 visibility = [ ":*" ] 764 765 include_dirs = [ "../../../include" ] 766 } 767 ``` 768 > **说明:** 769 > 770 > 一般在此处对相关配置进行设置,在测试用例编译脚本中可直接引用。 771 772 5. 指定测试用例编译目标输出的文件名称 773 774 ``` 775 ohos_unittest("CalculatorSubTest") { 776 } 777 ``` 778 6. 编写具体的测试用例编译脚本(添加需要参与编译的源文件、配置和依赖) 779 ``` 780 ohos_unittest("CalculatorSubTest") { 781 module_out_path = module_output_path 782 sources = [ 783 "../../../include/calculator.h", 784 "../../../src/calculator.cpp", 785 "../../../test/calculator_sub_test.cpp" 786 ] 787 sources += [ "calculator_sub_test.cpp" ] 788 configs = [ ":module_private_config" ] 789 deps = [ "//third_party/googletest:gtest_main" ] 790 } 791 ``` 792 793 > **说明:** 794 > 795 > 根据测试类型的不同,在具体编写过程中可选择不同的测试类型。 796 > - ohos_unittest:单元测试 797 > - ohos_js_unittest: FA模型js用例单元测试 798 > - ohos_js_stage_unittest: stage模型ArkTs用例单元测试 799 > - ohos_moduletest:模块测试 800 > - ohos_systemtest:系统测试 801 > - ohos_performancetest:性能测试 802 > - ohos_securitytest:安全测试 803 > - ohos_reliabilitytest:可靠性测试 804 > - ohos_distributedtest:分布式测试 805 806 7. 对目标测试用例文件进行条件分组 807 808 ``` 809 group("unittest") { 810 testonly = true 811 deps = [":CalculatorSubTest"] 812 } 813 ``` 814 > **说明:** 815 > 816 > 进行条件分组的目的在于执行用例时可以选择性地执行某一种特定类型的用例。 817 818- **JavaScript用例编译配置示例** 819 820 ``` 821 # Copyright (C) 2023 XXXX Device Co., Ltd. 822 823 import("//build/test.gni") 824 825 module_output_path = "developertest/app_info" 826 827 ohos_js_unittest("GetAppInfoJsTest") { 828 module_out_path = module_output_path 829 830 hap_profile = "./config.json" 831 certificate_profile = "//test/developertest/signature/openharmony_sx.p7b" 832 } 833 834 group("unittest") { 835 testonly = true 836 deps = [ ":GetAppInfoJsTest" ] 837 } 838 ``` 839 840 详细内容如下: 841 842 1. 添加文件头注释信息 843 844 ``` 845 # Copyright (C) 2023 XXXX Device Co., Ltd. 846 ``` 847 848 2. 导入编译模板文件 849 850 ``` 851 import("//build/test.gni") 852 ``` 853 854 3. 指定文件输出路径 855 856 ``` 857 module_output_path = "developertest/app_info" 858 ``` 859 > **说明:** 860 > 861 > 此处输出路径为部件/模块名。 862 863 4. 指定测试用例编译目标输出的文件名称 864 865 ``` 866 ohos_js_unittest("GetAppInfoJsTest") { 867 } 868 ``` 869 > **说明:** 870 > 871 > - 使用模板ohos_js_unittest定义js测试套,注意与C++用例区分。 872 > - js测试套编译输出文件为hap类型,hap名为此处定义的测试套名,测试套名称必须以JsTest结尾。 873 874 5. 指定hap包配置文件config.json和签名文件,两个配置为必选项。 875 876 ``` 877 ohos_js_unittest("GetAppInfoJsTest") { 878 module_out_path = module_output_path 879 880 hap_profile = "./config.json" 881 certificate_profile = "//test/developertest/signature/openharmony_sx.p7b" 882 } 883 ``` 884 config.json为hap编译所需配置文件,需要开发者根据被测sdk版本配置“target”项,其余项可默认,具体如下所示: 885 886 ```json 887 { 888 "app": { 889 "bundleName": "com.example.myapplication", 890 "vendor": "example", 891 "version": { 892 "code": 1, 893 "name": "1.0" 894 }, 895 "apiVersion": { 896 "compatible": 4, 897 "target": 5 // 根据被测sdk版本进行修改,此例为sdk5 898 } 899 }, 900 "deviceConfig": {}, 901 "module": { 902 "package": "com.example.myapplication", 903 "name": ".MyApplication", 904 "deviceType": [ 905 "phone" 906 ], 907 "distro": { 908 "deliveryWithInstall": true, 909 "moduleName": "entry", 910 "moduleType": "entry" 911 }, 912 "abilities": [ 913 { 914 "skills": [ 915 { 916 "entities": [ 917 "entity.system.home" 918 ], 919 "actions": [ 920 "action.system.home" 921 ] 922 } 923 ], 924 "name": "com.example.myapplication.MainAbility", 925 "icon": "$media:icon", 926 "description": "$string:mainability_description", 927 "label": "MyApplication", 928 "type": "page", 929 "launchType": "standard" 930 } 931 ], 932 "js": [ 933 { 934 "pages": [ 935 "pages/index/index" 936 ], 937 "name": "default", 938 "window": { 939 "designWidth": 720, 940 "autoDesignWidth": false 941 } 942 } 943 ] 944 } 945 } 946 ``` 947 948 6. 对目标测试用例文件进行条件分组 949 950 ``` 951 group("unittest") { 952 testonly = true 953 deps = [ ":GetAppInfoJsTest" ] 954 } 955 ``` 956 > **说明:** 957 > 958 > 进行条件分组的目的在于执行用例时可以选择性地执行某一种特定类型的用例。 959 960- **stage模型ArkTs用例编译配置示例** 961 962 ``` 963 # Copyright (C) 2023 XXXX Device Co., Ltd. 964 965 import("//build/test.gni") 966 967 want_output_path = "developertest/stage_test" 968 969 ohos_js_stage_unittest("ActsBundleMgrStageEtsTest") { 970 hap_profile = "entry/src/main/module.json" 971 deps = [ 972 ":actbmsstageetstest_js_assets", 973 ":actbmsstageetstest_resources", 974 ] 975 ets2abc = true 976 certificate_profile = "signature/openharmony_sx.p7b" 977 hap_name = "ActsBundleMgrStageEtsTest" 978 subsystem_name = "developertest" 979 part_name = "stage_test" 980 module_out_path = want_output_path 981 } 982 ohos_app_scope("actbmsstageetstest_app_profile") { 983 app_profile = "AppScope/app.json" 984 sources = [ "AppScope/resources" ] 985 } 986 ohos_js_assets("actbmsstageetstest_js_assets") { 987 source_dir = "entry/src/main/ets" 988 } 989 ohos_resources("actbmsstageetstest_resources") { 990 sources = [ "entry/src/main/resources" ] 991 deps = [ ":actbmsstageetstest_app_profile" ] 992 hap_profile = "entry/src/main/module.json" 993 } 994 group("unittest") { 995 testonly = true 996 deps = [] 997 deps += [ ":ActsBundleMgrStageEtsTest" ] 998 } 999 ``` 1000 详细内容如下: 1001 1002 1. 添加文件头注释信息 1003 1004 ``` 1005 # Copyright (C) 2023 XXXX Device Co., Ltd. 1006 ``` 1007 1008 2. 导入编译模板文件 1009 1010 ``` 1011 import("//build/test.gni") 1012 ``` 1013 1014 3. 指定文件输出路径 1015 1016 ``` 1017 want_output_path = "developertest/stage_test" 1018 ``` 1019 > **说明:** 1020 > 1021 > 此处输出路径为部件/模块名。 1022 1023 4. 指定测试用例编译目标输出的文件名称 1024 1025 ``` 1026 ohos_js_stage_unittest("ActsBundleMgrStageEtsTest") { 1027 } 1028 ``` 1029 > **说明:** 1030 > 1031 > 使用模板ohos_js_stage_unittest定义stage模型的ArkTs测试套。 1032 1033 5. 指定配置文件module.json、签名文件、部件名称和编译输出路径,都为必选项。 1034 1035 ``` 1036 ohos_js_stage_unittest("ActsBundleMgrStageEtsTest") { 1037 hap_profile = "entry/src/main/module.json" 1038 certificate_profile = "signature/openharmony_sx.p7b" 1039 subsystem_name = "developertest" 1040 part_name = "stage_test" // 部件名称 1041 } 1042 ``` 1043 1044 6. 指定配置资源文件(添加需要参与编译的源文件、配置和依赖) 1045 ``` 1046 # 声明一个HAP的AppScope模块,该目标的app_profile和sources会在编译时拼接到具体的entry内编译。 1047 ohos_app_scope("actbmsstageetstest_app_profile") { 1048 app_profile = "AppScope/app.json" 1049 sources = [ "AppScope/resources" ] 1050 } 1051 1052 # stage模型用例代码分别放置到ets目录下。 1053 ohos_js_assets("actbmsstageetstest_js_assets") { 1054 source_dir = "entry/src/main/ets" 1055 } 1056 1057 # 源文件,Stage模型编译后放置在resources目录下。 1058 ohos_resources("actbmsstageetstest_resources") { 1059 sources = [ "entry/src/main/resources" ] 1060 deps = [ ":actbmsstageetstest_app_profile" ] 1061 hap_profile = "entry/src/main/module.json" 1062 } 1063 1064 ``` 1065 1066 7. 对目标测试用例文件进行条件分组 1067 1068 ``` 1069 group("unittest") { 1070 testonly = true 1071 deps = [ ":GetAppInfoJsTest" ] 1072 } 1073 ``` 1074 > **说明:** 1075 > 1076 > 进行条件分组的目的在于执行用例时可以选择性地执行某一种特定类型的用例。 1077 1078- **Rust用例编译配置示例** 1079 1080 ``` 1081 #Copyright (C) 2023 XXXX Device Co., Ltd. 1082 1083 import("//build/ohos.gni") 1084 import("//build/test.gni") 1085 1086 module_output_path = "developer_test/rust_add_test" 1087 ohos_rust_unittest("rust_add_test") { 1088 module_out_path = module_output_path 1089 sources = [ 1090 "main.rs", 1091 "rust_add_test.rs", 1092 ... 1093 } 1094 deps = [ ":rust_st_add" ] 1095 } 1096 1097 group("unittest") { 1098 testonly = true 1099 deps = [ ":rust_add_test" ] 1100 } 1101 ``` 1102 1103 详细内容如下: 1104 1105 1. 添加文件头注释信息 1106 1107 ``` 1108 #Copyright (C) 2023 XXXX Device Co., Ltd. 1109 ``` 1110 2. 导入编译模板文件 1111 1112 ``` 1113 import("//build/ohos.gni") 1114 import("//build/test.gni") 1115 ``` 1116 3. 指定文件输出路径 1117 1118 ``` 1119 module_output_path = "developer_test/rust_add_test" 1120 ``` 1121 > **说明:** 1122 > 此处输出路径为部件/模块名。 1123 1124 4. 指定测试用例编译目标输出的文件名称 1125 1126 ``` 1127 ohos_rust_unittest("rust_add_test") { 1128 } 1129 ``` 1130 > **说明:** 1131 > - 使用模板ohos_rust_unittest定义rust的unittest测试套,注意与C++用例区分。 1132 > - 测试套名称必须以rust_开头。 1133 > - 还可以使用模板ohos_rust_systemtest定义rust的systemtest测试套。 1134 1135**编译入口配置文件bundle.json** 1136 1137当完成用例编译配置文件编写后,需要进一步编写部件编译配置文件,以关联到具体的测试用例。 1138``` 1139"build": { 1140 "sub_component": [ 1141 "//test/testfwk/developer_test/examples/app_info:app_info", 1142 "//test/testfwk/developer_test/examples/detector:detector", 1143 "//test/testfwk/developer_test/examples/calculator:calculator" 1144 ], 1145 "inner_list": [ 1146 { 1147 "header": { 1148 "header_base": "////test/testfwk/developer_test/examples/detector/include", 1149 "header_files": [ 1150 "detector.h" 1151 ] 1152 }, 1153 "name": "//test/testfwk/developer_test/examples/detector:detector" 1154 } 1155 ], 1156 "test": [ //配置模块calculator下的test 1157 "//test/testfwk/developer_test/examples/app_info/test:unittest", 1158 "//test/testfwk/developer_test/examples/calculator/test:unittest", 1159 "//test/testfwk/developer_test/examples/calculator/test:fuzztest" 1160 } 1161``` 1162> **说明:** 1163> 1164> test_list中配置的是对应模块的测试用例。 1165 1166## 配置测试资源 1167 1168测试依赖资源主要包括测试用例在执行过程中需要的图片文件、视频文件、第三方库等对外的文件资源,目前只支持静态资源的配置。 1169 1170依赖资源文件配置步骤如下: 1171 11721. 在部件的test目录下创建resource目录,在resource目录下创建对应的模块,在模块目录中存放该模块所需要的资源文件。 1173 11742. 在resource目录下对应的模块目录中创建一个ohos_test.xml文件,文件内容格式如下: 1175 1176 ```xml 1177 <?xml version="1.0" encoding="UTF-8"?> 1178 <configuration ver="2.0"> 1179 <target name="CalculatorSubTest"> 1180 <preparer> 1181 <option name="push" value="test.jpg -> /data/test/resource" src="res"/> 1182 <option name="push" value="libc++.z.so -> /data/test/resource" src="out"/> 1183 </preparer> 1184 </target> 1185 </configuration> 1186 ``` 1187 1188 3. 在测试用例的编译配置文件中定义resource_config_file进行指引,用来指定对应的资源文件ohos_test.xml。 1189 1190 ``` 1191 ohos_unittest("CalculatorSubTest") { 1192 resource_config_file = "//system/subsystem/partA/test/resource/calculator/ohos_test.xml" 1193 } 1194 ``` 1195 > **说明:** 1196 > 1197 >- target_name: 测试套的名称,定义在测试目录的BUILD.gn中。preparer: 表示该测试套执行前执行的动作。 1198 >- src="res": 表示测试资源位于test目录下的resource目录下,src="out":表示位于out/release/$(部件)目录下。 1199 1200## 执行测试用例 1201 1202### user_config.xml配置文件介绍 1203 1204在执行测试用例之前,针对用例使用设备的不同,需要对相应配置进行修改,配置文件路径为developer_test\config\user_config.xml,修改完成即可执行测试用例。 1205 1206```xml 1207<user_config> 1208 <build> 1209 <!-- 是否编译demo用例,默认为false,如果需要编译demo可修改为true --> 1210 <example>false</example> 1211 <!-- 是否编译版本,默认为false --> 1212 <version>false</version> 1213 <!-- 是否编译测试用例,默认为true,若已完成编译,再执行用例之前可修改为false,防止重新编译 --> 1214 <testcase>true</testcase> 1215 <!-- 在编译测试用例的情况下,选择编译target_cpu是64位的还是32位的,默认为空(32bit)可以选择: arm64 --> 1216 <parameter> 1217 <target_cpu></target_cpu> 1218 </parameter> 1219 </build> 1220 <environment> 1221 <!-- 配置远程映射机器的IP及端口,以支持HDC连接的设备 --> 1222 <device type="usb-hdc"> 1223 <ip></ip> 1224 <port></port> 1225 <sn></sn> 1226 </device> 1227 <!-- 配置设备的串口信息,以支持串口连接的设备 --> 1228 <device type="com" label="ipcamera"> 1229 <serial> 1230 <com></com> 1231 <type>cmd</type> 1232 <baud_rate>115200</baud_rate> 1233 <data_bits>8</data_bits> 1234 <stop_bits>1</stop_bits> 1235 <timeout>1</timeout> 1236 </serial> 1237 </device> 1238 </environment> 1239 <!-- 配置测试用例路径,若测试用例未编译,即<testcase>标签属性为true时,此处默认不填写;若编译已完成,需在此处指定测试用例的实际路径 --> 1240 <test_cases> 1241 <dir></dir> 1242 </test_cases> 1243 <!-- 配置覆盖率编译路径 --> 1244 <coverage> 1245 <outpath></outpath> 1246 </coverage> 1247 <!-- NFS挂载信息配置,被测设备仅支持串口连接时配置,指定NFS的映射路径,host_dir为PC侧的NFS目录,board_dir为板侧创建的目录 --> 1248 <NFS> 1249 <host_dir></host_dir> 1250 <mnt_cmd></mnt_cmd> 1251 <board_dir></board_dir> 1252 </NFS> 1253</user_config> 1254``` 1255> **说明:** 1256> 1257> 在执行测试用例之前,若使用HDC连接设备,用例仅需配置设备IP和端口号即可,其余信息均默认不修改。 1258 1259### Windows环境执行 1260#### 编译测试用例 1261 1262由于Windows环境下无法实现用例编译,因此执行用例前需要在Linux环境下进行用例编译,用例编译命令: 1263``` 1264./build.sh --product-name {product_name} --build-target make_test 1265``` 1266 1267> **说明:** 1268> 1269>- product-name:指定编译产品名称。 1270>- build-target:指定所需编译用例,make_test表示指定全部用例,实际开发中可指定特定用例。 1271 1272编译完成后,测试用例将自动保存在out/ohos-arm-release/packages/phone/tests目录下。 1273 1274#### 搭建执行环境 12751. 在Windows环境创建测试框架目录Test,并在此目录下创建testcase目录。 1276 12772. 从Linux环境拷贝测试框架developertest和xdevice到创建的Test目录下,拷贝编译好的测试用例到testcase目录下。 1278 1279 > **说明:** 1280 > 1281 > 将测试框架及测试用例从Linux环境移植到Windows环境,以便后续执行。 1282 12833. 修改user_config.xml 1284 ```xml 1285 <build> 1286 <!-- 由于测试用例已编译完成,此标签属性需改为false --> 1287 <testcase>false</testcase> 1288 </build> 1289 <test_cases> 1290 <!-- 由于已将测试用例拷贝到Windows环境下,测试用例输出路径发生改变,需要修改为拷贝后所存放的路径 --> 1291 <dir>D:\Test\testcase\tests</dir> 1292 </test_cases> 1293 ``` 1294 > **说明:** 1295 > 1296 > `<testcase>`标签表示是否需要编译用例;`<dir>`标签表示测试用例查找路径。 1297 1298#### 执行用例命令 1299 13001. 启动测试框架 1301 ``` 1302 start.bat 1303 ``` 13042. 选择产品形态 1305 1306 进入测试框架,系统会自动提示您选择产品形态,请根据实际的开发板进行选择。 1307 1308 如需手动添加,请在config/framework_config.xml的\<productform\>标签内增加产品项。 1309 13103. 执行测试用例 1311 1312 当选择完产品形态,可参考如下指令执行TDD测试用例。 1313 ``` 1314 run -t UT 1315 run -t UT -tp PartName 1316 run -t UT -tp PartName -tm TestModuleName 1317 run -t UT -tp ability_base -ts base_object_test 1318 run -t UT -tp PartName -tm TestModuleName -ts CalculatorSubTest 1319 run -t UT -ts base_object_test 1320 run -t UT -ts base_object_test -tc AAFwkBaseObjectTest.BaseObject_test_001 1321 run -t UT -ts CalculatorSubTest -tc CalculatorSubTest.interger_sub_00l 1322 run -t UT -cov coverage 1323 run -t UT -ra random 1324 run -t UT -ts base_object_test --repeat 5 1325 run -hl 1326 run -rh 3 1327 run --retry 1328 ``` 1329 1330 1331 执行命令参数说明: 1332 ``` 1333 -t [TESTTYPE]: 指定测试用例类型,有UT,MST,ST,PERF,FUZZ,BENCHMARK,另外还有ACTS,HATS等。(必选参数) 1334 -tp [TESTPART]: 指定部件,可独立使用。 1335 -tm [TESTMODULE]: 指定模块,不可独立使用,需结合-tp指定上级部件使用。 1336 -ts [TESTSUITE]: 指定测试套,可独立使用。 1337 -tc [TESTCASE]: 指定测试用例,同时需要注明测试套内class名称,不可独立使用,需结合-ts指定上级测试套使用。 1338 -cov [COVERAGE]: 覆盖率执行参数。 1339 -h : 帮助命令 1340 -ra [random]: c++用例乱序执行参数。 1341 --repeat : 支持设置用例执行次数。 1342 -hl [HISTORYLIST]: 显示最近10条测试用例,超过10条,只显示最近10条。 1343 -rh [RUNHISTORY]: 执行历史记录的第几条记录运行。 1344 --retry:检查上次运行结果,如果有失败用例则重复测试。 1345 ``` 1346 1347 1348### Linux环境执行 1349 1350 1351#### 映射远程端口及修改配置文件 1352为支持Linux远程服务器以及Linux虚拟机两种环境下执行测试用例,需要对端口进行远程映射,以实现与设备的数据通路连接。具体操作如下: 13531. HDC Server指令: 1354 ``` 1355 hdc_std kill 1356 hdc_std -m -s 0.0.0.0:8710 1357 ``` 1358 > **说明:** 1359 > 1360 > IP和端口号为默认值。 1361 13622. HDC Client指令: 1363 ``` 1364 hdc_std -s xx.xx.xx.xx:8710 list targets 1365 ``` 1366 > **说明:** 1367 > 1368 > 此处IP填写设备侧IP地址。 1369 13703. 修改配置文件user_config.xml 1371 ```xml 1372 <build> 1373 <!-- 测试用例需要编译时,此标签属性为true,反之为false --> 1374 <testcase>true</testcase> 1375 </build> 1376 <environment> 1377 <!-- 需要配置IP为远程映射机器的IP,端口即为8710,sn号为设备的sn号,以支持HDC连接的设备 --> 1378 <device type="usb-hdc"> 1379 <ip></ip> 1380 <port></port> 1381 <sn></sn> 1382 </device> 1383 <environment> 1384 ``` 1385 1386 1387#### 执行用例命令 13881. 启动测试框架 1389 ``` 1390 ./start.sh 1391 ``` 13922. 选择产品形态 1393 1394 进入测试框架,系统会自动提示您选择产品形态,请根据实际的开发板进行选择。 1395 1396 若需要自测试框架编译测试用例,且没有找到需要的产品形态需手动添加,请在config/framework_config.xml的\<productform\>标签内增加产品项。 1397 1398 ``` 1399 <framework_config> 1400 <productform> 1401 <option name="ipcamera_hispark_aries" /> 1402 <option name="ipcamera_hispark_taurus" /> 1403 <option name="wifiiot_hispark_pegasus" /> 1404 <option name="" /> 1405 </productform> 1406 </framework_config> 1407 1408 ``` 1409 14103. 执行测试用例 1411 1412 1) TDD命令 1413 1414 测试框架在执行用例时会根据指令找到所需用例,自动实现用例编译,执行过程,完成自动化测试。 1415 ``` 1416 run -t UT 1417 run -t UT -tp PartName 1418 run -t UT -tp PartName -tm TestModuleName 1419 run -t UT -tp ability_base -ts base_object_test 1420 run -t UT -tp PartName -tm TestModuleName -ts CalculatorSubTest 1421 run -t UT -ts base_object_test 1422 run -t UT -ts base_object_test -tc AAFwkBaseObjectTest.BaseObject_test_001 1423 run -t UT -ts CalculatorSubTest -tc CalculatorSubTest.interger_sub_00l 1424 run -t -cov coverage 1425 run -t UT -ra random 1426 run -t UT -tp PartName -pd partdeps 1427 run -t UT -ts base_object_test --repeat 5 1428 run -hl 1429 run -rh 3 1430 run --retry 1431 ``` 1432 执行命令参数说明: 1433 ``` 1434 -t [TESTTYPE]: 指定测试用例类型(必选参数),有UT,MST,ST,PERF,FUZZ,BENCHMARK等。 1435 -tp [TESTPART]: 指定部件,可独立使用。 1436 -tm [TESTMODULE]: 指定模块,不可独立使用,需结合-tp指定上级部件使用。 1437 -ts [TESTSUITE]: 指定测试套,可独立使用。 1438 -tc [TESTCASE]: 指定测试用例,同时需要注明测试套内class名称,不可独立使用,需结合-ts指定上级测试套使用。 1439 -cov [COVERAGE]: 覆盖率执行参数。 1440 -h : 帮助命令 1441 -ra [random]: c++用例乱序执行参数 1442 -pd [partdeps]: 二级依赖部件执行参数 1443 --repeat : 支持设置用例执行次数。 1444 -hl [HISTORYLIST]: 显示最近10条测试用例,超过10条,只显示最近10条。 1445 -rh [RUNHISTORY]: 执行历史记录的第几条记录运行。 1446 --retry:检查上次运行结果,如果有失败用例则重复测试。 1447 ``` 1448 1449 在Linux下可以使用help命令查看有哪些产品形态、测试类型、支持的子系统、部件。 1450 ``` 1451 查看帮助命令:help 1452 查看show命令:help show 1453 查看支持的设备形态: show productlist 1454 查看支持的测试类型: show typelist 1455 查看支持的测试子系统: show subsystemlist 1456 查看支持的测试部件: show partlist 1457 ``` 1458 2) ACTS/HATS命令 1459 1460 当选择完产品形态,可以参考如下执行ACTS或HATS测试用例。 1461 ``` 1462 run -t ACTS 1463 run -t HATS 1464 run -t ACTS -ss arkui 1465 run -t ACTS -ss arkui, modulemanager 1466 run -t ACTS -ss arkui -ts ActsAceEtsTest 1467 run -t HATS -ss telephony -ts HatsHdfV1RilServiceTest 1468 run -t ACTS -ss arkui -tp ActsPartName 1469 run -t ACTS -ss arkui -ts ActsAceEtsTest,ActsAceEtsResultTest 1470 run -t HATS -ss powermgr -ts HatsPowermgrBatteryTest,HatsPowermgrThermalTest 1471 run -t ACTS -ss arkui -ts ActsAceEtsTest -ta class:alphabetIndexerTest#alphabetIndexerTest001 1472 run -t ACTS -ss arkui -ts ActsAceEtsTest -ta class:alphabetIndexerTest#alphabetIndexerTest001 --repeat 2 1473 run -hl 1474 run -rh 1 1475 run --retry 1476 ``` 1477 执行命令参数说明,ACTS和HATS命令参数一致,与TDD有所不同: 1478 ``` 1479 -t [TESTTYPE]: 指定测试用例类型(必选参数),有ACTS,HATS等。 1480 -ss [SUBSYSTEM]: 指定子系统,可单独使用,且可以执行多个子系统,用逗号隔开。 1481 -tp [TESTPART]: 指定部件,可独立使用。 1482 -ts [TESTSUITE]: 指定测试套,可独立使用,且可以执行多个测试套,用逗号隔开。 1483 -ta [TESTARGS]: 指定测试类测试方法,需结合-ts指定上级测试套使用。 1484 --repeat : 支持设置用例执行次数。 1485 -hl [HISTORYLIST]: 显示最近10条测试用例,超过10条,只显示最近10条。 1486 -rh [RUNHISTORY]: 执行历史记录的第几条记录运行。 1487 --retry:检查上次运行结果,如果有失败用例则重复测试。 1488 ``` 1489 1490## 查看测试结果 1491 1492 1493当执行完测试指令,控制台会自动生成测试结果,若需要详细测试报告,您可在以下路径中进行查找相应的数据文档。 1494 1495测试结果输出根路径如下: 1496``` 1497test/developertest/reports/xxxx_xx_xx_xx_xx_xx 1498``` 1499> **说明:** 1500> 1501> 测试报告文件目录将自动生成。 1502 1503该目录中包含以下几类结果: 1504| 类型 | 描述 | 1505| ------------------------------------ | ------------------ | 1506| result/ | 测试用例格式化结果 | 1507| log/plan_log_xxxx_xx_xx_xx_xx_xx.log | 测试用例日志 | 1508| summary_report.html | 测试报告汇总 | 1509| details_report.html | 测试报告详情 | 1510 1511 1512 1513## 执行覆盖率用例 1514各子系统可按照以下方式,执行用例,生成gcda数据后方可生成覆盖率报告。 1515 15161. (可选执行)为了屏蔽非核心代码产生的冗余分支数据,可以在源码编译之前进入/test/testfwk/developer_test/localCoverage/restore_comment目录下执行: 1517 1518 python3 build_before_generate.py 1519 1520 选择编译时要屏蔽的部件,执行命令例如: 1521 1522 run -tp partname 1523 run -tp partname1 partname2 15242. 编译版本之前首先修改编译选项,在涉及到的子系统build.gn文件中添加cflags或cflags_cc及ldflags选项,都需要加--coverage字段: 1525 1526 ldflags = [ "--coverage" ] 1527 C: cflags = [ "--coverage" ] 1528 C++: cflags_cc = [ "--coverage" ] 1529 1530 **推荐:** 也可以参考窗口子系统的方式(推荐这种方式),参考链接:https://gitee.com/openharmony/window_window_manager/pulls/1274/files 15313. 执行覆盖率用例需要安装以下依赖包: 1532 1533 1)安装lcov, 安装命令:sudo apt install lcov 1534 2)安装dos2unix, 安装命令:apt install dos2unix. 1535 3)安装lxml, 安装命令: pip install lxml 1536 4)安装selectolax, 安装命令: pip install selectolax 1537 5)安装CppHeaderParser, 安装命令 pip install CppHeaderParser 1538 15394. 远程映射设备,修改usr_config.xml中的ip号,设备映射方式查看上面介绍的远程端口映射。 1540 1541 <!-- 配置远程映射机器的IP(设备挂载的pc的ip) --> 1542 <device type="usb-hdc"> 1543 <ip></ip> 1544 <port></port> 1545 <sn></sn> 1546 </device> 1547 15485. 执行 ./start.sh,输入运行命令,示例如下: 1549 1550 run -t UT -tp 部件名 -cov coverage 1551 run -t UT -ss 子系统名 -cov coverage 1552 run -t UT -ss 子系统名 -tp 部件名 -cov coverage 1553 run -t UT MST ST -tp 部件名 -cov coverage 1554 1555 **注意:** 执行以上命令必须添加 -cov coverage 参数 1556 15576. 从以下路径获取覆盖率报告: 1558 1559 代码覆盖率报告:/test/testfwk/developer_test/localCoverage/codeCoverage/results/coverage/reports/cxx/html 1560 1561 接口覆盖率报告:/test/testfwk/developer_test/localCoverage/interfaceCoverage/results/coverage/interface_kits/html 1562