README.md
1# Developers Test<a name="EN-US_TOPIC_0000001122338419"></a>
2
3- [Overview](#section7375710115617)
4- [Directory Structure](#section102031353175317)
5- [Constraints](#section87444710110)
6- [Installation](#section1347156474)
7- [Using Test Framework](#section75882026185016)
8- [Test Result and Log](#section414715805819)
9- [Repositories Involved](#section6299103515474)
10
11## Overview<a name="section7375710115617"></a>
12
13This module allows you to develop new test cases for new features, or modify existing test cases for modified features. The developers test framework helps you develop high-quality code.
14
15## Directory Structure<a name="section102031353175317"></a>
16
17```
18developertest/
19├── aw # Static libraries of the test framework
20│ ├── cxx # C++ libraries
21│ └── python # Python libraries
22├── config # Test framework configuration
23│ ├── build_config.xml # Test case build configuration
24│ ├── filter_config.xml # Test case filter configuration
25│ ├── framework_config.xml # Test type configuration
26│ └── user_config.xml # User configuration
27├── examples # Test case examples
28│ ├── calculator # Calculator
29│ └── test # Test resources
30├── src # Source code of the test framework
31│ ├── main # Entry function
32│ └── core # Core code of the test framework
33├── third_party # Adaptation code for third-party components on which the test framework relies
34│ └── lib # Compilation configuration of the static library
35├── BUILD.gn # Compilation entry of the test framework
36├── start.bat # Developers test entry for Windows
37└── start.sh # Developers test entry for Linux
38```
39
40## Constraints<a name="section87444710110"></a>
41
42The test tool environment must meet the following requirements:
43
441. Python version: 3.7.5 or later
452. Paramiko version: 2.7.1 or later
463. Setuptools version: 40.8.0 or later
474. RSA version: 4.0 or later
485. NFS version: V4 or later \(required when device supports connection using the serial port but not the hdc\)
496. pySerial version: 3.3 or later \(required when the device supports connection using the serial port but not the hdc\)
507. OS version: Windows 10 or later; Ubuntu 18.04
51
52## Installation<a name="section1347156474"></a>
53
54The Python environment is required.
55
561. Run the following command to install the Linux extension component Readline:
57
58 ```
59 sudo apt-get install libreadline-dev
60 ```
61
62 ```
63 Reading package lists... Done
64 Building dependency tree
65 Reading state information... Done
66 libreadline-dev is already the newest version (7.0-3).
67 0 upgraded, 0 newly installed, 0 to remove and 11 not upgraded.
68 ```
69
702. Run the following command to install the plug-in Setuptools:
71
72 ```
73 pip3 install setuptools
74 ```
75
76 If the installation is successful, the following prompts are displayed:
77
78 ```
79 Requirement already satisfied: setuptools in d:\programs\python37\lib\site-packages (41.2.0)
80 ```
81
823. Run the following command to install the plug-in Paramiko:
83
84 ```
85 pip3 install paramiko
86 ```
87
88 If the installation is successful, the following prompts are displayed:
89
90 ```
91 Installing collected packages: pycparser, cffi, pynacl, bcrypt, cryptography, paramiko
92 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
93 ```
94
954. Run the following command to install the Python plug-in RSA:
96
97 ```
98 pip3 install rsa
99 ```
100
101 If the installation is successful, the following prompts are displayed:
102
103 ```
104 Installing collected packages: pyasn1, rsa
105 Successfully installed pyasn1-0.4.8 rsa-4.7
106 ```
107
1085. Run the following command to install the serial port plug-in pySerial for Python on the local PC:
109
110 ```
111 pip3 install pyserial
112 ```
113
114 If the installation is successful, the following prompts are displayed:
115
116 ```
117 Requirement already satisfied: pyserial in d:\programs\python37\lib\site-packages\pyserial-3.4-py3.7.egg (3.4)
118 ```
119
1206. If the device supports test result output only using the serial port, install the NFS server.
121
122 For example, to install haneWIN NFS Server 1.2.50 for Windows, download the installation package from https://www.hanewin.net/nfs-e.htm.
123
124 For Linux, run the following command:
125
126 ```
127 sudo apt install nfs-kernel-server
128 ```
129
130 If the installation is successful, the following prompts are displayed:
131
132 ```
133 Reading package lists... Done
134 Building dependency tree
135 Reading state information... Done
136 nfs-kernel-server is already the newest version (1:1.3.4-2.1ubuntu5.3).
137 0 upgraded, 0 newly installed, 0 to remove and 11 not upgraded.
138 ```
139
140
141## Using Test Framework<a name="section75882026185016"></a>
142
143- \(Optional\) Install the XDevice module.
144 1. Go to the XDevice installation directory **test/xdevice**.
145 2. Open the console and run the following command:
146
147 ```
148 python setup.py install
149 ```
150
151 If the installation is successful, the following prompts are displayed:
152
153 ```
154 Installed d:\programs\python37\lib\site-packages\xdevice-0.0.0-py3.7.egg
155 Processing dependencies for xdevice==0.0.0
156 Finished processing dependencies for xdevice==0.0.0
157 ```
158
159
160- Configure the developers test module in the configuration file **developertest/config/user\_config.xml**.
161 1. Modify basic configuration parameters of the test framework.
162
163 \[build\] \# Configure build parameters of the test case.
164
165 ```
166 <build>
167 <example>false</example>
168 <version>false</version>
169 <testcase>true</testcase>
170 ...
171 </build>
172 ```
173
174 > **NOTE:**
175 >**example**: whether to build the test case example. The default value is **false**.
176 >**version**: whether to build the test version. The default value is **false**.
177 >**testcase**: whether to build the test case. The default value is **true**.
178
179 2. For devices that support the Harmony device connector \(hdc\), modify the configuration file as follows:
180
181 \[device\] \# Configure the device information with the **"usb-hdc"** attribute, including the test device IP address and the matched hdc port.
182
183 ```
184 <device type="usb-hdc">
185 <ip>192.168.1.1</ip>
186 <port>9111</port>
187 <sn></sn>
188 </device>
189 ```
190
191 3. For devices that only support the serial port connection, modify the configuration file as follows:
192
193 \[device\] \# Configure the serial port information with the **"ipcamera"** attribute, including the COM port and baud rate.
194
195 ```
196 <device type="com" label="ipcamera">
197 <serial>
198 <com>COM1</com>
199 <type>cmd</type>
200 <baud_rate>115200</baud_rate>
201 <data_bits>8</data_bits>
202 <stop_bits>1</stop_bits>
203 <timeout>1</timeout>
204 </serial>
205 </device>
206 ```
207
208
209- \(Optional\) Modify the configuration file **config/user\_config.xml** of the **developertest** module. If a test case has been compiled, specify the compilation output directory of the test case. In this case, the test platform will not recompile the test case.
210 1. Specify the output directory of the test case, that is, the compilation output directory between the **<test\_cases\>** tags.
211
212 ```
213 <test_cases>
214 <dir>/home/opencode/out/release/tests</dir>
215 </test_cases>
216 ```
217
218 2. For devices that only support serial port connection, specify the NFS directory on the PC \(**host\_dir**\) and the corresponding directory on the board \(**board\_dir**\) between the **<NFS\>** tags. For example:
219
220 ```
221 <NFS>
222 <host_dir>D:\nfs</host_dir>
223 <board_dir>user</board_dir>
224 </NFS>
225 ```
226
227
228- Prepare the test environment. \(Check that the test environment is ready if the tested device only supports serial port connection.\)
229 - The system image and file system have been burnt into a development board and are running properly on the development board. For example, in system mode, device prompt **OHOS\#** is displayed during shell login, indicating that the system is running properly.
230 - The development host has been connected to the serial port of the development board and the network port.
231 - The IP addresses of the development host and development board are in the same network segment and can ping each other.
232 - An empty directory is created on the development host for mounting test cases through the NFS, and the NFS service is started properly.
233
234- Run test suites.
235 - Start the test framework and go to the **test/developertest** directory.
236 1. Run the following command to start the test framework in Windows.
237
238 ```
239 start.bat
240 ```
241
242 2. Run the following command to start the test framework in Linux.
243
244 ```
245 ./strat.sh
246 ```
247
248
249 - Select a device form.
250
251 Configure device forms based on the actual development board, for example, **developertest/config/framework\_config.xml**.
252
253 - Run test commands.
254 1. To query the subsystems, modules, product forms, and test types supported by test cases, run the **show** commands.
255
256 ```
257 usage:
258 show productlist Querying Supported Product Forms
259 show typelist Querying the Supported Test Type
260 show subsystemlist Querying Supported Subsystems
261 show modulelist Querying Supported Modules
262 ```
263
264 2. Run the execution command, where **-t** is mandatory, and **-ss** and **-tm** are optional.
265
266 ```
267 run -t ut -ss test -tm example
268 ```
269
270 3. Specify the parameters that can be used to execute the test suite corresponding to a specific feature or module.
271
272 ```
273 usage: run [-h] [-p PRODUCTFORM] [-t [TESTTYPE [TESTTYPE ...]]]
274 [-ss SUBSYSTEM] [-tm TESTMODULE] [-ts TESTSUIT]
275 [-tc TESTCASE] [-tl TESTLEVEL]
276
277 optional arguments:
278 -h, --help show this help message and exit
279 -p PRODUCTFORM, --productform PRODUCTFORM Specified product form
280 -t [TESTTYPE [TESTTYPE ...]], --testtype [TESTTYPE [TESTTYPE ...]]
281 Specify test type(UT,MST,ST,PERF,ALL)
282 -ss SUBSYSTEM, --subsystem SUBSYSTEM Specify test subsystem
283 -tm TESTMODULE, --testmodule TESTMODULE Specified test module
284 -ts TESTSUIT, --testsuite TESTSUIT Specify test suite
285 -tc TESTCASE, --testcase TESTCASE Specify test case
286 -tl TESTLEVEL, --testlevel TESTLEVEL Specify test level
287 ```
288
289
290
291- See the test framework help if needed.
292 - Run the following command to query commands supported by the test platform:
293
294 ```
295 help
296 ```
297
298
299
300- Exit the self-test platform.
301 - Run the following command to exit the test platform:
302
303 ```
304 quit
305 ```
306
307
308
309## Test Result and Log<a name="section414715805819"></a>
310
311- Test logs and test reports are generated after you execute the test commands.
312- Test result
313 - Reports are displayed on the console. The root path of the test result is as follows:
314
315 ```
316 reports/xxxx-xx-xx-xx-xx-xx
317 ```
318
319 - Test case formatting result
320
321 ```
322 result/
323 ```
324
325 - Test case log
326
327 ```
328 log/plan_log_xxxx-xx-xx-xx-xx-xx.log
329 ```
330
331 - Report summary
332
333 ```
334 summary_report.html
335 ```
336
337 - Test report details
338
339 ```
340 details_report.html
341 ```
342
343
344- Test framework log
345
346 ```
347 reports/platform_log_xxxx-xx-xx-xx-xx-xx.log
348 ```
349
350- Latest test reports
351
352 ```
353 reports/latest
354 ```
355
356
357## Repositories Involved<a name="section6299103515474"></a>
358
359[testing subsystem](https://gitee.com/openharmony/docs/blob/master/en/readme/testing-subsystem.md)
360
361**test\_developertest**
362
363[test\_xdevice](https://gitee.com/openharmony/test_xdevice/blob/master/README.md)
364
README_zh.md
1# 测试子系统
2OpenHarmony为开发者提供了一套全面的自测试框架,开发者可根据测试需求开发相关测试用例,开发阶段提前发现缺陷,大幅提高代码质量。
3
4本文从基础环境构建,用例开发,编译以及执行等方面介绍OpenHarmony测试框架如何运行和使用。
5## 基础环境构建
6测试框架依赖于python运行环境,在使用测试框架之前可参阅以下方式进行配置。
7 - [环境配置](https://gitee.com/openharmony/docs/tree/master/zh-cn/device-dev/subsystems/subsys-testguide-envbuild.md)
8 - [源码获取](https://gitee.com/openharmony/docs/blob/master/zh-cn/device-dev/get-code/sourcecode-acquire.md)
9
10
11## 测试框架目录简介
12以下是测试框架的目录层级架构,在使用测试框架过程中可在相应目录查找对应组件。
13```
14test # 测试子系统
15├── developertest # 开发者测试组件
16│ ├── aw # 测试框架的静态库
17│ ├── config # 测试框架配置
18│ │ │ ...
19│ │ └── user_config.xml # 用户使用配置
20│ ├── examples # 测试用例示例
21│ ├── src # 测试框架源码
22│ ├── third_party # 测试框架依赖第三方组件适配
23│ ├── reports # 测试结果报告
24│ ├── BUILD.gn # 测试框架编译入口
25│ ├── start.bat # 开发者测试入口(Windows)
26│ └── start.sh # 开发者测试入口(Linux)
27└── xdevice # 测试框架依赖组件
28```
29## 测试用例编写
30### 测试用例目录规划
31使用测试框架过程中,可根据以下层级关系规划测试用例目录。
32```
33subsystem # 子系统
34├── partA # 部件A
35│ ├── moduleA # 模块A
36│ │ ├── include
37│ │ ├── src # 业务代码
38│ │ └── test # 测试目录
39│ │ ├── unittest # 单元测试
40│ │ │ ├── common # 公共用例
41│ │ │ │ ├── BUILD.gn # 测试用例编译配置
42│ │ │ │ └── testA_test.cpp # 单元测试用例源码
43│ │ │ ├── phone # 手机形态用例
44│ │ │ ├── ivi # 车机形态用例
45│ │ │ └── liteos-a # ipcamera使用liteos内核的用例
46│ │ ├── moduletest # 模块测试
47│ │ ...
48│ │
49│ ├── moduleB # 模块B
50│ ├── test
51│ │ └── resource # 依赖资源
52│ │ ├── moduleA # 模块A
53│ │ │ ├── ohos_test.xml # 资源配置文件
54│ │ ... └── 1.txt # 资源
55│ │
56│ ├── ohos_build # 编译入口配置
57│ ...
58│
59...
60```
61> **注意:** 测试用例根据不同设备形态差异分为通用用例和非通用用例,建议将通用用例存放在common目录下,非通用用例存放在相应设备形态目录下。
62
63### 测试用例编写
64本测试框架支持多种类型测试,针对不同测试类型提供了不同的用例编写模板以供参考。
65
66**TDD测试(C++)**
67
68- 用例源文件命名规范
69
70 测试用例源文件名称和测试套内容保持一致,文件命名采用全小写+下划线方式命名,以test结尾,具体格式为:[功能]_[子功能]_test,子功能支持向下细分。
71示例:
72 ```
73 calculator_sub_test.cpp
74 ```
75
76- 用例示例
77 ```
78 /*
79 * Copyright (c) 2021 XXXX Device Co., Ltd.
80 * Licensed under the Apache License, Version 2.0 (the "License");
81 * you may not use this file except in compliance with the License.
82 * You may obtain a copy of the License at
83 *
84 * http://www.apache.org/licenses/LICENSE-2.0
85 *
86 * Unless required by applicable law or agreed to in writing, software
87 * distributed under the License is distributed on an "AS IS" BASIS,
88 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
89 * See the License for the specific language governing permissions and
90 * limitations under the License.
91 */
92
93 #include "calculator.h"
94 #include <gtest/gtest.h>
95
96 using namespace testing::ext;
97
98 class CalculatorSubTest : public testing::Test {
99 public:
100 static void SetUpTestCase(void);
101 static void TearDownTestCase(void);
102 void SetUp();
103 void TearDown();
104 };
105
106 void CalculatorSubTest::SetUpTestCase(void)
107 {
108 // input testsuit setup step,setup invoked before all testcases
109 }
110
111 void CalculatorSubTest::TearDownTestCase(void)
112 {
113 // input testsuit teardown step,teardown invoked after all testcases
114 }
115
116 void CalculatorSubTest::SetUp(void)
117 {
118 // input testcase setup step,setup invoked before each testcases
119 }
120
121 void CalculatorSubTest::TearDown(void)
122 {
123 // input testcase teardown step,teardown invoked after each testcases
124 }
125
126 /**
127 * @tc.name: integer_sub_001
128 * @tc.desc: Verify the sub function.
129 * @tc.type: FUNC
130 * @tc.require: Issue Number
131 */
132 HWTEST_F(CalculatorSubTest, integer_sub_001, TestSize.Level1)
133 {
134 // step 1:调用函数获取结果
135 int actual = Sub(4,0);
136
137 // Step 2:使用断言比较预期与实际结果
138 EXPECT_EQ(4, actual);
139 }
140 ```
141 详细内容介绍:
142 1. 添加测试用例文件头注释信息
143 ```
144 /*
145 * Copyright (c) 2021 XXXX Device Co., Ltd.
146 * Licensed under the Apache License, Version 2.0 (the "License");
147 * you may not use this file except in compliance with the License.
148 * You may obtain a copy of the License at
149 *
150 * http://www.apache.org/licenses/LICENSE-2.0
151 *
152 * Unless required by applicable law or agreed to in writing, software
153 * distributed under the License is distributed on an "AS IS" BASIS,
154 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
155 * See the License for the specific language governing permissions and
156 * limitations under the License.
157 */
158 ```
159 2. 引用测试框架头文件和命名空间
160 ```
161 #include <gtest/gtest.h>
162
163 using namespace testing::ext;
164 ```
165 3. 添加被测试类的头文件
166 ```
167 #include "calculator.h"
168 ```
169 4. 定义测试套(测试类)
170 ```
171 class CalculatorSubTest : public testing::Test {
172 public:
173 static void SetUpTestCase(void);
174 static void TearDownTestCase(void);
175 void SetUp();
176 void TearDown();
177 };
178
179 void CalculatorSubTest::SetUpTestCase(void)
180 {
181 // input testsuit setup step,setup invoked before all testcases
182 }
183
184 void CalculatorSubTest::TearDownTestCase(void)
185 {
186 // input testsuit teardown step,teardown invoked after all testcases
187 }
188
189 void CalculatorSubTest::SetUp(void)
190 {
191 // input testcase setup step,setup invoked before each testcases
192 }
193
194 void CalculatorSubTest::TearDown(void)
195 {
196 // input testcase teardown step,teardown invoked after each testcases
197 }
198 ```
199 > **注意:** 在定义测试套时,测试套名称应与编译目标保持一致,采用大驼峰风格。
200
201 5. 测试用例实现,包含用例注释和逻辑实现
202 ```
203 /**
204 * @tc.name: integer_sub_001
205 * @tc.desc: Verify the sub function.
206 * @tc.type: FUNC
207 * @tc.require: Issue Number
208 */
209 HWTEST_F(CalculatorSubTest, integer_sub_001, TestSize.Level1)
210 {
211 //step 1:调用函数获取结果
212 int actual = Sub(4,0);
213
214 //Step 2:使用断言比较预期与实际结果
215 EXPECT_EQ(4, actual);
216 }
217 ```
218 在编写用例时,我们提供了三种用例模板供您选择。
219
220 | 类型 | 描述 |
221 | ------------| ------------|
222 | HWTEST(A,B,C)| 用例执行不依赖Setup&Teardown时,可选取|
223 | HWTEST_F(A,B,C)| 用例执行(不含参数)依赖于Setup&Teardown时,可选取|
224 | HWTEST_P(A,B,C)| 用例执行(含参数)依赖于Set&Teardown时,可选取|
225
226 其中,参数A,B,C的含义如下:
227 - 参数A为测试套名。
228 - 参数B为测试用例名,其命名必须遵循[功能点]_[编号]的格式,编号为3位数字,从001开始。
229 - 参数C为测试用例等级,具体分为门禁level0 以及非门禁level1-level4共五个等级,其中非门禁level1-level4等级的具体选取规则为:测试用例功能越重要,level等级越低。
230
231 **注意:**
232 - 测试用例的预期结果必须有对应的断言。
233 - 测试用例必须填写用例等级。
234 - 测试体建议按照模板分步实现。
235 - 用例描述信息按照标准格式@tc.xxx value书写,注释信息必须包含用例名称,用例描述,用例类型,需求编号四项。其中用例测试类型@tc.type参数的选取,可参考下表。
236
237 | 测试类型名称|类型编码|
238 | ------------|------------|
239 |功能测试 |FUNC|
240 |性能测试 |PERF|
241 |可靠性测试 |RELI|
242 |安全测试 |SECU|
243 |模糊测试 |FUZZ|
244
245
246**TDD测试(JS)**
247
248
249- 用例源文件命名规范
250
251 测试用例原文件名称采用大驼峰风格,以TEST结尾,具体格式为:[功能][子功能]TEST,子功能支持向下细分。
252示例:
253 ```
254 AppInfoTest.js
255 ```
256
257- 用例示例
258 ```
259 /*
260 * Copyright (C) 2021 XXXX Device Co., Ltd.
261 * Licensed under the Apache License, Version 2.0 (the "License");
262 * you may not use this file except in compliance with the License.
263 * You may obtain a copy of the License at
264 *
265 * http://www.apache.org/licenses/LICENSE-2.0
266 *
267 * Unless required by applicable law or agreed to in writing, software
268 * distributed under the License is distributed on an "AS IS" BASIS,
269 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
270 * See the License for the specific language governing permissions and
271 * limitations under the License.
272 */
273 import app from '@system.app'
274
275 import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
276
277 describe("AppInfoTest", function () {
278 beforeAll(function() {
279 // input testsuit setup step,setup invoked before all testcases
280 console.info('beforeAll caled')
281 })
282
283 afterAll(function() {
284 // input testsuit teardown step,teardown invoked after all testcases
285 console.info('afterAll caled')
286 })
287
288 beforeEach(function() {
289 // input testcase setup step,setup invoked before each testcases
290 console.info('beforeEach caled')
291 })
292
293 afterEach(function() {
294 // input testcase teardown step,teardown invoked after each testcases
295 console.info('afterEach caled')
296 })
297
298 /*
299 * @tc.name:appInfoTest001
300 * @tc.desc:verify app info is not null
301 * @tc.type: FUNC
302 * @tc.require: Issue Number
303 */
304 it("appInfoTest001", 0, function () {
305 //step 1:调用函数获取结果
306 var info = app.getInfo()
307
308 //Step 2:使用断言比较预期与实际结果
309 expect(info != null).assertEqual(true)
310 })
311 })
312 ```
313 详细内容介绍:
314 1. 添加测试用例文件头注释信息
315 ```
316 /*
317 * Copyright (C) 2021 XXXX Device Co., Ltd.
318 * Licensed under the Apache License, Version 2.0 (the "License");
319 * you may not use this file except in compliance with the License.
320 * You may obtain a copy of the License at
321 *
322 * http://www.apache.org/licenses/LICENSE-2.0
323 *
324 * Unless required by applicable law or agreed to in writing, software
325 * distributed under the License is distributed on an "AS IS" BASIS,
326 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
327 * See the License for the specific language governing permissions and
328 * limitations under the License.
329 */
330 ```
331 2. 导入被测api和jsunit测试库
332 ```
333 import app from '@system.app'
334
335 import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
336 ```
337 3. 定义测试套(测试类)
338 ```
339 describe("AppInfoTest", function () {
340 beforeAll(function() {
341 // input testsuit setup step,setup invoked before all testcases
342 console.info('beforeAll caled')
343 })
344
345 afterAll(function() {
346 // input testsuit teardown step,teardown invoked after all testcases
347 console.info('afterAll caled')
348 })
349
350 beforeEach(function() {
351 // input testcase setup step,setup invoked before each testcases
352 console.info('beforeEach caled')
353 })
354
355 afterEach(function() {
356 // input testcase teardown step,teardown invoked after each testcases
357 console.info('afterEach caled')
358 })
359 ```
360 4. 测试用例实现
361 ```
362 /*
363 * @tc.name:appInfoTest001
364 * @tc.desc:verify app info is not null
365 * @tc.type: FUNC
366 * @tc.require: Issue Number
367 */
368 it("appInfoTest001", 0, function () {
369 //step 1:调用函数获取结果
370 var info = app.getInfo()
371
372 //Step 2:使用断言比较预期与实际结果
373 expect(info != null).assertEqual(true)
374 })
375 ```
376
377**Fuzz测试**
378
379[Fuzz用例编写规范](https://gitee.com/openharmony/test_developertest/blob/master/libs/fuzzlib/README_zh.md)
380
381
382**Benchmark测试**
383
384[Benchmark用例编写规范](https://gitee.com/openharmony/test_developertest/blob/master/libs/benchmark/README_zh.md)
385
386
387### 测试用例编译文件编写
388根据测试用例目录规划,当执行某一用例时,测试框架会根据编译文件逐层查找,最终找到所需用例进行编译。下面通过不同示例来讲解gn文件如何编写。
389
390#### TDD测试
391针对不同语言,下面提供不同的编译模板以供参考。
392
393- **C++用例编译配置示例**
394 ```
395 # Copyright (c) 2021 XXXX Device Co., Ltd.
396
397 import("//build/test.gni")
398
399 module_output_path = "developertest/calculator"
400
401 config("module_private_config") {
402 visibility = [ ":*" ]
403
404 include_dirs = [ "../../../include" ]
405 }
406
407 ohos_unittest("CalculatorSubTest") {
408 module_out_path = module_output_path
409
410 sources = [
411 "../../../include/calculator.h",
412 "../../../src/calculator.cpp",
413 ]
414
415 sources += [ "calculator_sub_test.cpp" ]
416
417 configs = [ ":module_private_config" ]
418
419 deps = [ "//third_party/googletest:gtest_main" ]
420 }
421
422 group("unittest") {
423 testonly = true
424 deps = [":CalculatorSubTest"]
425 }
426 ```
427 详细内容如下:
428
429 1. 添加文件头注释信息
430 ```
431 # Copyright (c) 2021 XXXX Device Co., Ltd.
432 ```
433 2. 导入编译模板文件
434 ```
435 import("//build/test.gni")
436 ```
437 3. 指定文件输出路径
438 ```
439 module_output_path = "developertest/calculator"
440 ```
441 > **说明:** 此处输出路径为部件/模块名。
442
443 4. 配置依赖包含目录
444
445 ```
446 config("module_private_config") {
447 visibility = [ ":*" ]
448
449 include_dirs = [ "../../../include" ]
450 }
451 ```
452 > **说明:** 一般在此处对相关配置进行设置,在测试用例编译脚本中可直接引用。
453
454 5. 指定测试用例编译目标输出的文件名称
455
456 ```
457 ohos_unittest("CalculatorSubTest") {
458 }
459 ```
460 6. 编写具体的测试用例编译脚本(添加需要参与编译的源文件、配置和依赖)
461 ```
462 ohos_unittest("CalculatorSubTest") {
463 module_out_path = module_output_path
464 sources = [
465 "../../../include/calculator.h",
466 "../../../src/calculator.cpp",
467 "../../../test/calculator_sub_test.cpp"
468 ]
469 sources += [ "calculator_sub_test.cpp" ]
470 configs = [ ":module_private_config" ]
471 deps = [ "//third_party/googletest:gtest_main" ]
472 }
473 ```
474
475 > **说明:根据测试类型的不同,在具体编写过程中可选择不同的测试类型:**
476 > - ohos_unittest:单元测试
477 > - ohos_moduletest:模块测试
478 > - ohos_systemtest:系统测试
479 > - ohos_performancetest:性能测试
480 > - ohos_securitytest:安全测试
481 > - ohos_reliabilitytest:可靠性测试
482 > - ohos_distributedtest:分布式测试
483
484 7. 对目标测试用例文件进行条件分组
485
486 ```
487 group("unittest") {
488 testonly = true
489 deps = [":CalculatorSubTest"]
490 }
491 ```
492 > **说明:** 进行条件分组的目的在于执行用例时可以选择性的执行某一种特定类型的用例。
493
494- **JavaScript用例编译配置示例**
495
496 ```
497 # Copyright (C) 2021 XXXX Device Co., Ltd.
498
499 import("//build/test.gni")
500
501 module_output_path = "developertest/app_info"
502
503 ohos_js_unittest("GetAppInfoJsTest") {
504 module_out_path = module_output_path
505
506 hap_profile = "./config.json"
507 certificate_profile = "//test/developertest/signature/openharmony_sx.p7b"
508 }
509
510 group("unittest") {
511 testonly = true
512 deps = [ ":GetAppInfoJsTest" ]
513 }
514 ```
515
516 详细内容如下:
517
518 1. 添加文件头注释信息
519
520 ```
521 # Copyright (C) 2021 XXXX Device Co., Ltd.
522 ```
523 2. 导入编译模板文件
524
525 ```
526 import("//build/test.gni")
527 ```
528 3. 指定文件输出路径
529
530 ```
531 module_output_path = "developertest/app_info"
532 ```
533 > **说明:** 此处输出路径为部件/模块名。
534
535 4. 指定测试用例编译目标输出的文件名称
536
537 ```
538 ohos_js_unittest("GetAppInfoJsTest") {
539 }
540 ```
541 > **说明:**
542 >- 使用模板ohos_js_unittest定义js测试套,注意与C++用例区分。
543 >- js测试套编译输出文件为hap类型,hap名为此处定义的测试套名,测试套名称必须以JsTest结尾。
544
545 5. 指定hap包配置文件config.json和签名文件,两个配置为必选项
546
547 ```
548 ohos_js_unittest("GetAppInfoJsTest") {
549 module_out_path = module_output_path
550
551 hap_profile = "./config.json"
552 certificate_profile = "//test/developertest/signature/openharmony_sx.p7b"
553 }
554 ```
555 config.json为hap编译所需配置文件,需要开发者根据被测sdk版本配置“target”项,其余项可默认,具体如下所示:
556
557 ```
558 {
559 "app": {
560 "bundleName": "com.example.myapplication",
561 "vendor": "example",
562 "version": {
563 "code": 1,
564 "name": "1.0"
565 },
566 "apiVersion": {
567 "compatible": 4,
568 "target": 5 // 根据被测sdk版本进行修改,此例为sdk5
569 }
570 },
571 "deviceConfig": {},
572 "module": {
573 "package": "com.example.myapplication",
574 "name": ".MyApplication",
575 "deviceType": [
576 "phone"
577 ],
578 "distro": {
579 "deliveryWithInstall": true,
580 "moduleName": "entry",
581 "moduleType": "entry"
582 },
583 "abilities": [
584 {
585 "skills": [
586 {
587 "entities": [
588 "entity.system.home"
589 ],
590 "actions": [
591 "action.system.home"
592 ]
593 }
594 ],
595 "name": "com.example.myapplication.MainAbility",
596 "icon": "$media:icon",
597 "description": "$string:mainability_description",
598 "label": "MyApplication",
599 "type": "page",
600 "launchType": "standard"
601 }
602 ],
603 "js": [
604 {
605 "pages": [
606 "pages/index/index"
607 ],
608 "name": "default",
609 "window": {
610 "designWidth": 720,
611 "autoDesignWidth": false
612 }
613 }
614 ]
615 }
616 }
617 ```
618 6. 对目标测试用例文件进行条件分组
619 ```
620 group("unittest") {
621 testonly = true
622 deps = [ ":GetAppInfoJsTest" ]
623 }
624 ```
625 > **说明:** 进行条件分组的目的在于执行用例时可以选择性的执行某一种特定类型的用例。
626
627#### Fuzz测试
628
629[Fuzz编译文件编写规范](https://gitee.com/openharmony/test_developertest/blob/master/libs/fuzzlib/README_zh.md)
630
631#### Benchmark测试
632
633[Benchmark编译文件编写规范](https://gitee.com/openharmony/test_developertest/blob/master/libs/benchmark/README_zh.md)
634
635#### 编译入口配置文件ohos.build
636
637当完成用例编译配置文件编写后,需要进一步编写部件编译配置文件,以关联到具体的测试用例。
638```
639"partA": {
640 "module_list": [
641
642 ],
643 "inner_list": [
644
645 ],
646 "system_kits": [
647
648 ],
649 "test_list": [ //配置模块calculator下的test
650 "//system/subsystem/partA/calculator/test:unittest",
651 "//system/subsystem/partA/calculator/test:fuzztest",
652 "//system/subsystem/partA/calculator/test:benchmarktest"
653 }
654```
655> **说明:** test_list中配置的是对应模块的测试用例。
656
657### 测试用例资源配置
658测试依赖资源主要包括测试用例在执行过程中需要的图片文件,视频文件、第三方库等对外的文件资源。
659
660依赖资源文件配置步骤如下:
6611. 在部件的test目录下创建resource目录,在resource目录下创建对应的模块,在模块目录中存放该模块所需要的资源文件
662
6632. 在resource目录下对应的模块目录中创建一个ohos_test.xml文件,文件内容格式如下:
664 ```
665 <?xml version="1.0" encoding="UTF-8"?>
666 <configuration ver="2.0">
667 <target name="CalculatorSubTest">
668 <preparer>
669 <option name="push" value="test.jpg -> /data/test/resource" src="res"/>
670 <option name="push" value="libc++.z.so -> /data/test/resource" src="out"/>
671 </preparer>
672 </target>
673 </configuration>
674 ```
6753. 在测试用例的编译配置文件中定义resource_config_file进行指引,用来指定对应的资源文件ohos_test.xml
676 ```
677 ohos_unittest("CalculatorSubTest") {
678 resource_config_file = "//system/subsystem/partA/test/resource/calculator/ohos_test.xml"
679 }
680 ```
681 >**说明:**
682 >- target_name: 测试套的名称,定义在测试目录的BUILD.gn中。preparer: 表示该测试套执行前执行的动作。
683 >- src="res": 表示测试资源位于test目录下的resource目录下,src="out":表示位于out/release/$(部件)目录下。
684
685## 测试用例执行
686在执行测试用例之前,针对用例使用设备的不同,需要对相应配置进行修改,修改完成即可执行测试用例。
687
688### user_config.xml配置
689```
690<user_config>
691 <build>
692 <!-- 是否编译demo用例, 默认为false,如果需要编译demo可修改为true -->
693 <example>false</example>
694 <!-- 是否编译版本, 默认为false -->
695 <version>false</version>
696 <!-- 是否编译测试用例, 默认为true,若已完成编译,再执行用例之前可修改为false,防止重新编译 -->
697 <testcase>true</testcase>
698 </build>
699 <environment>
700 <!-- 配置远程映射机器的IP及端口,以支持HDC连接的设备 -->
701 <device type="usb-hdc">
702 <ip></ip>
703 <port></port>
704 <sn></sn>
705 </device>
706 <!-- 配置设备的串口信息,以支持串口连接的设备 -->
707 <device type="com" label="ipcamera">
708 <serial>
709 <com></com>
710 <type>cmd</type>
711 <baud_rate>115200</baud_rate>
712 <data_bits>8</data_bits>
713 <stop_bits>1</stop_bits>
714 <timeout>1</timeout>
715 </serial>
716 </device>
717 </environment>
718 <!-- 配置测试用例路径,若测试用例未编译,即<testcase>标签属性为true时,此处默认不填写;若编译已完成,需在此处指定测试用例的实际路径 -->
719 <test_cases>
720 <dir></dir>
721 </test_cases>
722 <!-- 配置覆盖率编译路径 -->
723 <coverage>
724 <outpath></outpath>
725 </coverage>
726 <!-- NFS挂载信息配置,被测设备仅支持串口连接时配置,指定NFS的映射路径,host_dir为PC侧的NFS目录,board_dir为板侧创建的目录 -->
727 <NFS>
728 <host_dir></host_dir>
729 <mnt_cmd></mnt_cmd>
730 <board_dir></board_dir>
731 </NFS>
732</user_config>
733```
734>**说明:** 在执行测试用例之前,若使用HDC连接设备,用例仅需配置设备IP和端口号即可,其余信息均默认不修改。
735
736### Windows环境执行
737#### 测试用例编译
738
739由于Windows环境下无法实现用例编译,因此执行用例前需要在Linux环境下进行用例编译,用例编译命令:
740```
741./build.sh --product-name Hi3516DV300 --build-target make_test
742```
743>说明:
744> - product-name:指定编译产品名称,例如Hi3516DV300。
745> - build-target:指定所需编译用例,make_test表示指定全部用例,实际开发中可指定特定用例。
746
747编译完成后,测试用例将自动保存在out/ohos-arm-release/packages/phone/tests目录下。
748
749#### 搭建执行环境
7501. 在Windows环境创建测试框架目录Test,并在此目录下创建testcase目录
751
7522. 从Linux环境拷贝测试框架developertest和xdevice到创建的Test目录下,拷贝编译好的测试用例到testcase目录下
753 >**说明:** 将测试框架及测试用例从Linux环境移植到Windows环境,以便后续执行。
754
7553. 修改user_config.xml
756 ```
757 <build>
758 <!-- 由于测试用例已编译完成,此标签属性需改为false -->
759 <testcase>false</testcase>
760 </build>
761 <test_cases>
762 <!-- 由于已将测试用例拷贝到Windows环境下,测试用例输出路径发生改变,需要修改为拷贝后所存放的路径 -->
763 <dir>D:\Test\testcase\tests</dir>
764 </test_cases>
765 ```
766 >**说明:** `<testcase>`标签表示是否需要编译用例;`<dir>`标签表示测试用例查找路径。
767
768#### 执行用例
7691. 启动测试框架
770 ```
771 start.bat
772 ```
7732. 选择产品形态
774
775 进入测试框架,系统会自动提示您选择产品形态,请根据实际的开发板进行选择。例如:Hi3516DV300。
776
7773. 执行测试用例
778
779 当选择完产品形态,可参考如下指令执行测试用例。
780 ```
781 run -t UT -ts CalculatorSubTest -tc interger_sub_00l
782 ```
783 执行命令参数说明:
784 ```
785 -t [TESTTYPE]: 指定测试用例类型,有UT,MST,ST,PERF,FUZZ,BENCHMARK等。(必选参数)
786 -tp [TESTPART]: 指定部件,可独立使用。
787 -tm [TESTMODULE]: 指定模块,不可独立使用,需结合-tp指定上级部件使用。
788 -ts [TESTSUITE]: 指定测试套,可独立使用。
789 -tc [TESTCASE]: 指定测试用例,不可独立使用,需结合-ts指定上级测试套使用。
790 -h : 帮助命令。
791 ```
792### Linux环境执行
793#### 远程端口映射
794为了在Linux远程服务器以及Linux虚拟机两种环境下执行测试用例,需要对端口进行远程映射,以实现与设备的数据通路连接。具体操作如下:
7951. HDC Server指令:
796 ```
797 hdc_std kill
798 hdc_std -m -s 0.0.0.0:8710
799 ```
800 >**说明:** IP和端口号为默认值。
801
8022. HDC Client指令:
803 ```
804 hdc_std -s xx.xx.xx.xx:8710 list targets
805 ```
806 >**说明:** 此处IP填写设备侧IP地址。
807
808#### 执行用例
8091. 启动测试框架
810 ```
811 ./start.sh
812 ```
8132. 选择产品形态
814
815 进入测试框架,系统会自动提示您选择产品形态,请根据实际的开发板进行选择。例如:Hi3516DV300。
816
8173. 执行测试用例
818
819 测试框架在执行用例时会根据指令找到所需用例,自动实现用例编译,执行过程,完成自动化测试。
820 ```
821 run -t UT -ts CalculatorSubTest -tc interger_sub_00l
822 ```
823 执行命令参数说明:
824 ```
825 -t [TESTTYPE]: 指定测试用例类型,有UT,MST,ST,PERF,FUZZ,BENCHMARK等。(必选参数)
826 -tp [TESTPART]: 指定部件,可独立使用。
827 -tm [TESTMODULE]: 指定模块,不可独立使用,需结合-tp指定上级部件使用。
828 -ts [TESTSUITE]: 指定测试套,可独立使用。
829 -tc [TESTCASE]: 指定测试用例,不可独立使用,需结合-ts指定上级测试套使用。
830 -h : 帮助命令。
831 ```
832
833## 测试报告日志
834当执行完测试指令,控制台会自动生成测试结果,若需要详细测试报告您可在相应的数据文档中进行查找。
835
836### 测试结果
837测试结果输出根路径如下:
838```
839test/developertest/reports/xxxx_xx_xx_xx_xx_xx
840```
841>**说明:** 测试报告文件目录将自动生成。
842
843该目录中包含以下几类结果:
844| 类型 | 描述|
845| ------------ | ------------ |
846| result/ |测试用例格式化结果|
847| log/plan_log_xxxx_xx_xx_xx_xx_xx.log | 测试用例日志 |
848| summary_report.html | 测试报告汇总 |
849| details_report.html | 测试报告详情 |
850
851### 测试框架日志
852```
853reports/platform_log_xxxx_xx_xx_xx_xx_xx.log
854```
855
856### 最新测试报告
857```
858reports/latest
859```
860
861## 涉及仓
862
863[test\_xdevice](https://gitee.com/openharmony/test_xdevice/blob/master/README_zh.md)
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892