• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# XTS子系统<a name="ZH-CN_TOPIC_0000001117409671"></a>
2
3-   [简介](#section465982318513)
4-   [系统类型](#section125090457443)
5-   [目录](#section161941989596)
6-   [约束](#section119744591305)
7-   [使用说明](#section137768191623)
8-   [用例开发指导](#section3695134065513)
9    -   [C语言用例开发编译指导(适用于轻量系统产品用例开发)](#section198193336544)
10    -   [C语言用例执行指导(适用于轻量系统产品用例开发)](#section13820233175418)
11    -   [C++语言用例开发编译指导(适用于小型系统、标准系统用例开发)](#section3822123311540)
12    -   [C++语言用例执行指导(适用于小型系统、标准系统用例开发)](#section128222336544)
13    -   [JS语言用例开发指导(适用于标准系统)](#section159801435165220)
14    -   [JS语言用例编译打包指导(适用于标准系统)](#section445519106559)
15    -   [Python用例开发编译指导](#section281040146993)
16    -   [Python用例执行指导](#section637497153319)
17
18-   [相关仓](#section1371113476307)
19
20## 简介<a name="section465982318513"></a>
21
22XTS子系统是OpenHarmony生态认证测试套件的集合,当前包括ACTS(application compatibility test suite)应用兼容性测试组件,后续将拓展DCTS(distributed compatibility test suite)分布式兼容性测试组件、HATS(hardware abstract test suite) 硬件抽象层兼容性测试组件。
23
24XTS子系统当前包括ACTS 与Tools 软件包:
25
26-   ACTS,存放ACTS 相关测试案例源码与配置文件,其目的是帮助终端设备制造商尽早发现软件与OpenHarmony的不兼容问题,确保软件在整个开发周期内符合OpenHarmony的兼容性标准。
27-   Tools,存放ACTS 相关测试案例开发框架。
28
29## 系统类型<a name="section125090457443"></a>
30
31OpenHarmony支持如下几种系统类型:
32
33-   轻量系统(mini system)
34
35    面向MCU类处理器例如Arm Cortex-M、RISC-V 32位的设备,硬件资源极其有限,支持的设备最小内存为128KiB,可以提供多种轻量级网络协议,轻量级的图形框架,以及丰富的IOT总线读写部件等。可支撑的产品如智能家居领域的连接类模组、传感器设备、穿戴类设备等。
36
37-   小型系统(small system)
38
39    面向应用处理器例如Arm Cortex-A的设备,支持的设备最小内存为1MiB,可以提供更高的安全能力、标准的图形框架、视频编解码的多媒体能力。可支撑的产品如智能家居领域的IP Camera、电子猫眼、路由器以及智慧出行领域的行车记录仪等。
40
41-   标准系统(standard system)
42
43    面向应用处理器例如Arm Cortex-A的设备,支持的设备最小内存为128MiB,可以提供增强的交互能力、3D GPU以及硬件合成能力、更多控件以及动画效果更丰富的图形能力、完整的应用框架。可支撑的产品如高端的冰箱显示屏。
44
45
46## 目录<a name="section161941989596"></a>
47
48```
49/test/xts
50├── acts                # 测试代码存放目录
51│   └── subsystem       # 标准系统子系统测试用例源码存放目录
52│   └── subsystem_lite  # 轻量系统、小型系统子系统测试用例源码存放目录
53│   └── BUILD.gn        # 标准系统测试用例编译配置
54│   └── build_lite      # 轻量系统、小型系统测试用例编译配置存放目录
55│       └── BUILD.gn    # 轻量系统、小型系统测试用例编译配置
56└── tools               # 测试工具代码存放目录
57```
58
59## 约束<a name="section119744591305"></a>
60
61轻量系统用例开发语言是C,小型系统用例开发语言是C++。
62
63## 使用说明<a name="section137768191623"></a>
64
65**表 1**  用例级别说明
66
67<a name="table12856113719456"></a>
68<table><thead align="left"><tr id="row88561737114517"><th class="cellrowborder" valign="top" width="33.333333333333336%" id="mcps1.2.4.1.1"><p id="p54711456452"><a name="p54711456452"></a><a name="p54711456452"></a>级别名称</p>
69</th>
70<th class="cellrowborder" valign="top" width="33.283328332833285%" id="mcps1.2.4.1.2"><p id="p114834544518"><a name="p114834544518"></a><a name="p114834544518"></a>基本定义</p>
71</th>
72<th class="cellrowborder" valign="top" width="33.383338333833386%" id="mcps1.2.4.1.3"><p id="p348154574518"><a name="p348154574518"></a><a name="p348154574518"></a>测试范围</p>
73</th>
74</tr>
75</thead>
76<tbody><tr id="row78561137134514"><td class="cellrowborder" valign="top" width="33.333333333333336%" headers="mcps1.2.4.1.1 "><p id="p17481345124512"><a name="p17481345124512"></a><a name="p17481345124512"></a>Level0</p>
77</td>
78<td class="cellrowborder" valign="top" width="33.283328332833285%" headers="mcps1.2.4.1.2 "><p id="p1548945204514"><a name="p1548945204514"></a><a name="p1548945204514"></a>冒烟</p>
79</td>
80<td class="cellrowborder" valign="top" width="33.383338333833386%" headers="mcps1.2.4.1.3 "><p id="p174824517456"><a name="p174824517456"></a><a name="p174824517456"></a>验证关键功能点基本功能/最基本DFX属性在最常见输入下的表现,通过表示功能基本可运行。</p>
81</td>
82</tr>
83<tr id="row28563372451"><td class="cellrowborder" valign="top" width="33.333333333333336%" headers="mcps1.2.4.1.1 "><p id="p448445144512"><a name="p448445144512"></a><a name="p448445144512"></a>Level1</p>
84</td>
85<td class="cellrowborder" valign="top" width="33.283328332833285%" headers="mcps1.2.4.1.2 "><p id="p44813452452"><a name="p44813452452"></a><a name="p44813452452"></a>基本</p>
86</td>
87<td class="cellrowborder" valign="top" width="33.383338333833386%" headers="mcps1.2.4.1.3 "><p id="p648184584514"><a name="p648184584514"></a><a name="p648184584514"></a>验证各功能点基本功能/基本DFX属性在常见输入下的表现,通过表示功能基本可测试。</p>
88</td>
89</tr>
90<tr id="row12856137204514"><td class="cellrowborder" valign="top" width="33.333333333333336%" headers="mcps1.2.4.1.1 "><p id="p164834520453"><a name="p164834520453"></a><a name="p164834520453"></a>Level2</p>
91</td>
92<td class="cellrowborder" valign="top" width="33.283328332833285%" headers="mcps1.2.4.1.2 "><p id="p148245184511"><a name="p148245184511"></a><a name="p148245184511"></a>重要</p>
93</td>
94<td class="cellrowborder" valign="top" width="33.383338333833386%" headers="mcps1.2.4.1.3 "><p id="p348945124510"><a name="p348945124510"></a><a name="p348945124510"></a>验证各功能点的基本功能/基本DFX属性在常规输入/常见异常情况下的表现,通过表示功能基本正常可用,可开展Beta。</p>
95</td>
96</tr>
97<tr id="row48571137204514"><td class="cellrowborder" valign="top" width="33.333333333333336%" headers="mcps1.2.4.1.1 "><p id="p7481545184513"><a name="p7481545184513"></a><a name="p7481545184513"></a>Level3</p>
98</td>
99<td class="cellrowborder" valign="top" width="33.283328332833285%" headers="mcps1.2.4.1.2 "><p id="p1748184514455"><a name="p1748184514455"></a><a name="p1748184514455"></a>一般</p>
100</td>
101<td class="cellrowborder" valign="top" width="33.383338333833386%" headers="mcps1.2.4.1.3 "><p id="p8481245104519"><a name="p8481245104519"></a><a name="p8481245104519"></a>验证各功能点的全部功能/全部DFX属性在各种常规/非常规输入组合下,或各种正常/异常预置条件组合下的表现。</p>
102</td>
103</tr>
104<tr id="row14857133712451"><td class="cellrowborder" valign="top" width="33.333333333333336%" headers="mcps1.2.4.1.1 "><p id="p1248144515456"><a name="p1248144515456"></a><a name="p1248144515456"></a>Level4</p>
105</td>
106<td class="cellrowborder" valign="top" width="33.283328332833285%" headers="mcps1.2.4.1.2 "><p id="p94804514519"><a name="p94804514519"></a><a name="p94804514519"></a>生僻</p>
107</td>
108<td class="cellrowborder" valign="top" width="33.383338333833386%" headers="mcps1.2.4.1.3 "><p id="p1248445164516"><a name="p1248445164516"></a><a name="p1248445164516"></a>验证关键功能点在极端异常预置条件下、用户难以触及的异常输入组合下的表现。</p>
109</td>
110</tr>
111</tbody>
112</table>
113
114**表 2**  用例粒度说明
115
116<a name="table1690733521014"></a>
117<table><thead align="left"><tr id="row9907143516105"><th class="cellrowborder" valign="top" width="19.591959195919593%" id="mcps1.2.4.1.1"><p id="p1141312416102"><a name="p1141312416102"></a><a name="p1141312416102"></a>用例规模</p>
118</th>
119<th class="cellrowborder" valign="top" width="35.17351735173517%" id="mcps1.2.4.1.2"><p id="p34131416109"><a name="p34131416109"></a><a name="p34131416109"></a>被测试对象</p>
120</th>
121<th class="cellrowborder" valign="top" width="45.23452345234523%" id="mcps1.2.4.1.3"><p id="p12413141171010"><a name="p12413141171010"></a><a name="p12413141171010"></a>测试环境</p>
122</th>
123</tr>
124</thead>
125<tbody><tr id="row2907335181017"><td class="cellrowborder" valign="top" width="19.591959195919593%" headers="mcps1.2.4.1.1 "><p id="p6414134117109"><a name="p6414134117109"></a><a name="p6414134117109"></a>LargeTest</p>
126</td>
127<td class="cellrowborder" valign="top" width="35.17351735173517%" headers="mcps1.2.4.1.2 "><p id="p20414104113101"><a name="p20414104113101"></a><a name="p20414104113101"></a>业务功能/全场景特性/整机及场景级DFX</p>
128</td>
129<td class="cellrowborder" valign="top" width="45.23452345234523%" headers="mcps1.2.4.1.3 "><p id="p5414841121013"><a name="p5414841121013"></a><a name="p5414841121013"></a>尽量使用贴近真实的环境设备</p>
130</td>
131</tr>
132<tr id="row10907635131015"><td class="cellrowborder" valign="top" width="19.591959195919593%" headers="mcps1.2.4.1.1 "><p id="p19414134117104"><a name="p19414134117104"></a><a name="p19414134117104"></a>MediumTest</p>
133</td>
134<td class="cellrowborder" valign="top" width="35.17351735173517%" headers="mcps1.2.4.1.2 "><p id="p114141241141018"><a name="p114141241141018"></a><a name="p114141241141018"></a>模块/子系统集成至设备后的功能/DFX</p>
135</td>
136<td class="cellrowborder" valign="top" width="45.23452345234523%" headers="mcps1.2.4.1.3 "><p id="p14414941111015"><a name="p14414941111015"></a><a name="p14414941111015"></a>使用真实的单设备进行验证,可进行消息模拟,尽量不对函数进行MOCK</p>
137</td>
138</tr>
139<tr id="row1990713353100"><td class="cellrowborder" valign="top" width="19.591959195919593%" headers="mcps1.2.4.1.1 "><p id="p641414110109"><a name="p641414110109"></a><a name="p641414110109"></a>SmallTest</p>
140</td>
141<td class="cellrowborder" valign="top" width="35.17351735173517%" headers="mcps1.2.4.1.2 "><p id="p54141541181018"><a name="p54141541181018"></a><a name="p54141541181018"></a>模块/类/函数</p>
142</td>
143<td class="cellrowborder" valign="top" width="45.23452345234523%" headers="mcps1.2.4.1.3 "><p id="p1841494131013"><a name="p1841494131013"></a><a name="p1841494131013"></a>在开发者个人环境进行测试,尽量不依赖其他模块,存在大量的MOCK</p>
144</td>
145</tr>
146</tbody>
147</table>
148
149**表 3**  测试类型说明
150
151<a name="table18811230124619"></a>
152<table><thead align="left"><tr id="row48163019466"><th class="cellrowborder" valign="top" width="19.040000000000003%" id="mcps1.2.3.1.1"><p id="p1791784418465"><a name="p1791784418465"></a><a name="p1791784418465"></a>测试类型名称</p>
153</th>
154<th class="cellrowborder" valign="top" width="80.96%" id="mcps1.2.3.1.2"><p id="p2917164444614"><a name="p2917164444614"></a><a name="p2917164444614"></a>测试类型定义</p>
155</th>
156</tr>
157</thead>
158<tbody><tr id="row682630104615"><td class="cellrowborder" valign="top" width="19.040000000000003%" headers="mcps1.2.3.1.1 "><p id="p1091784414469"><a name="p1091784414469"></a><a name="p1091784414469"></a>Function</p>
159</td>
160<td class="cellrowborder" valign="top" width="80.96%" headers="mcps1.2.3.1.2 "><p id="p16917344104618"><a name="p16917344104618"></a><a name="p16917344104618"></a>验证被测对象提供给用户的业务功能实现正确性的测试项,这里的“用户”可以是终端用户或开发者,功能包括业务功能及平台功能</p>
161</td>
162</tr>
163<tr id="row1821930154611"><td class="cellrowborder" valign="top" width="19.040000000000003%" headers="mcps1.2.3.1.1 "><p id="p591714474612"><a name="p591714474612"></a><a name="p591714474612"></a>Performance</p>
164</td>
165<td class="cellrowborder" valign="top" width="80.96%" headers="mcps1.2.3.1.2 "><p id="p15917154410463"><a name="p15917154410463"></a><a name="p15917154410463"></a>验证被测对象在特定预置条件/负载模型下的处理能力的测试项,“处理能力”一般以单位时间内可处理的业务量来衡量,如呼叫/秒,帧率/秒,事件处理量/秒等</p>
166</td>
167</tr>
168<tr id="row13821030104616"><td class="cellrowborder" valign="top" width="19.040000000000003%" headers="mcps1.2.3.1.1 "><p id="p691711440467"><a name="p691711440467"></a><a name="p691711440467"></a>Power</p>
169</td>
170<td class="cellrowborder" valign="top" width="80.96%" headers="mcps1.2.3.1.2 "><p id="p159171544104616"><a name="p159171544104616"></a><a name="p159171544104616"></a>验证被测对象在特定预置条件/负载模型下在一定时间内能源消耗量的测试项</p>
171</td>
172</tr>
173<tr id="row6821330114618"><td class="cellrowborder" valign="top" width="19.040000000000003%" headers="mcps1.2.3.1.1 "><p id="p13917164454612"><a name="p13917164454612"></a><a name="p13917164454612"></a>Reliability</p>
174</td>
175<td class="cellrowborder" valign="top" width="80.96%" headers="mcps1.2.3.1.2 "><p id="p11917194416465"><a name="p11917194416465"></a><a name="p11917194416465"></a>验证被测对象在正常/异常输入情况下,或业务量压力和长时间连续运行压力情况下业务表现的测试项,含稳定性、压力、故障注入、Monkey测试项</p>
176</td>
177</tr>
178<tr id="row11821930184612"><td class="cellrowborder" valign="top" width="19.040000000000003%" headers="mcps1.2.3.1.1 "><p id="p1691774474618"><a name="p1691774474618"></a><a name="p1691774474618"></a>Security</p>
179</td>
180<td class="cellrowborder" valign="top" width="80.96%" headers="mcps1.2.3.1.2 "><p id="p1791784410463"><a name="p1791784410463"></a><a name="p1791784410463"></a>验证系统对恶意威胁的防护能力,威胁包括但不限于未授权访问、使用、泄露、破坏、修改、毁灭,以保障信息的机密性、完整性和可用性; 验证系统对用户隐私的保护能力,保障用户的隐私数据被收集、使用、保有、披露和处置符合法律规范,保障用户的隐私权; 验证对各类安全规范的遵从情况,如安全设计规范、安全红线、工信部安全认证规范等,保障安全相关法律法规的合规。</p>
181</td>
182</tr>
183<tr id="row16825307467"><td class="cellrowborder" valign="top" width="19.040000000000003%" headers="mcps1.2.3.1.1 "><p id="p129188444462"><a name="p129188444462"></a><a name="p129188444462"></a>Global</p>
184</td>
185<td class="cellrowborder" valign="top" width="80.96%" headers="mcps1.2.3.1.2 "><p id="p179186444465"><a name="p179186444465"></a><a name="p179186444465"></a>验证被测对象在是否具有国际化数据支持和本地化能力的测试项,包括语言显示、输入/输出习惯、时间显示、区域特性如货币时间禁忌等等</p>
186</td>
187</tr>
188<tr id="row08211308464"><td class="cellrowborder" valign="top" width="19.040000000000003%" headers="mcps1.2.3.1.1 "><p id="p191814447465"><a name="p191814447465"></a><a name="p191814447465"></a>Compatibility</p>
189</td>
190<td class="cellrowborder" valign="top" width="80.96%" headers="mcps1.2.3.1.2 "><p id="p99181344144613"><a name="p99181344144613"></a><a name="p99181344144613"></a>当被测对象为应用时,包括被测对象对于自身数据的后向兼容性、对于系统的前后向兼容性、对于不同用户数据(如播放器之音频文件格式/智能短信之用户短信内容)的兼容性测试项; 当被测对象为系统时,包括被测系统对于系统自身数据的后向兼容性、以及对于生态中常用应用的兼容性测试项;当被测对象为软件时,包括被测系统对于相关的硬件的兼容性;</p>
191</td>
192</tr>
193<tr id="row1782730124618"><td class="cellrowborder" valign="top" width="19.040000000000003%" headers="mcps1.2.3.1.1 "><p id="p13918144134614"><a name="p13918144134614"></a><a name="p13918144134614"></a>User</p>
194</td>
195<td class="cellrowborder" valign="top" width="80.96%" headers="mcps1.2.3.1.2 "><p id="p1291844494611"><a name="p1291844494611"></a><a name="p1291844494611"></a>验证被测对象在真实用户场景下的用户体验感受的测试项,注意此种情况下没有客观的“正确”与“失败”,所有的结论及评价都应该来自于用户</p>
196</td>
197</tr>
198<tr id="row58243024617"><td class="cellrowborder" valign="top" width="19.040000000000003%" headers="mcps1.2.3.1.1 "><p id="p1291884474614"><a name="p1291884474614"></a><a name="p1291884474614"></a>Standard</p>
199</td>
200<td class="cellrowborder" valign="top" width="80.96%" headers="mcps1.2.3.1.2 "><p id="p0918124424614"><a name="p0918124424614"></a><a name="p0918124424614"></a>验证被测对象对于行业及公司内标准/协议/规范的遵从情况的测试项,注意此处的“标准”不包含任何安全标准,针对安全标准的测试项划归为“安全测试”类型</p>
201</td>
202</tr>
203<tr id="row382830124619"><td class="cellrowborder" valign="top" width="19.040000000000003%" headers="mcps1.2.3.1.1 "><p id="p119181044164619"><a name="p119181044164619"></a><a name="p119181044164619"></a>Safety</p>
204</td>
205<td class="cellrowborder" valign="top" width="80.96%" headers="mcps1.2.3.1.2 "><p id="p1291818443468"><a name="p1291818443468"></a><a name="p1291818443468"></a>验证被测对象的Safety属性,避免产品可能对人身安全、健康以及产品本身带来的危害。</p>
206</td>
207</tr>
208<tr id="row1083153014465"><td class="cellrowborder" valign="top" width="19.040000000000003%" headers="mcps1.2.3.1.1 "><p id="p39187441469"><a name="p39187441469"></a><a name="p39187441469"></a>Resilience</p>
209</td>
210<td class="cellrowborder" valign="top" width="80.96%" headers="mcps1.2.3.1.2 "><p id="p891815444462"><a name="p891815444462"></a><a name="p891815444462"></a>验证被测对象的韧性属性,确保系统受攻击时承受并保持在有定义的运行状态(包括降级)、恢复并适应攻击以保障Mission达成。</p>
211</td>
212</tr>
213</tbody>
214</table>
215
216## 用例开发指导<a name="section3695134065513"></a>
217
218根据测试系统选择测试框架和对应测试用例语言。
219
220**表 4**  系统和测试框架、开发语言对应关系
221
222<a name="table4418343171415"></a>
223<table><thead align="left"><tr id="row34183435145"><th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.1"><p id="p941874311148"><a name="p941874311148"></a><a name="p941874311148"></a>系统</p>
224</th>
225<th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.2"><p id="p1841804341413"><a name="p1841804341413"></a><a name="p1841804341413"></a>测试框架</p>
226</th>
227<th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.3"><p id="p2418104311148"><a name="p2418104311148"></a><a name="p2418104311148"></a>语言</p>
228</th>
229</tr>
230</thead>
231<tbody><tr id="row8419164319148"><td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="p7419194312143"><a name="p7419194312143"></a><a name="p7419194312143"></a>轻量系统</p>
232</td>
233<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="p10419124312145"><a name="p10419124312145"></a><a name="p10419124312145"></a>hctest</p>
234</td>
235<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="p11419643191410"><a name="p11419643191410"></a><a name="p11419643191410"></a>c</p>
236</td>
237</tr>
238<tr id="row141915438147"><td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="p441911436141"><a name="p441911436141"></a><a name="p441911436141"></a>小型系统</p>
239</td>
240<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="p541916432142"><a name="p541916432142"></a><a name="p541916432142"></a>hcpptest</p>
241</td>
242<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="p54191643131416"><a name="p54191643131416"></a><a name="p54191643131416"></a>c++</p>
243</td>
244</tr>
245<tr id="row4419134341417"><td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="p341964313143"><a name="p341964313143"></a><a name="p341964313143"></a>标准系统</p>
246</td>
247<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="p10419443171416"><a name="p10419443171416"></a><a name="p10419443171416"></a>HJSUnit、hcpptest</p>
248</td>
249<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="p9419143181414"><a name="p9419143181414"></a><a name="p9419143181414"></a>js、c++</p>
250</td>
251</tr>
252</tbody>
253</table>
254
255### C语言用例开发编译指导(适用于轻量系统产品用例开发)<a name="section198193336544"></a>
256
257**示例:轻量系统测试用例开发**
258
259当前使用的测试框架是hctest,hctest测试框架支持使用C语言编写测试用例,是在开源测试框架unity的基础上进行增强和适配。
260
2611.  用例目录规范:测试用例存储到test/xts/acts仓中
262
263    ```
264    ├── acts
265    │ └──subsystem_lite
266    │ │ └── module_hal
267    │ │ │ └── BUILD.gn
268    │ │ │ └── src
269    │ └──build_lite
270    │ │ └── BUILD.gn
271    ```
272
2732.  src目录下用例编写样例。
274
275    1.引用测试框架
276
277    ```
278    #include "hctest.h"
279    ```
280
281    2. 使用宏定义LITE\_TEST\_SUIT定义子系统、模块、测试套件名称
282
283    ```
284    /**
285    * @brief  register a test suit named "IntTestSuite"
286    * @param  test subsystem name
287    * @param  example module name
288    * @param  IntTestSuite test suit name
289    */
290    LITE_TEST_SUIT(test, example, IntTestSuite);
291    ```
292
293    3. 定义Setup与TearDown
294
295    命名方式:测试套件名称+Setup,测试套件名称+TearDown。
296
297    Setup与TearDown必须存在,可以为空函数。
298
299    4. 使用宏定义LITE\_TEST\_CASE写测试用例
300
301    包括三个参数:测试套件名称,测试用例名称,用例属性(测试类型、用例粒度、用例级别)。
302
303    ```
304    LITE_TEST_CASE(IntTestSuite, TestCase001, Function | MediumTest | Level1)
305    {
306      //do something
307    };
308    ```
309
310    5. 使用宏定义 RUN\_TEST\_SUITE注册测试套件
311
312    ```
313    RUN_TEST_SUITE(IntTestSuite);
314    ```
315
3163.  测试模块的配置文件(BUILD.gn)样例:
317
318    在每个测试模块目录下新建BUILD.gn编译文件,用于指定编译后静态库的名称、依赖的头文件、依赖的库等;具体写法如下:
319
320    ```
321    import("//test/xts/tools/lite/build/suite_lite.gni")
322    hctest_suite("ActsDemoTest") {
323        suite_name = "acts"
324        sources = [
325            "src/test_demo.c",
326        ]
327        include_dirs = [ ]
328        cflags = [ "-Wno-error" ]
329    }
330    ```
331
3324.  acts下BUILD.gn增加编译选项。
333
334    需要将测试模块加入到acts目录下的编译脚本中,编译脚本路径:test/xts/acts/build\_lite/BUILD.gn335
336    ```
337    lite_component("acts") {
338        ...
339        if(board_name == "liteos_m") {
340            features += [
341                ...
342                "//xts/acts/subsystem_lite/module_hal:ActsDemoTest"
343            ]
344        }
345    }
346    ```
347
3485.  测试套件编译命令_两种编译方式。
349
350    方式一:
351
352    ```
353    ./test/xts/tools/lite/build.sh product=wifiiot xts=acts
354    ```
355
356    方式二:
357
358    ```
359    hb set
360    选择 设备类型
361    hb build --gn-args build_xts=true
362    (注):若不追加--gn-args build_xts=true,不会编译acts测试套件。
363    ```
364
365    >![](figures/icon-note.gif) **说明:**
366    >acts测试套件编译中间件为静态库,最终链接到版本镜像中 。
367
368
369### C语言用例执行指导(适用于轻量系统产品用例开发)<a name="section13820233175418"></a>
370
371**示例:轻量系统测试用例执行**
372
373将版本镜像烧录进开发板。
374
375**测试步骤**
376
3771.  使用串口工具登录开发板,并保存串口打印信息。
3782.  重启设备,查看串口日志。
379
380**测试结果分析指导**
381
382基于串口打印日志进行分析;
383
384每个测试套件执行以Start to run test suite开始,以xx Tests xx Failures xx Ignored结束。
385
386### C++语言用例开发编译指导(适用于小型系统、标准系统用例开发)<a name="section3822123311540"></a>
387
388**示例:小型系统测试用例开发**(标准系统参考具体样例目录:global/i18n\_standard)
389
390当前使用的测试框架是hcpptest,hcpptest测试框架是在开源的googletest测试框架的基础上进行的增强和适配。
391
3921.  规范用例目录:测试用例存储到test/xts/acts仓中。
393
394    ```
395    ├── acts
396    │ └──subsystem_lite
397    │ │ └── module_posix
398    │ │ │ └── BUILD.gn
399    │ │ │ └── src
400    │ └──build_lite
401    │ │ └── BUILD.gn
402    ```
403
4042.  测试模块src下用例编写样例:
405
406    1. 引用测试框架:
407
408    需要引用gtest.h  如:\#include "gtest/gtest.h"
409
410    ```
411    #include "gtest/gtest.h"
412    ```
413
414    2. 定义Setup与TearDown
415
416    ```
417    using namespace std;
418    using namespace testing::ext;
419    class TestSuite: public testing::Test {
420    protected:
421    // Preset action of the test suite, which is executed before the first test case
422    static void SetUpTestCase(void){
423    }
424    // Test suite cleanup action, which is executed after the last test case
425    static void TearDownTestCase(void){
426    }
427    // Preset action of the test case
428    virtual void SetUp()
429    {
430    }
431    // Cleanup action of the test case
432    virtual void TearDown()
433    {
434    }
435    };
436    ```
437
438    3. 使用宏定义HWTEST或HWTEST\_F写测试用例
439
440    普通测试用例的定义:HWTEST(测试套名称, 测试用例名称, 用例标注)。
441
442    包含SetUp和TearDown的测试用例的定义 :HWTEST\_F(测试套名称, 测试用例名称,用例标注)。
443
444    宏定义包括三个参数:测试套件名称,测试用例名称,用例属性(测试类型、用例粒度、用例级别)。
445
446    ```
447    HWTEST_F(TestSuite, TestCase_0001, Function | MediumTest | Level1) {
448    // do something
449    }
450    ```
451
4523.  测试模块下用例配置文件(BUILD.gn)样例:
453
454    每个测试模块目录下新建BUILD.gn编译文件,用于指定编译后可执行文件的名称、依赖的头文件、依赖的库等;具体写法如下。每个测试模块将独立编译成.bin可执行文件, 该文件可直接push到单板上进行测试。
455
456    举例:
457
458    ```
459    import("//test/xts/tools/lite/build/suite_lite.gni")
460    hcpptest_suite("ActsDemoTest") {
461        suite_name = "acts"
462        sources = [
463            "src/TestDemo.cpp"
464        ]
465
466        include_dirs = [
467            "src",
468            ...
469        ]
470        deps = [
471            ...
472        ]
473        cflags = [ "-Wno-error" ]
474    }
475
476    ```
477
4784.  acts目录下增加编译选项(BUILD.gn)样例:
479
480    将测试模块加入到acts目录下的编译脚本中,编译脚本为:test/xts/acts/build\_lite/BUILD.gn481
482    ```
483     lite_component("acts") {
484    ...
485    else if(board_name == "liteos_a") {
486            features += [
487                ...
488                "//xts/acts/subsystem_lite/module_posix:ActsDemoTest"
489            ]
490        }
491    }
492    ```
493
4945.  测试套件编译命令_两种编译方式。
495
496    L1_LiteOS:
497
498    ```
499    方式一:
500    python3 build.py -p ipcamera_hispark_taurus@hisilicon --gn-args build_xts=true
501    方式二:
502    hb set
503    选择 设备类型
504    hb build --gn-args build_xts=true
505    (注):若不追加--gn-args build_xts=true,不会编译acts测试套件。
506    ```
507
508    L1_Linux:
509
510    ```
511    方式一:
512    python3 build.py -p ipcamera_hispark_taurus_linux@hisilicon --gn-args build_xts=true
513    方式二:
514    hb set
515    选择 设备类型
516    hb build --gn-args build_xts=true
517    (注):若不追加--gn-args build_xts=true,不会编译acts测试套件。
518    ```
519
520    >![](figures/icon-note.gif) **说明:**
521    >小型系统acts独立编译成可执行文件(bin格式), 在编译产物的suites\\acts目录下归档。
522
523
524### C++语言用例执行指导(适用于小型系统、标准系统用例开发)<a name="section128222336544"></a>
525
526**示例:小型系统测试用例执行**
527
528目前的用例执行采用nfs共享的方式,mount到单板去执行。
529
530**环境搭建**
531
5321.  使用网线或无线网络将开发板与PC进行连接。
5332.  开发板配置IP、子网掩码、网关,确保开发板与PC处于同一个网段。
5343.  PC安装nfs服务器并完成注册,启动nfs服务。
5354.  开发板配置mount命令,确保开发板可以访问PC端的nfs共享文件。
536
537    格式:mount \[nfs服务器IP\]:\[/nfs共享目录\] \[/开发板目录\] nfs
538
539    举例:
540
541    ```
542    mount 192.168.1.10:/nfs /nfs nfs
543    ```
544
545
546**用例执行**
547
548测试套件执行 ActsDemoTest.bin 触发用例执行,基于串口打印日志进行分析。
549
550### JS语言用例开发指导(适用于标准系统)<a name="section159801435165220"></a>
551
552当前使用的测试框架是HJSUnit,用于支撑OpenHarmony application测试(特指基于JS应用框架使用 Javascript 语言开发的 APP)进行自动化测试。
553
554**用例编写基础语法**
555
556测试用例为 js 语言,必须满足 JavaScript 语言编程规范:
557
558**表 5**
559
560<a name="table13980103565212"></a>
561<table><thead align="left"><tr id="row1098093545218"><th class="cellrowborder" valign="top" width="17.92179217921792%" id="mcps1.2.4.1.1"><p id="p49811235135214"><a name="p49811235135214"></a><a name="p49811235135214"></a>用例语法</p>
562</th>
563<th class="cellrowborder" valign="top" width="68.88688868886888%" id="mcps1.2.4.1.2"><p id="p898183565220"><a name="p898183565220"></a><a name="p898183565220"></a>描述</p>
564</th>
565<th class="cellrowborder" valign="top" width="13.19131913191319%" id="mcps1.2.4.1.3"><p id="p17981143512526"><a name="p17981143512526"></a><a name="p17981143512526"></a>要求</p>
566</th>
567</tr>
568</thead>
569<tbody><tr id="row1598183515528"><td class="cellrowborder" valign="top" width="17.92179217921792%" headers="mcps1.2.4.1.1 "><p id="p139810358527"><a name="p139810358527"></a><a name="p139810358527"></a>beforeAll</p>
570</td>
571<td class="cellrowborder" valign="top" width="68.88688868886888%" headers="mcps1.2.4.1.2 "><p id="p198111352526"><a name="p198111352526"></a><a name="p198111352526"></a>测试套级别的预置条件,在所有测试用例开始前执行且仅执行一次,支持一个参数:预置动作函数</p>
572</td>
573<td class="cellrowborder" valign="top" width="13.19131913191319%" headers="mcps1.2.4.1.3 "><p id="p298114355529"><a name="p298114355529"></a><a name="p298114355529"></a>可选</p>
574</td>
575</tr>
576<tr id="row11981103512529"><td class="cellrowborder" valign="top" width="17.92179217921792%" headers="mcps1.2.4.1.1 "><p id="p20981135135215"><a name="p20981135135215"></a><a name="p20981135135215"></a>afterAll</p>
577</td>
578<td class="cellrowborder" valign="top" width="68.88688868886888%" headers="mcps1.2.4.1.2 "><p id="p39811935115217"><a name="p39811935115217"></a><a name="p39811935115217"></a>测试套级别的清理条件,在所有测试用例结束后执行且仅执行一次,支持一个参数:清理动作函数</p>
579</td>
580<td class="cellrowborder" valign="top" width="13.19131913191319%" headers="mcps1.2.4.1.3 "><p id="p998110354527"><a name="p998110354527"></a><a name="p998110354527"></a>可选</p>
581</td>
582</tr>
583<tr id="row1298116359526"><td class="cellrowborder" valign="top" width="17.92179217921792%" headers="mcps1.2.4.1.1 "><p id="p898112359526"><a name="p898112359526"></a><a name="p898112359526"></a>beforeEach</p>
584</td>
585<td class="cellrowborder" valign="top" width="68.88688868886888%" headers="mcps1.2.4.1.2 "><p id="p149811335175215"><a name="p149811335175215"></a><a name="p149811335175215"></a>测试用例级别的预置条件,在每条测试用例开始前执行,执行次数与 it 定义的测试用例数一致,支持一个参数:预置动作函数</p>
586</td>
587<td class="cellrowborder" valign="top" width="13.19131913191319%" headers="mcps1.2.4.1.3 "><p id="p13981133585212"><a name="p13981133585212"></a><a name="p13981133585212"></a>可选</p>
588</td>
589</tr>
590<tr id="row6982435115219"><td class="cellrowborder" valign="top" width="17.92179217921792%" headers="mcps1.2.4.1.1 "><p id="p19982133517525"><a name="p19982133517525"></a><a name="p19982133517525"></a>afterEach</p>
591</td>
592<td class="cellrowborder" valign="top" width="68.88688868886888%" headers="mcps1.2.4.1.2 "><p id="p1398213575219"><a name="p1398213575219"></a><a name="p1398213575219"></a>测试用例级别的清理条件,在每条测试用例结束后执行,执行次数与 it 定义的测试用例数一致,支持一个参数:清理动作函数</p>
593</td>
594<td class="cellrowborder" valign="top" width="13.19131913191319%" headers="mcps1.2.4.1.3 "><p id="p159821535195219"><a name="p159821535195219"></a><a name="p159821535195219"></a>可选</p>
595</td>
596</tr>
597<tr id="row16982143513521"><td class="cellrowborder" valign="top" width="17.92179217921792%" headers="mcps1.2.4.1.1 "><p id="p598203510527"><a name="p598203510527"></a><a name="p598203510527"></a>describe</p>
598</td>
599<td class="cellrowborder" valign="top" width="68.88688868886888%" headers="mcps1.2.4.1.2 "><p id="p5982123595215"><a name="p5982123595215"></a><a name="p5982123595215"></a>定义一个测试套,支持两个参数:测试套名称和测试套函数; describe 支持嵌套,每个 describe 内均可以定义 beforeAll 、beforeEach 、afterEach 和 afterAll</p>
600</td>
601<td class="cellrowborder" valign="top" width="13.19131913191319%" headers="mcps1.2.4.1.3 "><p id="p898217352527"><a name="p898217352527"></a><a name="p898217352527"></a>必选</p>
602</td>
603</tr>
604<tr id="row6982113518526"><td class="cellrowborder" valign="top" width="17.92179217921792%" headers="mcps1.2.4.1.1 "><p id="p17982123510526"><a name="p17982123510526"></a><a name="p17982123510526"></a>it</p>
605</td>
606<td class="cellrowborder" valign="top" width="68.88688868886888%" headers="mcps1.2.4.1.2 "><p id="p598211352522"><a name="p598211352522"></a><a name="p598211352522"></a>定义一条测试用例,支持三个参数:用例名称,过滤参数和用例函数</p>
607<p id="p5205769588"><a name="p5205769588"></a><a name="p5205769588"></a>备注:</p>
608<p id="p6965058101"><a name="p6965058101"></a><a name="p6965058101"></a>过滤参数:过滤参数为一个 32 位的 Int 类型参数,0 位 置1表示不筛选、默认执行;0-10 位 置1表示<strong id="b9169154410212"><a name="b9169154410212"></a><a name="b9169154410212"></a>测试用例类型</strong>;16-18 位 置1表示<strong id="b1427947428"><a name="b1427947428"></a><a name="b1427947428"></a>测试用例规模</strong>;24-28 位 置1表示<strong id="b289818491213"><a name="b289818491213"></a><a name="b289818491213"></a>测试层级</strong>。</p>
609<p id="p7965165151011"><a name="p7965165151011"></a><a name="p7965165151011"></a><strong id="b69449534215"><a name="b69449534215"></a><a name="b69449534215"></a>测试用例类型</strong>。置位0-10分别表示:FUNCTION 方法类测试、PERFORMANCE 性能类测试、POWER 功耗类测试、RELIABILITY 可靠性测试、SECURITY 安全合规测试、GLOBAL 整体性测试、COMPATIBILITY 兼容性测试、USER 用户测试、STANDARD 标准测试、SAFETY 安全特性测试,RESILIENCE 压力测试。</p>
610<p id="p199651555102"><a name="p199651555102"></a><a name="p199651555102"></a><strong id="b10114125712211"><a name="b10114125712211"></a><a name="b10114125712211"></a>测试用例规模</strong>。置位16-18分别表示:SMALL 小型测试、MEDIUM 中型测试、LARGE 大型测试。</p>
611<p id="p296545151020"><a name="p296545151020"></a><a name="p296545151020"></a><strong id="b167975581223"><a name="b167975581223"></a><a name="b167975581223"></a>测试层级</strong>。置位24-28分别表示:LEVEL0-0 级测试、LEVEL1-1 级测试、LEVEL2-2 级测试、LEVEL3-3 级测试、LEVEL4-4 级测试。</p>
612</td>
613<td class="cellrowborder" valign="top" width="13.19131913191319%" headers="mcps1.2.4.1.3 "><p id="p17601752115716"><a name="p17601752115716"></a><a name="p17601752115716"></a>必选</p>
614</td>
615</tr>
616</tbody>
617</table>
618
619用例编写语法采用 jasmine 的标准语法,格式支持ES6格式。
620
621**以FA 模式为例:**
622
6231.  规范用例目录:测试用例存储到 src/main/js/test目录。
624
625    ```
626    ├── BUILD.gn
627    ├── Test.json                    # 资源依赖hap不需要Test.json文件
628    ├── signature
629    │ └──openharmony_sx.p7b          # 签名工具
630    └──src
631    │ └──main
632    │ │ └──js
633    │ │ │ └──MainAbility
634    │ │ │ │ └──app.js
635    │ │ │ │ └──pages
636    │ │ │ │ │ └──index
637    │ │ │ │ │ │ └──index.js
638    │ │ │ └──test                   # 测试代码存放目录
639    │ │ │ │ │ └──List.test.js
640    │ │ │ │ │ └──Ability.test.js
641    │ │ │ └──TestAbility            # 测试框架入口模板文件,添加后无需修改
642    │ │ │ │ └──app.js
643    │ │ │ │ └──pages
644    │ │ │ │ │ └──index
645    │ │ │ │ │ │ └──index.js
646    │ │ │ └──TestRunner             # 测试框架入口模板文件,添加后无需修改
647    │ │ │ │ └──OpenHarmonyTestRunner.js
648    │ └── resources                 # hap资源存放目录
649    │ └── config.json               # hap配置文件
650    ```
651
6522.  OpenHarmonyTestRunner.js 示例
653
654    ```
655    //加载js 测试框架
656    import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'
657
658    ...
659
660     export default {
661        ...
662        onRun() {
663            console.log('OpenHarmonyTestRunner onRun run')
664            var abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
665            var abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
666
667            var testAbilityName = abilityDelegatorArguments.parameters['-p'] + '.MainAbility'
668
669            var cmd = 'aa start -d 0 -a ' + testAbilityName + ' -b ' + abilityDelegatorArguments.bundleName
670            ...
671        }
672    };
673    ```
674
6753.  index.js示例
676
677    ```
678    export default {
679        ...
680        onShow() {
681            console.info('onShow finish!')
682        },
683        ...
684    }
685    ```
686
6874.  app.js示例
688
689    ```
690    //加载测试用例
691    import { Hypium } from '@ohos/hypium'
692    import testsuite from '../test/List.test'
693    export default {
694        onCreate() {
695            console.info('TestApplication onCreate');
696            var abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
697            var abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
698            console.info('start run testcase!!!')
699            Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite)
700        },
701        ...
702    };
703    ```
704
7055.  单元测试用例示例
706
707    ```
708    // Example1: 使用HJSUnit进行单元测试
709    describe('appInfoTest', function () {
710        it('app_info_test_001', 0, function () {
711            var info = app.getInfo()
712            expect(info.versionName).assertEqual('1.0')
713            expect(info.versionCode).assertEqual('3')
714        })
715    })
716    ```
717
718
719FA_JS 模式测试模块下用例配置文件(BUILD.gn)样例:
720
721```
722import("//test/xts/tools/build/suite.gni")
723
724ohos_js_hap_suite("ActsDemoTest") {
725  hap_profile = "./src/main/config.json"
726  deps = [
727    ":hjs_demo_js_assets",
728    ":hjs_demo_resources",
729  ]
730  certificate_profile = "./signature/openharmony_sx.p7b"    //签名文件
731  hap_name = "ActsDemoTest"                                 //测试套件,以Acts开头,以Test结尾,采用驼峰式命名
732  part_name = "..."                                         //部件
733  subsystem_name = "..."                                    //子系统
734}
735ohos_js_assets("hjs_demo_js_assets") {
736  js2abc = true
737  hap_profile = "./src/main/config.json"
738  source_dir = "./src/main/js"
739}
740ohos_resources("hjs_demo_resources") {
741  sources = [ "./src/main/resources" ]
742  hap_profile = "./src/main/config.json"
743}
744```
745
746FA_TS 模式测试模块下用例配置文件(BUILD.gn)样例:
747
748```
749import("//test/xts/tools/build/suite.gni")
750
751ohos_js_hap_suite("ActsDemoTest") {
752  hap_profile = "./src/main/config.json"
753  deps = [
754    ":ace_demo_ets_assets",
755    ":ace_demo_ets_resources",
756    ":ace_demo_ets_test_assets",
757  ]
758  ets2abc = true
759  certificate_profile = "./signature/openharmony_sx.p7b"   //签名文件
760  hap_name = "ActsDemoTest"                                //测试套件,以Acts开头,以Test结尾,采用驼峰式命名
761  part_name = "..."                                        //部件
762  subsystem_name = "..."                                   //子系统
763}
764ohos_js_assets("ace_demo_ets_assets") {
765  source_dir = "./src/main/ets/MainAbility"
766}
767ohos_js_assets("ace_demo_ets_test_assets") {
768  source_dir = "./src/main/ets/TestAbility"
769}
770ohos_resources("ace_demo_ets_resources") {
771  sources = [ "./src/main/resources" ]
772  hap_profile = "./src/main/config.json"
773}
774```
775
776FA_JS 模式适配指导请参考
777
778​                  [JS工程目录结构(FA模型)](https://gitee.com/openharmony/xts_acts/wikis/XTS_%E7%99%BE%E7%A7%91%E6%8C%87%E5%AF%BC/JS%E5%B7%A5%E7%A8%8B%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84%EF%BC%88FA%E6%A8%A1%E5%9E%8B%EF%BC%89)
779
780FA_TS 模式适配指导请参考
781
782​                  [eTS工程目录结构(FA模型)](https://gitee.com/openharmony/xts_acts/wikis/XTS_%E7%99%BE%E7%A7%91%E6%8C%87%E5%AF%BC/eTS%E5%B7%A5%E7%A8%8B%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84%EF%BC%88FA%E6%A8%A1%E5%9E%8B%EF%BC%89)
783
784**以Stage 模式为例:**
785
7861. 规范用例目录:测试用例存储到 src/main/js/test目录。
787
788   ```
789   ├── BUILD.gn                     # 配置文件
790   ├── Test.json                    # 资源依赖hap不需要Test.json文件
791   ├── signature
792   │ └──openharmony_sx.p7b          # 签名工具
793   ├── AppScope
794   │ └──resource
795   │ └──app.json
796   ├── entry
797   │ └──src
798   │ │ └──main
799   │ │ │ └──ets
800   │ │ │ │ └──test                   # 测试代码存放目录
801   │ │ │ │ │ └──List.test.ets
802   │ │ │ │ │ └──Ability.test.ets
803   │ │ │ │ └──MainAbility
804   │ │ │ │ │ └──MainAbility.ts
805   │ │ │ │ │ └──pages
806   │ │ │ │ │ │ └──index
807   │ │ │ │ │ │ │ └──index.ets
808   │ │ │ │ └──TestAbility
809   │ │ │ │ │ └──TestAbility.ts       # 测试用例启动入口 ability
810   │ │ │ │ │ └──pages
811   │ │ │ │ │ │ └──index.ets
812   │ │ │ │ └──Application
813   │ │ │ │ │ └──AbilityStage.ts
814   │ │ │ │ └──TestRunner             # 测试框架入口模板文件,添加后无需修改
815   │ │ │ │ │ └──OpenHarmonyTestRunner.js
816   │ │ └── resources                 # hap资源存放目录
817   │ │ └── module.json               # hap配置文件
818   ```
819
8202. OpenHarmonyTestRunner.ts 示例
821
822   【注】在TestRunner目录下的 OpenHarmonyTestRunner.ts 文件中的 async onRun() 方法下存在拉起测试套入口xxxAbility的cmd 命令:
823
824   例如:
825
826   var cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName
827
828   需与module.json中 "abilities" 下的 "name" 字段保持一致,保证拉起的是我们需要的测试入口。
829
830   ```
831   import TestRunner from '@ohos.application.testRunner'
832   import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'
833
834   ...
835
836   export default class OpenHarmonyTestRunner implements TestRunner {
837       ...
838       async onRun() {
839           console.log('OpenHarmonyTestRunner onRun run')
840           abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
841           abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
842           var testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility'
843           let lMonitor = {
844               abilityName: testAbilityName,
845               onAbilityCreate: onAbilityCreateCallback,
846           };
847           abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback)
848           var cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName
849           ...
850       }
851   };
852   ```
853
8543. index.ets示例
855
856   ```
857   import router from '@ohos.router';
858
859   @Entry
860   @Component
861   struct Index {
862
863     aboutToAppear(){
864       console.info("start run testcase!!!!")
865     }
866
867     build() {
868       ...
869     }
870   }
871   ```
872
8734. app.js示例
874
875   ```
876   //加载测试用例
877   import Ability from '@ohos.app.ability.UIAbility'
878   import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'
879   import { Hypium } from '@ohos/hypium'
880   import testsuite from '../test/List.test'
881
882   export default class TestAbility extends Ability {
883       onCreate(want, launchParam) {
884           console.log('TestAbility onCreate')
885           var abilityDelegator: any
886           abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
887           var abilityDelegatorArguments: any
888           abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
889           console.info('start run testcase!!!')
890           Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite)
891       }
892       ...
893   };
894   ```
895
896Stage 模式测试模块下用例配置文件(BUILD.gn)样例:
897
898```
899import("//test/xts/tools/build/suite.gni")
900
901ohos_js_hap_suite("ActsDemoTest") {
902  hap_profile = "/src/main/module.json"
903  js_build_mode = "debug"
904  deps = [
905    ":edm_js_assets",
906    ":edm_resources",
907  ]
908  ets2abc = true
909  certificate_profile = "signature/openharmony_sx.p7b"     //签名文件
910  hap_name = "ActsDemoTest"                                //测试套件,以Acts开头,以Test结尾,采用驼峰式命名
911  subsystem_name = "customization"                         //子系统
912  part_name = "enterprise_device_management"               //部件
913}
914
915ohos_app_scope("edm_app_profile") {
916  app_profile = "AppScope/app.json"
917  sources = [ "AppScope/resources" ]
918}
919
920ohos_js_assets("edm_js_assets") {
921  source_dir = "/src/main/ets"
922}
923
924ohos_resources("edm_resources") {
925  sources = [ "/src/main/resources" ]
926  deps = [ ":edm_app_profile" ]
927  hap_profile = "/src/main/module.json"
928}
929```
930
931Stage 模式适配指导请参考
932
933​                          [eTS工程目录结构(Stage模型)](https://gitee.com/openharmony/xts_acts/wikis/XTS_%E7%99%BE%E7%A7%91%E6%8C%87%E5%AF%BC/eTS%E5%B7%A5%E7%A8%8B%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84%EF%BC%88Stage%E6%A8%A1%E5%9E%8B%EF%BC%89)
934
935### JS语言用例编译打包指导(适用于标准系统)<a name="section445519106559"></a>
936
937hap包编译请参考 [标准系统 JS用例源码编译Hap包指导](https://gitee.com/openharmony/xts_acts/wikis/%E6%A0%87%E5%87%86%E7%B3%BB%E7%BB%9F%20JS%E7%94%A8%E4%BE%8B%E6%BA%90%E7%A0%81%E7%BC%96%E8%AF%91Hap%E5%8C%85%E6%8C%87%E5%AF%BC%20?sort_id=4427112)938
939### Python用例开发编译指导 <a name="section281040146993"></a>
940
941XTS当前支持执行Python测试用例,可用于系统黑盒测试,并提供了对设备进行操作的接口,包括执行Shell命令、文件传输、应用安装卸载等。
942
943**用例开发指导**
944
945Python用例开发有两种模式,分别是测试用例模式和测试套模式。
946
9471. 测试用例模式
948
949测试用例由一个测试用例json配置文件和一个Python测试脚本py文件组成。
950
951用例目录示例:
952```
953├── BUILD.gn                     # 编译配置文件
954├── TestCase.json                # 测试用例配置文件
955├── testcase1.py                 # 测试用例Python脚本
956```
957
958json配置文件描述测试用例的配置信息,如环境设备信息、测试驱动信息、测试所需kit等。其中需指定测试用例执行驱动类型为 'DeviceTest',并指定Python测试脚本路径(测试用例模式仅可指定一个py文件)。测试用例配置文件示例:
959```
960// TestCase.json
961
962{
963    "description": "Configuration for Openharmony device test case",
964    // environment 字段描述测试套执行所需环境信息,如设备数量、类型等
965    "environment": [
966        {
967            "type": "device",       // device 表示 OpenHarmony 设备
968            "label": "phone"
969        }
970    ],
971    // driver 字段描述测试用例执行所需驱动、测试用例文件路径等
972    "driver": {
973        "type": "DeviceTest",
974        "py_file": ["testcase1.py"]
975    },
976    // kits 字段描述测试用例需要的测试公共kit,如 pushkit 、 shellkit 等
977    "kits": [
978    ]
979}
980```
981
982<a name="testcase"></a>
983
984Python测试用例继承'TestCase',需重写三个生命周期函数。测试用例Python脚本示例如下:
985| 生命周期函数 | 说明 |
986| ----- | ----- |
987| setup | 测试用例前置步骤,用于执行测试用例的预置动作 |
988| process | 测试用例实际操作步骤,用于执行测试用例实际测试步骤 |
989| teardown | 测试用例清理步骤,用于执行测试用例的环境清理等操作 |
990
991```
992// testcase1.py
993
994#!/usr/bin/env python3
995# -*- coding: utf-8 -*-
996
997from devicetest.core.test_case import TestCase, Step
998
999class testcase1(TestCase):
1000    def __init__(self, controllers):
1001        self.TAG = self.__class__.__name__
1002        super().__init__(self.TAG, controllers)
1003
1004    def setup(self):
1005        Step("Setup")
1006
1007    def process(self):
1008        Step("Process")
1009        # driver1 表示 TestCase.json 中定义的环境设备
1010        res = self.device1.execute_shell_command("ls -l")
1011        # 使用该方式打印日志,可将输出保留在测试报告中
1012        self.log.info(res)
1013        # 使用断言判断测试通过与否
1014        assert res != ""
1015
1016    def teardown(self):
1017        Step("Teardown")
1018```
1019
10202. 测试套模式
1021
1022测试套由一个测试套json配置文件、一个测试套py文件、多个Python测试脚本py文件组成。
1023
1024用例目录示例:
1025```
1026├── BUILD.gn                     # 编译配置文件
1027├── TestSuite.json               # 测试套json配置文件
1028├── TestSuite.py                 # 测试套py文件
1029├── testcase1.py                 # Python测试脚本py文件
1030├── testcase2.py
1031├── testcase3.py
1032```
1033
1034json配置文件中需指定测试套执行驱动类型为 'DeviceTestSuite',并指定测试套py文件和Python测试脚本路径。测试套配置文件示例如下:
1035```
1036// TestSuite.json
1037
1038{
1039    "description": "Configuration for Openharmony device test suite",
1040    // environment 字段描述测试套执行所需环境信息,如设备类型等
1041    "environment": [
1042        {
1043            "type": "device",           // device 表示 OpenHarmony 设备
1044            "label": "phone"
1045        }
1046    ],
1047    // driver 字段描述测试套执行所需驱动、测试套py文件路径、所包含所有测试脚本路径等
1048    "driver": {
1049        "type": "DeviceTestSuite",
1050        "testsuite": "TestSuite.py",
1051        "suitecases": [
1052            "./testcase1.py",
1053            "./testcase2.py",
1054            "./testcase3.py"
1055        ]
1056    },
1057    // kits 字段描述测试套需要的测试公共kit,如 pushkit 、 shellkit 等
1058    "kits": [
1059    ]
1060}
1061```
1062
1063> **注:**  Python测试脚本若与测试套py文件在同一目录下且以'TC_'开头,则无需在json配置文件中配置 'suitecases',框架将自动扫描执行所有符合条件的测试用例。(若已配置 'suitecases' 且不为空,框架则不再自动扫描)
1064
1065测试套需继承'TestSuite',并重写两个生命周期函数。 测试套py文件示例如下:
1066
1067| 生命周期函数 | 说明 |
1068| ----- | ----- |
1069| setup | 整个测试套的前置步骤,在测试套运行前先执行该函数 |
1070| teardown | 整个测试套的清理步骤,在测试套运行后执行该函数 |
1071
1072```
1073// TestSuite.py
1074
1075#!/usr/bin/env python3
1076# -*- coding: utf-8 -*-
1077
1078from devicetest.core.test_case import Step
1079from devicetest.core.suite.test_suite import TestSuite
1080
1081class TestSuite(TestSuite):
1082    def setup(self):
1083        Step("Setup")
1084
1085    def teardown(self):
1086        Step("Teardown")
1087```
1088
1089[测试套模式Python测试脚本示例,同测试用例模式](#testcase)
1090
1091**接口参考**
1092
1093| <div style="width:300px">接口定义</div> | 参数说明 | 返回值 | 接口说明 |
1094| ----- | ----- | ----- | ----- |
1095| execute_shell_command(command, timeout) | 1. command:必选,shell命令字符串 <br> 2. timeout:可选,命令执行超时时间,单位 ms,执行超时抛出异常 | shell命令执行输出 | 执行shell命令 |
1096| reboot() | 无 | 无 | 设备重启 |
1097| install_package(package_path) | package_path:必选,应用包路径 | bm安装命令执行输出 | 安装应用 |
1098| uninstall_package(package_name) | package_name:必选,应用包名 | bm卸载命令执行输出 | 卸载应用 |
1099| pull_file(remote, local) | 1. remote:必选,设备文件路径 <br> 2. local:必选,本地文件路径 | 无 | 设备pull文件到本地 |
1100| push_file(local, remote) | 1. local:必选,本地文件路径 <br> 2. remote:必选,设备文件路径 | 无 | 本地push文件到设备 |
1101
1102> **注:**  ```install_package()```、```push_file()``` 等以本地路径作为参数的接口,需要将本地文件放置到 resource 目录下并通过 ```get_resource_path()``` 获取本地文件路径,示例如下:
1103> ```
1104> from devicetest.utils.file_util import get_resource_path
1105>
1106> path = get_resource_path('test.hap') // resource目录下的相对路径
1107> res = self.device1.install_package(path)
1108> ```
1109
1110**用例编译指导**
1111
1112编译配置文件 BUILD.gn 主要将测试用例py文件及json文件拷贝到xts输出目录下。编译配置文件示例如下(以测试用例模式为例):
1113```
1114// BUILD.gn
1115
1116import("//build/ohos.gni")
1117
1118group("ActsDemoTest") {
1119  testonly = true
1120  deps = [ ":PyTestCase" ]
1121}
1122ohos_copy("PyTestCase") {
1123  subsystem_name = "..."
1124  part_name = "..."
1125  sources = [
1126    "./TestCase.json",
1127    "./testcase1.py",
1128  ]
1129  outputs =
1130      [ root_out_dir + "/suites/acts/acts/testcases/{{source_file_part}}" ]
1131}
1132```
1133
1134### Python脚本用例执行指导 <a name="section637497153319"></a>
1135
1136**测试用例模式:**
1137
1138``` run -l TestCase ```
1139
1140**测试套模式:**
1141
1142全量执行:``` run -l TestSuite ```
1143
1144部分执行:``` run -l TestSuite -ta class:testcase1,testcase3 ```
1145
1146部分不执行:``` run -l TestSuite -ta notClass:testcase1,testcase3 ```
1147
1148### 全量编译指导(适用于标准系统)<a name="section159801435165220"></a>
1149
11501.  全量编译
1151    test/xts/acts目录下执行编译命令:
1152
1153    ```
1154    ./build.sh product_name=rk3568 system_size=standard
1155    ```
1156
11572.  单个子系统编译
1158
1159    test/xts/acts目录下执行编译命令:
1160
1161    ```
1162    ./build.sh product_name=rk3568 system_size=standard target_subsystem=××××
1163    ```
1164
11653.  单模块编译
1166
1167    test/xts/acts目录下执行编译命令:
1168
1169    ```./build.sh suite=acts system_size=standard target_subsystem=××××
1170    ./build.sh product_name=rk3568 system_size=standard suite=xxx
1171    suite 后面添加的是BUILD.gn 中ohos_js_hap_suite模板的命名
1172    ```
1173
1174    测试用例输出目录:out/rk3568/suites/acts/testcases
1175
1176    测试框架&用例整体输出目录:out/rk3568/suites/acts(编译用例时会同步编译测试套执行框架)
1177
1178### 全量用例执行指导(适用于小型系统、标准系统)<a name="section159801435165220"></a>
1179
1180搭建测试环境Windows工作台下安装python3.7及以上版本,确保工作台和测试设备正常连接。
1181
1182**注:高版本python不带 easy_install,可以安装低于 52.0.0 的 setuptools,解决该问题。**
1183
1184```
1185pip install setuptools==50.0.0
1186```
1187
1188测试执行目录(对应编译生成的out/release/suites/acts目录)
1189
1190    ├── testcase                       # 测试套文件存放目录
1191    │ └──xxx.hap                       # 测试套可执行hap文件
1192    │ └──xxx.json                      # 测试套对应执行配置文件
1193    ├── tools                          # 测试框架工具目录
1194    ├── run.bat                        # window平台测试套启动执行文件
1195    ├── report                         # 测试报告生成目录
1196
1197用例执行
11981.  在Windows工作台上,找到从Linux服务器上拷贝下来的测试套件用例目录,在Windows命令窗口进入对应目录,直接执行acts\run.bat1199
12002.  界面启动后,输入用例执行指令。
1201
1202    全量执行:```run acts ```
1203
1204    模块执行(具体模块可以查看\acts\testcases\):```run –l ActsSamgrTest ```
1205
1206    单包执行(具体模块可以查看\acts\testcases\):(适用于OH驱动)
1207
1208    ```
1209    run -l uitestActs -ta class:UiTestCase#testChecked
1210
1211    uitestActs: 测试hap
1212    UiTestCase: testsuite
1213    testChecked: testcase
1214    ```
1215
1216
12173.  查看测试报告。
1218    进入acts\reports\,获取当前的执行记录,打开“summary_report.html”可以获取到测试报告。
1219
1220## 相关仓<a name="section1371113476307"></a>
1221
1222**xts\_acts**
1223
1224[xts\_tools](https://gitee.com/openharmony/xts_tools)
1225
1226