1# XTS Test Case Development Guide 2 3 4## Introduction 5 6The X test suite (XTS) subsystem contains a set of OpenHarmony compatibility test suites, including the currently supported application compatibility test suite (ACTS) and the device compatibility test suite (DCTS) that will be supported in the future. 7 8This subsystem contains the ACTS and **tools** software package. 9 10- The **acts** directory stores the source code and configuration files of ACTS test cases. The ACTS helps device vendors detect the software incompatibility as early as possible and ensures that the software is compatible with OpenHarmony during the entire development process. 11 12- The **tools** software package stores the test case development framework related to **acts**. 13 14 15## System Types 16 17The following system types are supported: 18 19- Mini system 20 The mini system fits into the devices that come with MCU processors, such as Arm Cortex-M and 32-bit RISC-V, and memory greater than or equal to 128 KiB. This system provides a variety of lightweight network protocols, a lightweight graphics framework, and a wide range of read/write components with the Internet of Things (IoT) bus. Typical products include connection modules, sensors, and wearables for smart home. 21 22- Small system 23 The small system fits into the devices that come with application processors, such as Arm Cortex-A, and memory greater than or equal to 1 MiB. This system provides higher security capabilities, a standard graphics framework, and video encoding and decoding capabilities. Typical products include smart home IP cameras, electronic cat eyes, and routers, and event data recorders (EDRs) for easy travel. 24 25- Standard system 26 The standard system fits into the devices that come with application processors, such as Arm Cortex-A, and memory greater than or equal to 128 MiB. This system provides a complete application framework supporting enhanced interaction, 3D GPU, hardware composer, diverse components, and rich animations. The standard system applies to high-end refrigerator displays. 27 28 29## Contents 30 31 32``` 33/test/xts 34├── acts # Test code 35│ └── subsystem # Source code of subsystem test cases for the standard system 36│ └── subsystem_lite # Source code of subsystems test cases for mini and small systems 37│ └── BUILD.gn # Build configuration of test cases for the standard system 38│ └── build_lite 39│ └── BUILD.gn # Build configuration of test cases for mini and small systems 40└── tools # Test tool code 41``` 42 43 44## Constraints 45 46Test cases for the mini system must be developed based on C, and those for the small system must be developed based on C++. 47 48 49## How to Use 50 51**Table 1** Case levels 52 53| Level| Basic Definition| Test Scope| 54| -------- | -------- | -------- | 55| Level0 | Smoke| Verifies basic functionalities of key features and basic DFX attributes with the most common input. The pass result indicates that the features are runnable.| 56| Level1 | Basic| Verifies basic functionalities of key features and basic DFX attributes with common input. The pass result indicates that the features are testable.| 57| Level2 | Major| Verifies basic functionalities of key features and basic DFX attributes with common input and errors. The pass result indicates that the features are functional and ready for beta testing.| 58| Level3 | Minor| Verifies functionalities of all key features, and all DFX attributes with common and uncommon input combinations or normal and abnormal preset conditions.| 59| Level4 | Rare| Verifies functionalities of key features under extremely abnormal presets and uncommon input combinations.| 60 61**Table 2** Case scales 62 63| Case Scale| Test Object| Test Environment| 64| -------- | -------- | -------- | 65| LargeTest | Service functionalities, all-scenario features, and mechanical power environment (MPE) and scenario-level DFX| Devices close to real devices| 66| MediumTest | Modules, subsystem functionalities after module integration, and DFX| Single device that is actually used. You can perform message simulation, but do not mock functions.| 67| SmallTest | Modules, classes, and functions| Local PC. Use a large number of mocks to replace dependencies with other modules.| 68 69**Table 3** Test types 70 71| Test Type| Definition| 72| -------- | -------- | 73| Function | Tests the correctness of both service and platform functionalities provided by the tested object for end users or developers.| 74| Performance | Tests the processing capability of the tested object under specific preset conditions and load models. The processing capability is measured by the service volume that can be processed in a unit time, for example, call per second, frame per second, or event processing volume per second.| 75| Power | Tests the power consumption of the tested object in a certain period of time under specific preset conditions and load models.| 76| Reliability | Tests the service performance of the tested object under common and uncommon input conditions, or specified service volume pressure and long-term continuous running pressure. The test covers stability, pressure handling, fault injection, and Monkey test items.| 77| Security | Tests the capability of defending against security threats, including but not limited to unauthorized access, use, disclosure, damage, modification, and destruction, to ensure information confidentiality, integrity, and availability. Tests the privacy protection capability to ensure that the collection, use, retention, disclosure, and disposal of users' private data comply with laws and regulations. Tests the compliance with various security specifications, such as security design, security requirements, and security certification of the Ministry of Industry and Information Technology (MIIT).| 78| Global | Tests the internationalized data and localization capabilities of the tested object, including multi-language display, various input/output habits, time formats, and regional features, such as currency, time, and culture taboos.| 79| Compatibility | Tests backward compatibility of an application with its own data, the forward and backward compatibility with the system, and the compatibility with different user data, such as audio file content of the player and smart SMS messages. Tests system backward compatibility with its own data and the compatibility of common applications in the ecosystem. Tests software compatibility with related hardware.| 80| User | Tests user experience of the object in real user scenarios. All conclusions and comments should come from the users, which are all subjective evaluation in this case.| 81| Standard | Tests the compliance with industry and company-specific standards, protocols, and specifications. The standards here do not include any security standards that should be classified into the security test.| 82| Safety | Tests the safety property of the tested object to avoid possible hazards to personal safety, health, and the object itself.| 83| Resilience | Tests the resilience property of the tested object to ensure that it can withstand and maintain the defined running status (including downgrading) when being attacked, and recover from and adapt defense to the attacks to approach mission assurance.| 84 85 86## Test Case Development Guide 87 88You should select the appropriate programming language and your target test framework to develop test cases. 89 90**Table 4** Test frameworks and test case languages for different systems 91 92| System| Test Framework| Language| 93| -------- | -------- | -------- | 94| Mini system| hctest | c | 95| Small system| hcpptest | c++ | 96| Standard system| HJSUnit and HCPPTest| JavaScript and C++| 97 98### C-based Test Case Development and Compilation (for the Mini System) 99 100**Developing test cases for the mini system** 101 102The HCTest framework is used to support test cases developed with the C language. HCTest is enhanced and adapted based on the open-source test framework Unity. 103 1041. Access the **test/xts/acts** repository where the test cases will be stored. 105 106 ``` 107 ├── acts 108 │ └──subsystem_lite 109 │ │ └── module_hal 110 │ │ │ └── BUILD.gn 111 │ │ │ └── src 112 │ └──build_lite 113 │ │ └── BUILD.gn 114 ``` 115 1162. Write the test case in the **src** directory. 117 118 1. Import the test framework header file. 119 120 ``` 121 #include "hctest.h" 122 ``` 123 124 2. Use the **LITE_TEST_SUIT** macro to define names of the subsystem, module, and test suite. 125 126 127 ``` 128 /** 129 * @brief register a test suite named "IntTestSuite" 130 * @param test subsystem name 131 * @param example module name 132 * @param IntTestSuite test suite name 133 */ 134 LITE_TEST_SUIT(test, example, IntTestSuite); 135 ``` 136 137 3. Define SetUp and TearDown. 138 139 Format: Test suite name+SetUp, Test suite name+TearDown. 140 141 The SetUp and TearDown functions must exist, but function bodies can be empty. 142 143 4. Use the **LITE_TEST_CASE** macro to write the test case. 144 145 Three parameters are involved: test suite name, test case name, and test case properties (including type, granularity, and level). 146 ``` 147 LITE_TEST_CASE(IntTestSuite, TestCase001, Function | MediumTest | Level1) 148 { 149 //do something 150 }; 151 ``` 152 5. Use the **RUN_TEST_SUITE** macro to register the test suite. 153 154 155 ``` 156 RUN_TEST_SUITE(IntTestSuite); 157 ``` 158 1593. Create a configuration file (**BUILD.gn**) of the test module. 160 Create a **BUILD.gn** (example) build file in each test module directory. Specify the name of the built static library and its dependent header file and library in the build file. The format is as follows: 161 162 163 ``` 164 import("//test/xts/tools/lite/build/suite_lite.gni") 165 hctest_suite("ActsDemoTest") { 166 suite_name = "acts" 167 sources = [ 168 "src/test_demo.c", 169 ] 170 include_dirs = [ ] 171 cflags = [ "-Wno-error" ] 172 } 173 ``` 174 1754. Add build options to the **BUILD.gn** file in the **acts** directory. 176 You need to add the test module to the **test/xts/acts/build_lite/BUILD.gn** script in the **acts** directory. 177 178 179 ``` 180 lite_component("acts") { 181 ... 182 if(board_name == "liteos_m") { 183 features += [ 184 ... 185 "//xts/acts/subsystem_lite/module_hal:ActsDemoTest" 186 ] 187 } 188 } 189 ``` 190 1915. Run build commands. 192 Test suites are built along with the version build. The ACTS is built together with the debug version. 193 194 > **NOTE** 195 > The ACTS build middleware is a static library, which will be linked to the image. 196 197### C-based Test Case Execution (for the Mini System) 198 199**Executing test cases for the mini system** 200 201Burn the image into the development board. 202 203**Executing the test** 204 2051. Use a serial port tool to log in to the development board and save information about the serial port. 206 2072. Restart the device and view serial port logs. 208 209**Analyzing the test result** 210 211View the serial port logs, whose format is as follows: 212 213The log for each test suite starts with **Start to run test suite:** and ends with **xx Tests xx Failures xx Ignored**. 214 215 216### C++-based Test Case Development and Compilation (for Standard and Small Systems) 217 218**Developing test cases for small-system devices** (For examples of the standard system, go to the **global/i18n_standard** directory.) 219 220The HCPPTest framework is enhanced and adapted based on the open-source framework Googletest. 221 2221. Access the **test/xts/acts** repository where the test cases will be stored. 223 224 ``` 225 ├── acts 226 │ └──subsystem_lite 227 │ │ └── module_posix 228 │ │ │ └── BUILD.gn 229 │ │ │ └── src 230 │ └──build_lite 231 │ │ └── BUILD.gn 232 ``` 233 2342. Write the test case in the **src** directory. 235 1. Import the test framework header file. 236 237 The following statement includes **gtest.h**: 238 239 240 ``` 241 #include "gtest/gtest.h" 242 ``` 243 244 2. Define SetUp and TearDown. 245 246 247 ``` 248 using namespace std; 249 using namespace testing::ext; 250 class TestSuite: public testing::Test { 251 protected: 252 // Preset action of the test suite, which is executed before the first test case 253 static void SetUpTestCase(void){ 254 } 255 // Test suite cleanup action, which is executed after the last test case 256 static void TearDownTestCase(void){ 257 } 258 // Preset action of the test case 259 virtual void SetUp() 260 { 261 } 262 // Cleanup action of the test case 263 virtual void TearDown() 264 { 265 } 266 }; 267 ``` 268 269 3. Use the **HWTEST** or **HWTEST_F** macro to write the test case. 270 271 **HWTEST**: definition of common test cases, including the test suite name, test case name, and case annotation. 272 273 **HWTEST_F**: definition of SetUp and TearDown test cases, including the test suite name, test case name, and case annotation. 274 275 Three parameters are involved: test suite name, test case name, and test case properties (including type, granularity, and level). 276 277 278 ``` 279 HWTEST_F(TestSuite, TestCase_0001, Function | MediumTest | Level1) { 280 // do something 281 } 282 ``` 283 2843. Create a configuration file (**BUILD.gn**) of the test module. 285 Create a **BUILD.gn** build file in each test module directory. Specify the name of the built static library and its dependent header file and library in the build file. Each test module is independently built into a **.bin** executable file, which can be directly pushed to the development board for testing. 286 287 Example: 288 289 ``` 290 import("//test/xts/tools/lite/build/suite_lite.gni") 291 hcpptest_suite("ActsDemoTest") { 292 suite_name = "acts" 293 sources = [ 294 "src/TestDemo.cpp" 295 ] 296 297 include_dirs = [ 298 "src", 299 ... 300 ] 301 deps = [ 302 ... 303 ] 304 cflags = [ "-Wno-error" ] 305 } 306 307 ``` 308 3094. Add build options to the **BUILD.gn** file in the **acts** directory. 310 Add the test module to the **test/xts/acts/build_lite/BUILD.gn** script in the **acts** directory. 311 312 313 ``` 314 lite_component("acts") { 315 ... 316 else if(board_name == "liteos_a") { 317 features += [ 318 ... 319 "//xts/acts/subsystem_lite/module_posix:ActsDemoTest" 320 ] 321 } 322 } 323 ``` 324 3255. Run build commands. 326 Test suites are built along with the version build. The ACTS is built together with the debug version. 327 > **NOTE** 328 > 329 > The ACTS for the small system is independently built to an executable file (.bin) and archived in the **suites\acts** directory of the build result. 330 331 332### C++-based Test Case Execution (for Standard and Small Systems) 333 334**Executing test cases for the small system** 335 336Currently, test cases are shared by the NFS and mounted to the development board for execution. 337 338**Setting up the environment** 339 3401. Use a network cable or wireless network to connect the development board to your PC. 341 3422. Configure the IP address, subnet mask, and gateway for the development board. Ensure that the development board and the PC are on the same network segment. 343 3443. Install and register the NFS server on the PC and start the NFS service. 345 3464. Run the **mount** command for the development board to ensure that the development board can access NFS shared files on the PC. 347 Format: **mount** *NFS server IP address***:/***NFS shared directory* **/***development board directory* **nfs** 348 349 Example: 350 351 ``` 352 mount 192.168.1.10:/nfs /nfs nfs 353 ``` 354 355**Executing test cases** 356 357Execute **ActsDemoTest.bin** to trigger test case execution, and analyze serial port logs generated after the execution is complete. 358 359### JavaScript-based Test Case Development (for the Standard System) 360 361The HJSUnit framework is used to support automated tests of OpenHarmony applications that are developed using the JavaScript language based on the JS application framework. 362 363**Basic syntax of test cases** 364 365The test cases are developed with the JavaScript language and must meet the programming specifications of the language. 366 367**Table 7** Basic syntax of test cases 368 369| Syntax| Description| Requirement| 370| -------- | -------- | -------- | 371| beforeAll | Presets a test-suite-level action executed only once before all test cases are executed. You can pass the action function as the only parameter.| Optional| 372| afterAll | Presets a test-suite-level clear action executed only once after all test cases are executed. You can pass the clear function as the only parameter.| Optional| 373| beforeEach | Presets a test-case-level action executed before each test case is executed. The number of execution times is the same as the number of test cases defined by **it**. You can pass the action function as the only parameter.| Optional| 374| afterEach | Presets a test-case-level clear action executed after each test case is executed. The number of execution times is the same as the number of test cases defined by **it**. You can pass the clear function as the only parameter.| Optional| 375| describe | Defines a test suite. You can pass two parameters: test suite name and test suite function. **describe** supports embedding. **beforeAll**, **beforeEach**, **afterEach**, and **afterAll** can be defined in each **describe**.| Mandatory| 376| it | Defines a test case. You can pass three parameters: test case name, filter parameter, and test case function.<br>**NOTE**<br>**Filter parameter**: The filter parameter is a 32-bit parameter of the **Int** type. **1** of bit 0 indicates not to filter. **1** of bits 0-10 indicates the test case type. **1** of bits 16-18 indicates the test case scale. **1** of bits 24-28 indicates the test level.<br>**Test case type**: Bits 0-10 indicate the following respectively: FUNCTION test, PERFORMANCE test, POWER test, RELIABILITY test, SECURITY test, GLOBAL test, COMPATIBILITY test, USER test, STANDARD test, SAFETY test, and RESILIENCE test.<br>**Test case scale**: Bits 16-18 indicate the following respectively: SMALL test, MEDIUM test, and LARGE test.<br>**Test level**: Bits 24-28 indicate the following respectively: LEVEL0-0 test, LEVEL1-1 test, LEVEL2-2 test, LEVEL3-3 test, and LEVEL4-4 test.| Mandatory| 377 378Use the standard syntax of Jasmine to write test cases. The ES6 specification is supported. 379 3801. Store the test cases in the **entry/src/main/js/test** directory, whose structure is as follows: 381 382 ``` 383 ├── BUILD.gn 384 │ └──entry 385 │ │ └──src 386 │ │ │ └──main 387 │ │ │ │ └──js 388 │ │ │ │ │ └──default 389 │ │ │ │ │ │ └──pages 390 │ │ │ │ │ │ │ └──index 391 │ │ │ │ │ │ │ │ └──index.js # Entry file 392 │ │ │ │ │ └──test # Test code 393 │ │ │ └── resources # HAP resources 394 │ │ │ └── config.json # HAP configuration file 395 ``` 396 3972. Start the JS test framework and load test cases. The following is an example for **index.js**: 398 399 ``` 400 // Start the JS test framework and load test cases. 401 import {Core, ExpectExtend} from 'deccjsunit/index' 402 403 export default { 404 data: { 405 title: "" 406 }, 407 onInit() { 408 this.title = this.$t('strings.world'); 409 }, 410 onShow() { 411 console.info('onShow finish') 412 const core = Core.getInstance() 413 const expectExtend = new ExpectExtend({ 414 'id': 'extend' 415 }) 416 core.addService('expect', expectExtend) 417 core.init() 418 const configService = core.getDefaultService('config') 419 configService.setConfig(this) 420 require('../../../test/List.test') 421 core.execute() 422 }, 423 onReady() { 424 }, 425 } 426 ``` 427 4283. Write a unit test case by referring to the following example: 429 430 ``` 431 // Use HJSUnit to perform the unit test. 432 describe('appInfoTest', function () { 433 it('app_info_test_001', 0, function () { 434 var info = app.getInfo() 435 expect(info.versionName).assertEqual('1.0') 436 expect(info.versionCode).assertEqual('3') 437 }) 438 }) 439 ``` 440 441### JavaScript-based Test Case Packaging (for the Standard System) 442 443For details about HAP package compilation, see [JS application development guide for the standard system](https://developer.harmonyos.com/en/docs/documentation/doc-guides/build_overview-0000001055075201). 444 445 446## Full Compilation (for the Standard System) 447 4481. Perform full compilation. 449 Command: 450 451 452 ``` 453 ./build.sh suite=acts system_size=standard 454 ``` 455 456 Test case output directory: **out/release/suites/acts/testcases** 457 458 Test framework and case output directory: **out/release/suites/acts** (The test suite execution framework is compiled during case compilation.) 459 460 461## Full Test Case Execution (for Small and Standard Systems) 462 463**Setting up a test environment** 464 465Install Python 3.7 or a later version on a Windows environment and ensure that the Windows environment is properly connected to the test device. 466 467**Test execution directory** (corresponding to the **out/release/suites/acts** directory generated during compilation) 468 469``` 470├── testcase # Directory for storing test suite files 471│ └──xxx.hap # HAP file executed by the test suite 472│ └──xxx.json # Execution configuration file of the test suite 473├── tools # Test framework tool directory 474├── run.bat # File for starting the test suite on the Windows platform 475├── report # Directory for storing the test reports 476``` 477 478**Executing test cases** 479 4801. On the Windows environment, locate the directory in which the test cases are stored (**out/release/suites/acts**, copied from the Linux server), go to the directory in the Windows command line interface (CLI), and run **acts\run.bat**. 481 4822. Enter the command for executing the test case. 483 - Execute all test cases. 484 485 ``` 486 run acts 487 ``` 488 489 **Figure 1** Running process 490 491 ![en-us_image_0000001200230833](figures/en-us_image_0000001200230833.gif) 492 493 - Execute the test cases of a module (view specific module information in **\acts\testcases\**). 494 495 ``` 496 run –l ActsSamgrTest 497 ``` 498 499 **Figure 2** Viewing the running command 500 501 ![en-us_image_0000001154351160](figures/en-us_image_0000001154351160.jpg) 502 503 Wait until the execution is complete. 504 5053. View test reports. 506 Go to **acts\reports\**, obtain the current execution record, and open **summary_report.html** to view the test report. 507