1e41f4b71Sopenharmony_ci# Development Self-Test Framework User Guide 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Overview 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ciOpenHarmony provides you with a comprehensive development self-test framework **developer_test**. As a part of the OpenHarmony test toolset, the development self-test framework is provided for you to test the development by yourself. You can develop relevant test cases based on your test requirements to discover defects at the development phase, greatly improving the code quality. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ciThis document describes how to use the development self-test framework of OpenHarmony. 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci### Overview 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciAfter adding or modifying code, OpenHarmony developers want to quickly verify whether the modified code functions properly, and the system already has a large number of automated test cases of existing functions, such as TDD cases and XTS cases. The development self-test framework aims to help you improve your self-test efficiency so that you can quickly execute the specified automated test cases and conducting development tests at the development phase. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci### Constraints 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ciWhen executing test cases using the framework, you must connect to the OpenHarmony device in advance. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci## Environment Setup 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ciThe development self-test framework depends on the Python environment. It is required that the Python version be 3.7.5 or later. Before using the framework, you can refer to the following document for configuration. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ciClick [here](https://gitee.com/openharmony/docs/blob/master/en/device-dev/get-code/sourcecode-acquire.md) to obtain the source code. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci### Basic Self-Test Framework Environment 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci| Environment Dependency | Version | Description | 30e41f4b71Sopenharmony_ci| ----------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | 31e41f4b71Sopenharmony_ci| Operating system | Ubuntu 20.04 or later | Code compilation environment. | 32e41f4b71Sopenharmony_ci| Linux extension component| libreadline-dev | Plugin used to read commands. | 33e41f4b71Sopenharmony_ci| python | 3.7.5 or later | Language used by the test framework. | 34e41f4b71Sopenharmony_ci| Python plugins | pyserial 3.3 or later, paramiko 2.7.1 or later, setuptools 40.8.0 or later, and rsa4.0 or later| - **pserial**: supports Python serial port communication.<br>- **paramiko**: allows Python to use SSH. <br>- **setuptools**: allows Python packages to be created and distributed easily. <br>- **rsa**: implements RSA encryption in Python.| 35e41f4b71Sopenharmony_ci| NFS Server | haneWIN NFS Server 1.2.50 or later or NFS v4 or later | Devices can be connected using serial ports. Mini- and small-system devices are used. | 36e41f4b71Sopenharmony_ci| HDC | 1.2.0a | A tool that enables devices to be connected through the OpenHarmony Device Connector (HDC). | 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci1. Install Ubuntu. 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci As Ubuntu 20 has built-in Python 3.8.5, you do not need to install Python separately. 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci2. Run the following command to install the Linux extended component readline: 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci ```bash 47e41f4b71Sopenharmony_ci sudo apt-get install libreadline-dev 48e41f4b71Sopenharmony_ci ``` 49e41f4b71Sopenharmony_ci The installation is successful if the following information is displayed: 50e41f4b71Sopenharmony_ci ``` 51e41f4b71Sopenharmony_ci Reading package lists... Done 52e41f4b71Sopenharmony_ci Building dependency tree 53e41f4b71Sopenharmony_ci Reading state information... Done 54e41f4b71Sopenharmony_ci libreadline-dev is already the newest version (7.0-3). 55e41f4b71Sopenharmony_ci 0 upgraded, 0 newly installed, 0 to remove and 11 not upgraded. 56e41f4b71Sopenharmony_ci ``` 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci2. Run the following command to install the **setuptools** plugin: 59e41f4b71Sopenharmony_ci ```bash 60e41f4b71Sopenharmony_ci pip3 install setuptools 61e41f4b71Sopenharmony_ci ``` 62e41f4b71Sopenharmony_ci The installation is successful if the following information is displayed: 63e41f4b71Sopenharmony_ci ``` 64e41f4b71Sopenharmony_ci Requirement already satisfied: setuptools in d:\programs\python37\lib\site-packages (41.2.0) 65e41f4b71Sopenharmony_ci ``` 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci3. Run the following command to install the **paramiko** plugin: 68e41f4b71Sopenharmony_ci ```bash 69e41f4b71Sopenharmony_ci pip3 install paramiko 70e41f4b71Sopenharmony_ci ``` 71e41f4b71Sopenharmony_ci The installation is successful if the following information is displayed: 72e41f4b71Sopenharmony_ci ``` 73e41f4b71Sopenharmony_ci Installing collected packages: pycparser, cffi, pynacl, bcrypt, cryptography, paramiko 74e41f4b71Sopenharmony_ci 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 75e41f4b71Sopenharmony_ci ``` 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci4. Run the following command to install the **ras** plugin: 78e41f4b71Sopenharmony_ci ```bash 79e41f4b71Sopenharmony_ci pip3 install rsa 80e41f4b71Sopenharmony_ci ``` 81e41f4b71Sopenharmony_ci The installation is successful if the following information is displayed: 82e41f4b71Sopenharmony_ci ``` 83e41f4b71Sopenharmony_ci Installing collected packages: pyasn1, rsa 84e41f4b71Sopenharmony_ci Successfully installed pyasn1-0.4.8 rsa-4.7 85e41f4b71Sopenharmony_ci ``` 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci5. Run the following command to install the **pyserial** plugin: 88e41f4b71Sopenharmony_ci ```bash 89e41f4b71Sopenharmony_ci pip3 install pyserial 90e41f4b71Sopenharmony_ci ``` 91e41f4b71Sopenharmony_ci The installation is successful if the following information is displayed: 92e41f4b71Sopenharmony_ci ``` 93e41f4b71Sopenharmony_ci Requirement already satisfied: pyserial in d:\programs\python37\lib\site-packages\pyserial-3.4-py3.7.egg (3.4) 94e41f4b71Sopenharmony_ci ``` 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci6. Install the NFS server if the device outputs results only through the serial port. 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci > **NOTE** 99e41f4b71Sopenharmony_ci > 100e41f4b71Sopenharmony_ci > This operation applies to small-system or mini-system devices, not standard-system devices. 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci - Windows OS: Install the **haneWIN NFS Server1.2.50** package. 103e41f4b71Sopenharmony_ci - Linux OS: Run the following command to install the NFS server: 104e41f4b71Sopenharmony_ci ```bash 105e41f4b71Sopenharmony_ci sudo apt install nfs-kernel-server 106e41f4b71Sopenharmony_ci ``` 107e41f4b71Sopenharmony_ci The installation is successful if the following information is displayed: 108e41f4b71Sopenharmony_ci ``` 109e41f4b71Sopenharmony_ci Reading package lists... Done 110e41f4b71Sopenharmony_ci Building dependency tree 111e41f4b71Sopenharmony_ci Reading state information... Done 112e41f4b71Sopenharmony_ci nfs-kernel-server is already the newest version (1:1.3.4-2.1ubuntu5.3). 113e41f4b71Sopenharmony_ci 0 upgraded, 0 newly installed, 0 to remove and 11 not upgraded. 114e41f4b71Sopenharmony_ci ``` 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci7. If the device supports HDC connection, install the HDC tool. For details about the installation process, see [HDC-OpenHarmony Device Connector](https://gitee.com/openharmony/developtools_hdc/blob/master/README.md). 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci### Environment Dependency Check 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci| Check Item | Action | Requirement | 122e41f4b71Sopenharmony_ci| -------------------------------------------------- | --------------------------------------------------- | ------------------------- | 123e41f4b71Sopenharmony_ci| Check whether Python is installed successfully. | Run the **python --version** command. | The Python version is 3.7.5 or later. | 124e41f4b71Sopenharmony_ci| Check whether Python plugins are successfully installed. | Go to the **test/developertest** directory and run **start.bat** or **start.sh**.| The **>>>** prompt is displayed.| 125e41f4b71Sopenharmony_ci| Check the NFS server status (for the devices that support only serial port output).| Log in to the development board through the serial port and run the **mount** command to mount the NFS. | The file directory can be mounted. | 126e41f4b71Sopenharmony_ci| Check whether the HDC is successfully installed. | Run the **hdc -v** command. | The HDC version is 1.2.0a or later. | 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci## Test Case Preparation 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ciThe test framework supports multiple types of tests and provides different test case templates for them. 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci**TDD Test (C++)** 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci- Naming rules for source files 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci The source file name of test cases must be the same as that of the test suite. The file names must use lowercase letters and in the [Function]\_[Sub-function]\_**test** format. More specific sub-functions can be added as required. 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci- The following uses **calculator_sub_test.cpp** as an example to describe how to compile a single-thread test case: 141e41f4b71Sopenharmony_ci ``` 142e41f4b71Sopenharmony_ci /* 143e41f4b71Sopenharmony_ci * Copyright (c) 2023 XXXX Device Co., Ltd. 144e41f4b71Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 145e41f4b71Sopenharmony_ci * you may not use this file except in compliance with the License. 146e41f4b71Sopenharmony_ci * You may obtain a copy of the License at 147e41f4b71Sopenharmony_ci * 148e41f4b71Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 149e41f4b71Sopenharmony_ci * 150e41f4b71Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 151e41f4b71Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 152e41f4b71Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 153e41f4b71Sopenharmony_ci * See the License for the specific language governing permissions and 154e41f4b71Sopenharmony_ci * limitations under the License. 155e41f4b71Sopenharmony_ci */ 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci #include "calculator.h" 158e41f4b71Sopenharmony_ci #include <gtest/gtest.h> 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci using namespace testing::ext; 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci class CalculatorSubTest : public testing::Test { 163e41f4b71Sopenharmony_ci public: 164e41f4b71Sopenharmony_ci static void SetUpTestCase(void); 165e41f4b71Sopenharmony_ci static void TearDownTestCase(void); 166e41f4b71Sopenharmony_ci void SetUp(); 167e41f4b71Sopenharmony_ci void TearDown(); 168e41f4b71Sopenharmony_ci }; 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci void CalculatorSubTest::SetUpTestCase(void) 171e41f4b71Sopenharmony_ci { 172e41f4b71Sopenharmony_ci // Set a setup function, which will be called before all test cases. 173e41f4b71Sopenharmony_ci } 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci void CalculatorSubTest::TearDownTestCase(void) 176e41f4b71Sopenharmony_ci { 177e41f4b71Sopenharmony_ci // Set a teardown function, which will be called after all test cases. 178e41f4b71Sopenharmony_ci } 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci void CalculatorSubTest::SetUp(void) 181e41f4b71Sopenharmony_ci { 182e41f4b71Sopenharmony_ci // Set a setup function, which will be called before all test cases. 183e41f4b71Sopenharmony_ci } 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci void CalculatorSubTest::TearDown(void) 186e41f4b71Sopenharmony_ci { 187e41f4b71Sopenharmony_ci // Set a teardown function, which will be called after all test cases. 188e41f4b71Sopenharmony_ci } 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci /** 191e41f4b71Sopenharmony_ci * @tc.name: integer_sub_001 192e41f4b71Sopenharmony_ci * @tc.desc: Verify the sub function. 193e41f4b71Sopenharmony_ci * @tc.type: FUNC 194e41f4b71Sopenharmony_ci * @tc.require: issueNumber 195e41f4b71Sopenharmony_ci */ 196e41f4b71Sopenharmony_ci HWTEST_F(CalculatorSubTest, integer_sub_001, TestSize.Level1) 197e41f4b71Sopenharmony_ci { 198e41f4b71Sopenharmony_ci // Step 1 Call the function to obtain the test result. 199e41f4b71Sopenharmony_ci int actual = Sub(4, 0); 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci // Step 2 Use an assertion to compare the obtained result with the expected result. 202e41f4b71Sopenharmony_ci EXPECT_EQ(4, actual); 203e41f4b71Sopenharmony_ci } 204e41f4b71Sopenharmony_ci ``` 205e41f4b71Sopenharmony_ci The procedure is as follows: 206e41f4b71Sopenharmony_ci 1. Add comment information to the test case file header. 207e41f4b71Sopenharmony_ci ``` 208e41f4b71Sopenharmony_ci /* 209e41f4b71Sopenharmony_ci * Copyright (c) 2023 XXXX Device Co., Ltd. 210e41f4b71Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 211e41f4b71Sopenharmony_ci * you may not use this file except in compliance with the License. 212e41f4b71Sopenharmony_ci * You may obtain a copy of the License at 213e41f4b71Sopenharmony_ci * 214e41f4b71Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 215e41f4b71Sopenharmony_ci * 216e41f4b71Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 217e41f4b71Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 218e41f4b71Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 219e41f4b71Sopenharmony_ci * See the License for the specific language governing permissions and 220e41f4b71Sopenharmony_ci * limitations under the License. 221e41f4b71Sopenharmony_ci */ 222e41f4b71Sopenharmony_ci ``` 223e41f4b71Sopenharmony_ci 2. Add the test framework header file and namespace. 224e41f4b71Sopenharmony_ci ``` 225e41f4b71Sopenharmony_ci #include <gtest/gtest.h> 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci using namespace testing::ext; 228e41f4b71Sopenharmony_ci ``` 229e41f4b71Sopenharmony_ci 3. Add the header file of the test class. 230e41f4b71Sopenharmony_ci ``` 231e41f4b71Sopenharmony_ci #include "calculator.h" 232e41f4b71Sopenharmony_ci ``` 233e41f4b71Sopenharmony_ci 4. Define the test suite (test class). 234e41f4b71Sopenharmony_ci > When defining a test suite, ensure that the test suite name is the same as the target to build and uses the upper camel case style. 235e41f4b71Sopenharmony_ci ``` 236e41f4b71Sopenharmony_ci class CalculatorSubTest : public testing::Test { 237e41f4b71Sopenharmony_ci public: 238e41f4b71Sopenharmony_ci static void SetUpTestCase(void); 239e41f4b71Sopenharmony_ci static void TearDownTestCase(void); 240e41f4b71Sopenharmony_ci void SetUp(); 241e41f4b71Sopenharmony_ci void TearDown(); 242e41f4b71Sopenharmony_ci }; 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci void CalculatorSubTest::SetUpTestCase(void) 245e41f4b71Sopenharmony_ci { 246e41f4b71Sopenharmony_ci // Set a setup function, which will be called before all test cases. 247e41f4b71Sopenharmony_ci } 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ci void CalculatorSubTest::TearDownTestCase(void) 250e41f4b71Sopenharmony_ci { 251e41f4b71Sopenharmony_ci // Set a teardown function, which will be called after all test cases. 252e41f4b71Sopenharmony_ci } 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci void CalculatorSubTest::SetUp(void) 255e41f4b71Sopenharmony_ci { 256e41f4b71Sopenharmony_ci // Set a setup function, which will be called before all test cases. 257e41f4b71Sopenharmony_ci } 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci void CalculatorSubTest::TearDown(void) 260e41f4b71Sopenharmony_ci { 261e41f4b71Sopenharmony_ci // Set a teardown function, which will be called after all test cases. 262e41f4b71Sopenharmony_ci } 263e41f4b71Sopenharmony_ci ``` 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ci 5. Add implementation of the test cases, including test case comments and logic. 266e41f4b71Sopenharmony_ci ``` 267e41f4b71Sopenharmony_ci /** 268e41f4b71Sopenharmony_ci * @tc.name: integer_sub_001 269e41f4b71Sopenharmony_ci * @tc.desc: Verify the sub function. 270e41f4b71Sopenharmony_ci * @tc.type: FUNC 271e41f4b71Sopenharmony_ci * @tc.require: issueNumber 272e41f4b71Sopenharmony_ci */ 273e41f4b71Sopenharmony_ci HWTEST_F(CalculatorSubTest, integer_sub_001, TestSize.Level1) 274e41f4b71Sopenharmony_ci { 275e41f4b71Sopenharmony_ci // Step 1 Call the function to obtain the test result. 276e41f4b71Sopenharmony_ci int actual = Sub(4, 0); 277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_ci // Step 2 Use an assertion to compare the obtained result with the expected result. 279e41f4b71Sopenharmony_ci EXPECT_EQ(4, actual); 280e41f4b71Sopenharmony_ci } 281e41f4b71Sopenharmony_ci ``` 282e41f4b71Sopenharmony_ci > **NOTE** 283e41f4b71Sopenharmony_ci > 284e41f4b71Sopenharmony_ci > The value of **@tc.require** must start with AR/SR or issue, for example, **issueI56WJ7**. 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci- The following uses **base_object_test.cpp** as an example to describe how to compile a multi-thread test case: 287e41f4b71Sopenharmony_ci ``` 288e41f4b71Sopenharmony_ci // The test case file header comment and test case comment are the same as those in the single-thread test case example. 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci #include "base_object.h" 291e41f4b71Sopenharmony_ci #include <gtest/gtest.h> 292e41f4b71Sopenharmony_ci #include <gtest/hwext/gtest-multithread.h> 293e41f4b71Sopenharmony_ci #include <unistd.h> 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ci using namespace testing::ext; 296e41f4b71Sopenharmony_ci using namespace testing::mt; 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci namespace OHOS { 299e41f4b71Sopenharmony_ci namespace AAFwk { 300e41f4b71Sopenharmony_ci class AAFwkBaseObjectTest : public testing::Test {......} 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci // Step 1 Set the function to be tested to return the factorial result. 303e41f4b71Sopenharmony_ci int factorial(int n) 304e41f4b71Sopenharmony_ci { 305e41f4b71Sopenharmony_ci int result = 1; 306e41f4b71Sopenharmony_ci for (int i = 1; i <= n; i++) { 307e41f4b71Sopenharmony_ci result *= i; 308e41f4b71Sopenharmony_ci } 309e41f4b71Sopenharmony_ci printf("Factorial Function Result : %d! = %d\n", n, result); 310e41f4b71Sopenharmony_ci return result; 311e41f4b71Sopenharmony_ci } 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ci // Step 2 Use an assertion to compare the obtained result with the expected result. 314e41f4b71Sopenharmony_ci void factorial_test() 315e41f4b71Sopenharmony_ci { 316e41f4b71Sopenharmony_ci int ret = factorial(3); // Call the function to obtain the result. 317e41f4b71Sopenharmony_ci std::thread::id this_id = std::this_thread::get_id(); 318e41f4b71Sopenharmony_ci std::ostringstream oss; 319e41f4b71Sopenharmony_ci oss << this_id; 320e41f4b71Sopenharmony_ci std::string this_id_str = oss.str(); 321e41f4b71Sopenharmony_ci long int thread_id = atol(this_id_str.c_str()); 322e41f4b71Sopenharmony_ci printf("running thread...: %ld\n", thread_id); // Output the ID of the running thread. 323e41f4b71Sopenharmony_ci EXPECT_EQ(ret, 6); 324e41f4b71Sopenharmony_ci } 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci HWTEST_F(AAFwkBaseObjectTest, Factorial_test_001, TestSize.Level1) 327e41f4b71Sopenharmony_ci { 328e41f4b71Sopenharmony_ci SET_THREAD_NUM(4); 329e41f4b71Sopenharmony_ci printf("Factorial_test_001 BEGIN\n"); 330e41f4b71Sopenharmony_ci GTEST_RUN_TASK(factorial_test); 331e41f4b71Sopenharmony_ci printf("Factorial_test_001 END\n"); 332e41f4b71Sopenharmony_ci } 333e41f4b71Sopenharmony_ci 334e41f4b71Sopenharmony_ci HWMTEST_F(AAFwkBaseObjectTest, Factorial_test_002, TestSize.Level1, 6) 335e41f4b71Sopenharmony_ci { 336e41f4b71Sopenharmony_ci printf("Factorial_test_002 BEGIN\n"); 337e41f4b71Sopenharmony_ci factorial_test(); 338e41f4b71Sopenharmony_ci printf("Factorial_test_002 END\n"); 339e41f4b71Sopenharmony_ci } 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ci } // namespace AAFwk 342e41f4b71Sopenharmony_ci } // namespace OHOS 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci ``` 345e41f4b71Sopenharmony_ci The procedure is as follows: 346e41f4b71Sopenharmony_ci 1. Add comment information to the test case file header. 347e41f4b71Sopenharmony_ci 348e41f4b71Sopenharmony_ci > **NOTE** 349e41f4b71Sopenharmony_ci > 350e41f4b71Sopenharmony_ci > The standard is the same as that of the single-thread test case. 351e41f4b71Sopenharmony_ci 352e41f4b71Sopenharmony_ci ``` 353e41f4b71Sopenharmony_ci #include <gtest/gtest.h> 354e41f4b71Sopenharmony_ci #include <gtest/hwext/gtest-multithread.h> 355e41f4b71Sopenharmony_ci #include <unistd.h> 356e41f4b71Sopenharmony_ci using namespace testing::ext; 357e41f4b71Sopenharmony_ci using namespace testing::mt; 358e41f4b71Sopenharmony_ci ``` 359e41f4b71Sopenharmony_ci 3. Add the header file of the test class. 360e41f4b71Sopenharmony_ci ``` 361e41f4b71Sopenharmony_ci #include "base_object.h" 362e41f4b71Sopenharmony_ci ``` 363e41f4b71Sopenharmony_ci 4. Define the test suite (test class). 364e41f4b71Sopenharmony_ci ``` 365e41f4b71Sopenharmony_ci class AAFwkBaseObjectTest : public testing::Test {......} 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ci ``` 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci 370e41f4b71Sopenharmony_ci > **NOTE** 371e41f4b71Sopenharmony_ci > 372e41f4b71Sopenharmony_ci > The standard is the same as that of the single-thread test case. 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ci ``` 375e41f4b71Sopenharmony_ci // Step 1 Set the function to be tested to return the factorial result. 376e41f4b71Sopenharmony_ci int factorial(int n) 377e41f4b71Sopenharmony_ci { 378e41f4b71Sopenharmony_ci int result = 1; 379e41f4b71Sopenharmony_ci for (int i = 1; i <= n; i++) { 380e41f4b71Sopenharmony_ci result *= i; 381e41f4b71Sopenharmony_ci } 382e41f4b71Sopenharmony_ci printf("Factorial Function Result : %d! = %d\n", n, result); 383e41f4b71Sopenharmony_ci return result; 384e41f4b71Sopenharmony_ci } 385e41f4b71Sopenharmony_ci 386e41f4b71Sopenharmony_ci // Step 2 Use an assertion to compare the obtained result with the expected result. 387e41f4b71Sopenharmony_ci void factorial_test() 388e41f4b71Sopenharmony_ci { 389e41f4b71Sopenharmony_ci int ret = factorial(3); // Call the function to obtain the result. 390e41f4b71Sopenharmony_ci std::thread::id this_id = std::this_thread::get_id(); 391e41f4b71Sopenharmony_ci std::ostringstream oss; 392e41f4b71Sopenharmony_ci oss << this_id; 393e41f4b71Sopenharmony_ci std::string this_id_str = oss.str(); 394e41f4b71Sopenharmony_ci long int thread_id = atol(this_id_str.c_str()); 395e41f4b71Sopenharmony_ci printf("running thread...: %ld\n", thread_id); // Output the ID of the running thread. 396e41f4b71Sopenharmony_ci EXPECT_EQ(ret, 6); 397e41f4b71Sopenharmony_ci } 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci // GTEST_RUN_TASK(TestFunction) is a multi-thread startup function. The parameter is a custom function. 400e41f4b71Sopenharmony_ci // If SET_THREAD_NUM() is not called, the default value 10 will be used. 401e41f4b71Sopenharmony_ci HWTEST_F(AAFwkBaseObjectTest, Factorial_test_001, TestSize.Level1) 402e41f4b71Sopenharmony_ci { 403e41f4b71Sopenharmony_ci SET_THREAD_NUM(4); // Set the number of threads. It can be dynamically set in the same test suite. 404e41f4b71Sopenharmony_ci printf("Factorial_test_001 BEGIN\n"); 405e41f4b71Sopenharmony_ci GTEST_RUN_TASK(factorial_test); // Start the multi-thread execution of the factorial_test task. 406e41f4b71Sopenharmony_ci printf("Factorial_test_001 END\n"); 407e41f4b71Sopenharmony_ci } 408e41f4b71Sopenharmony_ci 409e41f4b71Sopenharmony_ci // HWMTEST_F(TEST_SUITE, TEST_TC, TEST_LEVEL, THREAD_NUM) 410e41f4b71Sopenharmony_ci // THREAD_NUM can be used to set the number of threads for executing a test case. 411e41f4b71Sopenharmony_ci // HWMTEST_F creates a specified number of threads and executes the tested function. 412e41f4b71Sopenharmony_ci HWMTEST_F(AAFwkBaseObjectTest, Factorial_test_002, TestSize.Level1, 6) 413e41f4b71Sopenharmony_ci { 414e41f4b71Sopenharmony_ci printf("Factorial_test_002 BEGIN\n"); 415e41f4b71Sopenharmony_ci factorial_test(); 416e41f4b71Sopenharmony_ci printf("Factorial_test_002 END\n"); 417e41f4b71Sopenharmony_ci } 418e41f4b71Sopenharmony_ci // Add the multi-thread API MTEST_ADD_TASK(THREAD_ID,ThreadTestFunc). Multiple threads are registered but are not executed in this test case. Instead, they are executed later in a unified manner. This API is applicable to the multi-thread test in the scenario where multiple test cases are combined. 419e41f4b71Sopenharmony_ci // THREAD_ID is used to distinguish threads and starts from 0. You can also use a random thread ID by passing in RANDOM_THREAD_ID. In this scenario, each thread ID is unique. 420e41f4b71Sopenharmony_ci // Add the multi-thread API MTEST_POST_RUN() to execute the previously registered threads in a unified manner. 421e41f4b71Sopenharmony_ci ``` 422e41f4b71Sopenharmony_ci > **NOTE** 423e41f4b71Sopenharmony_ci > 424e41f4b71Sopenharmony_ci > The comments for multi-thread test cases are the same as those of single-thread test cases. 425e41f4b71Sopenharmony_ci 426e41f4b71Sopenharmony_ci- About C++ test case templates: 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ci The following test case templates are provided for your reference. 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci | Type| Description| 431e41f4b71Sopenharmony_ci | ------------| ------------| 432e41f4b71Sopenharmony_ci | HWTEST(A,B,C)| Use this template if the test case execution does not depend on setup or teardown.| 433e41f4b71Sopenharmony_ci | HWTEST_F(A,B,C)| Use this template if the test case execution (excluding parameters) depends on setup and teardown.| 434e41f4b71Sopenharmony_ci | HWMTEST_F(A,B,C,D)| Use this template if the multi-thread test case execution depends on setup and teardown.| 435e41f4b71Sopenharmony_ci | HWTEST_P(A,B,C)| Use this template if the test case execution (including parameters) depends on setup and teardown.| 436e41f4b71Sopenharmony_ci 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci In the template names: 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ci - **A** indicates the test suite name. 441e41f4b71Sopenharmony_ci 442e41f4b71Sopenharmony_ci - **B** indicates the test case name, which is in the *Function*_*No.* format. The *No.* is a three-digit number starting from **001**. 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci - **C** indicates the test case level. There are five test case levels: guard-control level 0 and non-guard-control level 1 to level 4. Of levels 1 to 4, a smaller value indicates a more important function verified by the test case. 445e41f4b71Sopenharmony_ci 446e41f4b71Sopenharmony_ci - **D** indicates the number of threads for executing the multi-thread test case. 447e41f4b71Sopenharmony_ci 448e41f4b71Sopenharmony_ci **Note**: 449e41f4b71Sopenharmony_ci 450e41f4b71Sopenharmony_ci - The expected result of each test case must have an assertion. 451e41f4b71Sopenharmony_ci 452e41f4b71Sopenharmony_ci - The test case level must be specified. 453e41f4b71Sopenharmony_ci 454e41f4b71Sopenharmony_ci - It is recommended that the test be implemented step by step according to the template. 455e41f4b71Sopenharmony_ci 456e41f4b71Sopenharmony_ci - The test case description is in the standard @tc.*xxx* *value* format. The comment must contain the test case name, description, type, and requirement number. The test case type @tc.type can be any of the following: 457e41f4b71Sopenharmony_ci 458e41f4b71Sopenharmony_ci 459e41f4b71Sopenharmony_ci | Test Case Type | Code | 460e41f4b71Sopenharmony_ci | ------------ | -------- | 461e41f4b71Sopenharmony_ci | Function test | FUNC | 462e41f4b71Sopenharmony_ci | Performance Test | PERF | 463e41f4b71Sopenharmony_ci | Reliability test | RELI | 464e41f4b71Sopenharmony_ci | Security Test | SECU | 465e41f4b71Sopenharmony_ci | Fuzz test | FUZZ | 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci**TDD Test (JavaScript)** 468e41f4b71Sopenharmony_ci 469e41f4b71Sopenharmony_ci- Naming rules for source files 470e41f4b71Sopenharmony_ci 471e41f4b71Sopenharmony_ci 472e41f4b71Sopenharmony_ci The source file name of a test case must be in the [Function][Sub-function]**Test** format, and each part must use the upper camel case style. More specific sub-functions can be added as required. 473e41f4b71Sopenharmony_ci Example: 474e41f4b71Sopenharmony_ci ``` 475e41f4b71Sopenharmony_ci AppInfoTest.js 476e41f4b71Sopenharmony_ci ``` 477e41f4b71Sopenharmony_ci 478e41f4b71Sopenharmony_ci- Test case example 479e41f4b71Sopenharmony_ci 480e41f4b71Sopenharmony_ci ```js 481e41f4b71Sopenharmony_ci /* 482e41f4b71Sopenharmony_ci * Copyright (C) 2023 XXXX Device Co., Ltd. 483e41f4b71Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 484e41f4b71Sopenharmony_ci * you may not use this file except in compliance with the License. 485e41f4b71Sopenharmony_ci * You may obtain a copy of the License at 486e41f4b71Sopenharmony_ci * 487e41f4b71Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 488e41f4b71Sopenharmony_ci * 489e41f4b71Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 490e41f4b71Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 491e41f4b71Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 492e41f4b71Sopenharmony_ci * See the License for the specific language governing permissions and 493e41f4b71Sopenharmony_ci * limitations under the License. 494e41f4b71Sopenharmony_ci */ 495e41f4b71Sopenharmony_ci import app from '@system.app' 496e41f4b71Sopenharmony_ci 497e41f4b71Sopenharmony_ci import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' 498e41f4b71Sopenharmony_ci 499e41f4b71Sopenharmony_ci describe("AppInfoTest", function () { 500e41f4b71Sopenharmony_ci beforeAll(function() { 501e41f4b71Sopenharmony_ci // Set a setup function, which will be called before all test cases. 502e41f4b71Sopenharmony_ci console.info('beforeAll caled') 503e41f4b71Sopenharmony_ci }) 504e41f4b71Sopenharmony_ci 505e41f4b71Sopenharmony_ci afterAll(function() { 506e41f4b71Sopenharmony_ci // Set a teardown function, which will be called after all test cases. 507e41f4b71Sopenharmony_ci console.info('afterAll caled') 508e41f4b71Sopenharmony_ci }) 509e41f4b71Sopenharmony_ci 510e41f4b71Sopenharmony_ci beforeEach(function() { 511e41f4b71Sopenharmony_ci // Set a setup function, which will be called before all test cases. 512e41f4b71Sopenharmony_ci console.info('beforeEach caled') 513e41f4b71Sopenharmony_ci }) 514e41f4b71Sopenharmony_ci 515e41f4b71Sopenharmony_ci afterEach(function() { 516e41f4b71Sopenharmony_ci // Set a teardown function, which will be called after all test cases. 517e41f4b71Sopenharmony_ci console.info('afterEach caled') 518e41f4b71Sopenharmony_ci }) 519e41f4b71Sopenharmony_ci 520e41f4b71Sopenharmony_ci /* 521e41f4b71Sopenharmony_ci * @tc.name:appInfoTest001 522e41f4b71Sopenharmony_ci * @tc.desc:verify app info is not null 523e41f4b71Sopenharmony_ci * @tc.type: FUNC 524e41f4b71Sopenharmony_ci * @tc.require: issueNumber 525e41f4b71Sopenharmony_ci */ 526e41f4b71Sopenharmony_ci it("appInfoTest001", 0, function () { 527e41f4b71Sopenharmony_ci // Step 1 Call the function to obtain the test result. 528e41f4b71Sopenharmony_ci var info = app.getInfo() 529e41f4b71Sopenharmony_ci 530e41f4b71Sopenharmony_ci // Step 2 Use an assertion to compare the obtained result with the expected result. 531e41f4b71Sopenharmony_ci expect(info != null).assertEqual(true) 532e41f4b71Sopenharmony_ci }) 533e41f4b71Sopenharmony_ci }) 534e41f4b71Sopenharmony_ci ``` 535e41f4b71Sopenharmony_ci The procedure is as follows: 536e41f4b71Sopenharmony_ci 1. Add comment information to the test case file header. 537e41f4b71Sopenharmony_ci ``` 538e41f4b71Sopenharmony_ci /* 539e41f4b71Sopenharmony_ci * Copyright (C) 2023 XXXX Device Co., Ltd. 540e41f4b71Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 541e41f4b71Sopenharmony_ci * you may not use this file except in compliance with the License. 542e41f4b71Sopenharmony_ci * You may obtain a copy of the License at 543e41f4b71Sopenharmony_ci * 544e41f4b71Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 545e41f4b71Sopenharmony_ci * 546e41f4b71Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 547e41f4b71Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 548e41f4b71Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 549e41f4b71Sopenharmony_ci * See the License for the specific language governing permissions and 550e41f4b71Sopenharmony_ci * limitations under the License. 551e41f4b71Sopenharmony_ci */ 552e41f4b71Sopenharmony_ci ``` 553e41f4b71Sopenharmony_ci 2. Import the APIs and JSUnit test library to test. 554e41f4b71Sopenharmony_ci ```js 555e41f4b71Sopenharmony_ci import app from '@system.app' 556e41f4b71Sopenharmony_ci 557e41f4b71Sopenharmony_ci import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' 558e41f4b71Sopenharmony_ci ``` 559e41f4b71Sopenharmony_ci 3. Define the test suite (test class). 560e41f4b71Sopenharmony_ci ```js 561e41f4b71Sopenharmony_ci describe("AppInfoTest", function () { 562e41f4b71Sopenharmony_ci beforeAll(function() { 563e41f4b71Sopenharmony_ci // Set a setup function, which will be called before all test cases. 564e41f4b71Sopenharmony_ci console.info('beforeAll caled') 565e41f4b71Sopenharmony_ci }) 566e41f4b71Sopenharmony_ci 567e41f4b71Sopenharmony_ci afterAll(function() { 568e41f4b71Sopenharmony_ci // Set a teardown function, which will be called after all test cases. 569e41f4b71Sopenharmony_ci console.info('afterAll caled') 570e41f4b71Sopenharmony_ci }) 571e41f4b71Sopenharmony_ci 572e41f4b71Sopenharmony_ci beforeEach(function() { 573e41f4b71Sopenharmony_ci // Set a setup function, which will be called before all test cases. 574e41f4b71Sopenharmony_ci console.info('beforeEach caled') 575e41f4b71Sopenharmony_ci }) 576e41f4b71Sopenharmony_ci 577e41f4b71Sopenharmony_ci afterEach(function() { 578e41f4b71Sopenharmony_ci // Set a teardown function, which will be called after all test cases. 579e41f4b71Sopenharmony_ci console.info('afterEach caled') 580e41f4b71Sopenharmony_ci }) 581e41f4b71Sopenharmony_ci ``` 582e41f4b71Sopenharmony_ci 4. Write test cases 583e41f4b71Sopenharmony_ci ```JS 584e41f4b71Sopenharmony_ci /* 585e41f4b71Sopenharmony_ci * @tc.name:appInfoTest001 586e41f4b71Sopenharmony_ci * @tc.desc:verify app info is not null 587e41f4b71Sopenharmony_ci * @tc.type: FUNC 588e41f4b71Sopenharmony_ci * @tc.require: issueNumber 589e41f4b71Sopenharmony_ci */ 590e41f4b71Sopenharmony_ci it("appInfoTest001", 0, function () { 591e41f4b71Sopenharmony_ci // Step 1 Call the function to obtain the test result. 592e41f4b71Sopenharmony_ci var info = app.getInfo() 593e41f4b71Sopenharmony_ci 594e41f4b71Sopenharmony_ci // Step 2 Use an assertion to compare the obtained result with the expected result. 595e41f4b71Sopenharmony_ci expect(info != null).assertEqual(true) 596e41f4b71Sopenharmony_ci }) 597e41f4b71Sopenharmony_ci ``` 598e41f4b71Sopenharmony_ci > **NOTE** 599e41f4b71Sopenharmony_ci > 600e41f4b71Sopenharmony_ci > The value of **@tc.require** must start with AR/SR or issue, for example, **issueI56WJ7**. 601e41f4b71Sopenharmony_ci 602e41f4b71Sopenharmony_ci**Fuzzing Test** 603e41f4b71Sopenharmony_ci 604e41f4b71Sopenharmony_ci[Fuzzing case specifications](https://gitee.com/openharmony/testfwk_developer_test/blob/master/libs/fuzzlib/README_zh.md) 605e41f4b71Sopenharmony_ci 606e41f4b71Sopenharmony_ci 607e41f4b71Sopenharmony_ci**Benchmark Test** 608e41f4b71Sopenharmony_ci 609e41f4b71Sopenharmony_ci[Benchmark case specifications](https://gitee.com/openharmony/testfwk_developer_test/blob/master/libs/benchmark/README_zh.md) 610e41f4b71Sopenharmony_ci 611e41f4b71Sopenharmony_ci## **Test Case Building** 612e41f4b71Sopenharmony_ci 613e41f4b71Sopenharmony_ciWhen a test case is executed, the test framework searches for the build file of the test case in the test case directory and builds the test case located. The following describes how to write build files (GN files) in different programming languages. 614e41f4b71Sopenharmony_ci 615e41f4b71Sopenharmony_ci**TDD Test** 616e41f4b71Sopenharmony_ci 617e41f4b71Sopenharmony_ciThe following provides templates for different languages for your reference. 618e41f4b71Sopenharmony_ci 619e41f4b71Sopenharmony_ci- **Test case build file example (C++)** 620e41f4b71Sopenharmony_ci 621e41f4b71Sopenharmony_ci ``` 622e41f4b71Sopenharmony_ci # Copyright (c) 2023 XXXX Device Co., Ltd. 623e41f4b71Sopenharmony_ci 624e41f4b71Sopenharmony_ci import("//build/test.gni") 625e41f4b71Sopenharmony_ci 626e41f4b71Sopenharmony_ci module_output_path = "developertest/calculator" 627e41f4b71Sopenharmony_ci 628e41f4b71Sopenharmony_ci config("module_private_config") { 629e41f4b71Sopenharmony_ci visibility = [ ":*" ] 630e41f4b71Sopenharmony_ci 631e41f4b71Sopenharmony_ci include_dirs = [ "../../../include" ] 632e41f4b71Sopenharmony_ci } 633e41f4b71Sopenharmony_ci 634e41f4b71Sopenharmony_ci ohos_unittest("CalculatorSubTest") { 635e41f4b71Sopenharmony_ci module_out_path = module_output_path 636e41f4b71Sopenharmony_ci 637e41f4b71Sopenharmony_ci sources = [ 638e41f4b71Sopenharmony_ci "../../../include/calculator.h", 639e41f4b71Sopenharmony_ci "../../../src/calculator.cpp", 640e41f4b71Sopenharmony_ci ] 641e41f4b71Sopenharmony_ci 642e41f4b71Sopenharmony_ci sources += [ "calculator_sub_test.cpp" ] 643e41f4b71Sopenharmony_ci 644e41f4b71Sopenharmony_ci configs = [ ":module_private_config" ] 645e41f4b71Sopenharmony_ci 646e41f4b71Sopenharmony_ci deps = [ "//third_party/googletest:gtest_main" ] 647e41f4b71Sopenharmony_ci } 648e41f4b71Sopenharmony_ci 649e41f4b71Sopenharmony_ci group("unittest") { 650e41f4b71Sopenharmony_ci testonly = true 651e41f4b71Sopenharmony_ci deps = [":CalculatorSubTest"] 652e41f4b71Sopenharmony_ci } 653e41f4b71Sopenharmony_ci ``` 654e41f4b71Sopenharmony_ci The procedure is as follows: 655e41f4b71Sopenharmony_ci 656e41f4b71Sopenharmony_ci 1. Add comment information for the file header. 657e41f4b71Sopenharmony_ci ``` 658e41f4b71Sopenharmony_ci # Copyright (c) 2023 XXXX Device Co., Ltd. 659e41f4b71Sopenharmony_ci ``` 660e41f4b71Sopenharmony_ci 2. Import the build template. 661e41f4b71Sopenharmony_ci ``` 662e41f4b71Sopenharmony_ci import("//build/test.gni") 663e41f4b71Sopenharmony_ci ``` 664e41f4b71Sopenharmony_ci 3. Specify the file output path. 665e41f4b71Sopenharmony_ci ``` 666e41f4b71Sopenharmony_ci module_output_path = "developertest/calculator" 667e41f4b71Sopenharmony_ci ``` 668e41f4b71Sopenharmony_ci > **NOTE** 669e41f4b71Sopenharmony_ci > 670e41f4b71Sopenharmony_ci > The output path is the *Part name*/*Module name*. 671e41f4b71Sopenharmony_ci 672e41f4b71Sopenharmony_ci 4. Configure the directories for dependencies. 673e41f4b71Sopenharmony_ci 674e41f4b71Sopenharmony_ci ``` 675e41f4b71Sopenharmony_ci config("module_private_config") { 676e41f4b71Sopenharmony_ci visibility = [ ":*" ] 677e41f4b71Sopenharmony_ci 678e41f4b71Sopenharmony_ci include_dirs = [ "../../../include" ] 679e41f4b71Sopenharmony_ci } 680e41f4b71Sopenharmony_ci ``` 681e41f4b71Sopenharmony_ci > **NOTE** 682e41f4b71Sopenharmony_ci > 683e41f4b71Sopenharmony_ci > Generally, the dependency directories are configured here and directly referenced in the build script of the test case. 684e41f4b71Sopenharmony_ci 685e41f4b71Sopenharmony_ci 5. Set the output build file for the test cases. 686e41f4b71Sopenharmony_ci 687e41f4b71Sopenharmony_ci ``` 688e41f4b71Sopenharmony_ci ohos_unittest("CalculatorSubTest") { 689e41f4b71Sopenharmony_ci } 690e41f4b71Sopenharmony_ci ``` 691e41f4b71Sopenharmony_ci 6. Write the build script (add the source file, configuration, and dependencies) for the test cases. 692e41f4b71Sopenharmony_ci ``` 693e41f4b71Sopenharmony_ci ohos_unittest("CalculatorSubTest") { 694e41f4b71Sopenharmony_ci module_out_path = module_output_path 695e41f4b71Sopenharmony_ci sources = [ 696e41f4b71Sopenharmony_ci "../../../include/calculator.h", 697e41f4b71Sopenharmony_ci "../../../src/calculator.cpp", 698e41f4b71Sopenharmony_ci "../../../test/calculator_sub_test.cpp" 699e41f4b71Sopenharmony_ci ] 700e41f4b71Sopenharmony_ci sources += [ "calculator_sub_test.cpp" ] 701e41f4b71Sopenharmony_ci configs = [ ":module_private_config" ] 702e41f4b71Sopenharmony_ci deps = [ "//third_party/googletest:gtest_main" ] 703e41f4b71Sopenharmony_ci } 704e41f4b71Sopenharmony_ci ``` 705e41f4b71Sopenharmony_ci 706e41f4b71Sopenharmony_ci > **NOTE** 707e41f4b71Sopenharmony_ci > 708e41f4b71Sopenharmony_ci > Set the test type based on actual requirements. The following test types are available: 709e41f4b71Sopenharmony_ci > - **ohos_unittest**: unit test 710e41f4b71Sopenharmony_ci > - **ohos_js_unittest**: FA model JS unit test 711e41f4b71Sopenharmony_ci > - **ohos_js_stage_unittest**: stage model ArkTS unit test 712e41f4b71Sopenharmony_ci > - **ohos_moduletest**: module test 713e41f4b71Sopenharmony_ci > - **ohos_systemtest**: system test 714e41f4b71Sopenharmony_ci > - **ohos_performancetest**: performance test 715e41f4b71Sopenharmony_ci > - **ohos_securitytest**: security test 716e41f4b71Sopenharmony_ci > - **ohos_reliabilitytest**: reliability test 717e41f4b71Sopenharmony_ci > - **ohos_distributedtest**: distributed test 718e41f4b71Sopenharmony_ci 719e41f4b71Sopenharmony_ci 7. Group the test case files by test type. 720e41f4b71Sopenharmony_ci 721e41f4b71Sopenharmony_ci ``` 722e41f4b71Sopenharmony_ci group("unittest") { 723e41f4b71Sopenharmony_ci testonly = true 724e41f4b71Sopenharmony_ci deps = [":CalculatorSubTest"] 725e41f4b71Sopenharmony_ci } 726e41f4b71Sopenharmony_ci ``` 727e41f4b71Sopenharmony_ci > **NOTE** 728e41f4b71Sopenharmony_ci > 729e41f4b71Sopenharmony_ci > Grouping test cases by test type allows you to execute a specific type of test cases when required. 730e41f4b71Sopenharmony_ci 731e41f4b71Sopenharmony_ci- **Test case build file example (JavaScript)** 732e41f4b71Sopenharmony_ci 733e41f4b71Sopenharmony_ci ``` 734e41f4b71Sopenharmony_ci # Copyright (C) 2023 XXXX Device Co., Ltd. 735e41f4b71Sopenharmony_ci 736e41f4b71Sopenharmony_ci import("//build/test.gni") 737e41f4b71Sopenharmony_ci 738e41f4b71Sopenharmony_ci module_output_path = "developertest/app_info" 739e41f4b71Sopenharmony_ci 740e41f4b71Sopenharmony_ci ohos_js_unittest("GetAppInfoJsTest") { 741e41f4b71Sopenharmony_ci module_out_path = module_output_path 742e41f4b71Sopenharmony_ci 743e41f4b71Sopenharmony_ci hap_profile = "./config.json" 744e41f4b71Sopenharmony_ci certificate_profile = "//test/developertest/signature/openharmony_sx.p7b" 745e41f4b71Sopenharmony_ci } 746e41f4b71Sopenharmony_ci 747e41f4b71Sopenharmony_ci group("unittest") { 748e41f4b71Sopenharmony_ci testonly = true 749e41f4b71Sopenharmony_ci deps = [ ":GetAppInfoJsTest" ] 750e41f4b71Sopenharmony_ci } 751e41f4b71Sopenharmony_ci ``` 752e41f4b71Sopenharmony_ci 753e41f4b71Sopenharmony_ci The procedure is as follows: 754e41f4b71Sopenharmony_ci 755e41f4b71Sopenharmony_ci 1. Add comment information for the file header. 756e41f4b71Sopenharmony_ci 757e41f4b71Sopenharmony_ci ``` 758e41f4b71Sopenharmony_ci # Copyright (C) 2023 XXXX Device Co., Ltd. 759e41f4b71Sopenharmony_ci ``` 760e41f4b71Sopenharmony_ci 761e41f4b71Sopenharmony_ci 2. Import the build template. 762e41f4b71Sopenharmony_ci 763e41f4b71Sopenharmony_ci ``` 764e41f4b71Sopenharmony_ci import("//build/test.gni") 765e41f4b71Sopenharmony_ci ``` 766e41f4b71Sopenharmony_ci 767e41f4b71Sopenharmony_ci 3. Specify the file output path. 768e41f4b71Sopenharmony_ci 769e41f4b71Sopenharmony_ci ``` 770e41f4b71Sopenharmony_ci module_output_path = "developertest/app_info" 771e41f4b71Sopenharmony_ci ``` 772e41f4b71Sopenharmony_ci > **NOTE** 773e41f4b71Sopenharmony_ci > 774e41f4b71Sopenharmony_ci > The output path is the *Part name*/*Module name*. 775e41f4b71Sopenharmony_ci 776e41f4b71Sopenharmony_ci 4. Set the output build file for the test cases. 777e41f4b71Sopenharmony_ci 778e41f4b71Sopenharmony_ci ``` 779e41f4b71Sopenharmony_ci ohos_js_unittest("GetAppInfoJsTest") { 780e41f4b71Sopenharmony_ci } 781e41f4b71Sopenharmony_ci ``` 782e41f4b71Sopenharmony_ci > **NOTE** 783e41f4b71Sopenharmony_ci > 784e41f4b71Sopenharmony_ci > - Use the **ohos_js_unittest** template to define the JavaScript test suite. Pay attention to the difference between JavaScript and C++. 785e41f4b71Sopenharmony_ci > - The file generated for the JavaScript test suite must be in .hap format and named after the test suite name defined here. The test suite name must end with **JsTest**. 786e41f4b71Sopenharmony_ci 787e41f4b71Sopenharmony_ci 5. Configure the **config.json** file and signature file, which are mandatory. 788e41f4b71Sopenharmony_ci 789e41f4b71Sopenharmony_ci ``` 790e41f4b71Sopenharmony_ci ohos_js_unittest("GetAppInfoJsTest") { 791e41f4b71Sopenharmony_ci module_out_path = module_output_path 792e41f4b71Sopenharmony_ci 793e41f4b71Sopenharmony_ci hap_profile = "./config.json" 794e41f4b71Sopenharmony_ci certificate_profile = "//test/developertest/signature/openharmony_sx.p7b" 795e41f4b71Sopenharmony_ci } 796e41f4b71Sopenharmony_ci ``` 797e41f4b71Sopenharmony_ci **config.json** is the configuration file required for HAP build. You need to set **target** based on the tested SDK version. Default values can be retained for other items. The following is an example: 798e41f4b71Sopenharmony_ci 799e41f4b71Sopenharmony_ci ```json 800e41f4b71Sopenharmony_ci { 801e41f4b71Sopenharmony_ci "app": { 802e41f4b71Sopenharmony_ci "bundleName": "com.example.myapplication", 803e41f4b71Sopenharmony_ci "vendor": "example", 804e41f4b71Sopenharmony_ci "version": { 805e41f4b71Sopenharmony_ci "code": 1, 806e41f4b71Sopenharmony_ci "name": "1.0" 807e41f4b71Sopenharmony_ci }, 808e41f4b71Sopenharmony_ci "apiVersion": { 809e41f4b71Sopenharmony_ci "compatible": 4, 810e41f4b71Sopenharmony_ci "target": 5 // Set it based on the tested SDK version. In this example, SDK5 is used. 811e41f4b71Sopenharmony_ci } 812e41f4b71Sopenharmony_ci }, 813e41f4b71Sopenharmony_ci "deviceConfig": {}, 814e41f4b71Sopenharmony_ci "module": { 815e41f4b71Sopenharmony_ci "package": "com.example.myapplication", 816e41f4b71Sopenharmony_ci "name": ".MyApplication", 817e41f4b71Sopenharmony_ci "deviceType": [ 818e41f4b71Sopenharmony_ci "phone" 819e41f4b71Sopenharmony_ci ], 820e41f4b71Sopenharmony_ci "distro": { 821e41f4b71Sopenharmony_ci "deliveryWithInstall": true, 822e41f4b71Sopenharmony_ci "moduleName": "entry", 823e41f4b71Sopenharmony_ci "moduleType": "entry" 824e41f4b71Sopenharmony_ci }, 825e41f4b71Sopenharmony_ci "abilities": [ 826e41f4b71Sopenharmony_ci { 827e41f4b71Sopenharmony_ci "skills": [ 828e41f4b71Sopenharmony_ci { 829e41f4b71Sopenharmony_ci "entities": [ 830e41f4b71Sopenharmony_ci "entity.system.home" 831e41f4b71Sopenharmony_ci ], 832e41f4b71Sopenharmony_ci "actions": [ 833e41f4b71Sopenharmony_ci "action.system.home" 834e41f4b71Sopenharmony_ci ] 835e41f4b71Sopenharmony_ci } 836e41f4b71Sopenharmony_ci ], 837e41f4b71Sopenharmony_ci "name": "com.example.myapplication.MainAbility", 838e41f4b71Sopenharmony_ci "icon": "$media:icon", 839e41f4b71Sopenharmony_ci "description": "$string:mainability_description", 840e41f4b71Sopenharmony_ci "label": "MyApplication", 841e41f4b71Sopenharmony_ci "type": "page", 842e41f4b71Sopenharmony_ci "launchType": "standard" 843e41f4b71Sopenharmony_ci } 844e41f4b71Sopenharmony_ci ], 845e41f4b71Sopenharmony_ci "js": [ 846e41f4b71Sopenharmony_ci { 847e41f4b71Sopenharmony_ci "pages": [ 848e41f4b71Sopenharmony_ci "pages/index/index" 849e41f4b71Sopenharmony_ci ], 850e41f4b71Sopenharmony_ci "name": "default", 851e41f4b71Sopenharmony_ci "window": { 852e41f4b71Sopenharmony_ci "designWidth": 720, 853e41f4b71Sopenharmony_ci "autoDesignWidth": false 854e41f4b71Sopenharmony_ci } 855e41f4b71Sopenharmony_ci } 856e41f4b71Sopenharmony_ci ] 857e41f4b71Sopenharmony_ci } 858e41f4b71Sopenharmony_ci } 859e41f4b71Sopenharmony_ci ``` 860e41f4b71Sopenharmony_ci 861e41f4b71Sopenharmony_ci 6. Group the test case files by test type. 862e41f4b71Sopenharmony_ci 863e41f4b71Sopenharmony_ci ``` 864e41f4b71Sopenharmony_ci group("unittest") { 865e41f4b71Sopenharmony_ci testonly = true 866e41f4b71Sopenharmony_ci deps = [ ":GetAppInfoJsTest" ] 867e41f4b71Sopenharmony_ci } 868e41f4b71Sopenharmony_ci ``` 869e41f4b71Sopenharmony_ci > **NOTE** 870e41f4b71Sopenharmony_ci > 871e41f4b71Sopenharmony_ci > Grouping test cases by test type allows you to execute a specific type of test cases when required. 872e41f4b71Sopenharmony_ci 873e41f4b71Sopenharmony_ci- **Example of ArkTS case compilation configuration for the stage model** 874e41f4b71Sopenharmony_ci 875e41f4b71Sopenharmony_ci ``` 876e41f4b71Sopenharmony_ci # Copyright (C) 2023 XXXX Device Co., Ltd. 877e41f4b71Sopenharmony_ci 878e41f4b71Sopenharmony_ci import("//build/test.gni") 879e41f4b71Sopenharmony_ci 880e41f4b71Sopenharmony_ci want_output_path = "developertest/stage_test" 881e41f4b71Sopenharmony_ci 882e41f4b71Sopenharmony_ci ohos_js_stage_unittest("ActsBundleMgrStageEtsTest") { 883e41f4b71Sopenharmony_ci hap_profile = "entry/src/main/module.json" 884e41f4b71Sopenharmony_ci deps = [ 885e41f4b71Sopenharmony_ci ":actbmsstageetstest_js_assets", 886e41f4b71Sopenharmony_ci ":actbmsstageetstest_resources", 887e41f4b71Sopenharmony_ci ] 888e41f4b71Sopenharmony_ci ets2abc = true 889e41f4b71Sopenharmony_ci certificate_profile = "signature/openharmony_sx.p7b" 890e41f4b71Sopenharmony_ci hap_name = "ActsBundleMgrStageEtsTest" 891e41f4b71Sopenharmony_ci subsystem_name = "developertest" 892e41f4b71Sopenharmony_ci part_name = "stage_test" 893e41f4b71Sopenharmony_ci module_out_path = want_output_path 894e41f4b71Sopenharmony_ci } 895e41f4b71Sopenharmony_ci ohos_app_scope("actbmsstageetstest_app_profile") { 896e41f4b71Sopenharmony_ci app_profile = "AppScope/app.json" 897e41f4b71Sopenharmony_ci sources = [ "AppScope/resources" ] 898e41f4b71Sopenharmony_ci } 899e41f4b71Sopenharmony_ci ohos_js_assets("actbmsstageetstest_js_assets") { 900e41f4b71Sopenharmony_ci source_dir = "entry/src/main/ets" 901e41f4b71Sopenharmony_ci } 902e41f4b71Sopenharmony_ci ohos_resources("actbmsstageetstest_resources") { 903e41f4b71Sopenharmony_ci sources = [ "entry/src/main/resources" ] 904e41f4b71Sopenharmony_ci deps = [ ":actbmsstageetstest_app_profile" ] 905e41f4b71Sopenharmony_ci hap_profile = "entry/src/main/module.json" 906e41f4b71Sopenharmony_ci } 907e41f4b71Sopenharmony_ci group("unittest") { 908e41f4b71Sopenharmony_ci testonly = true 909e41f4b71Sopenharmony_ci deps = [] 910e41f4b71Sopenharmony_ci deps += [ ":ActsBundleMgrStageEtsTest" ] 911e41f4b71Sopenharmony_ci } 912e41f4b71Sopenharmony_ci ``` 913e41f4b71Sopenharmony_ci The procedure is as follows: 914e41f4b71Sopenharmony_ci 915e41f4b71Sopenharmony_ci 1. Add comment information for the file header. 916e41f4b71Sopenharmony_ci 917e41f4b71Sopenharmony_ci ``` 918e41f4b71Sopenharmony_ci # Copyright (C) 2023 XXXX Device Co., Ltd. 919e41f4b71Sopenharmony_ci ``` 920e41f4b71Sopenharmony_ci 921e41f4b71Sopenharmony_ci 2. Import the build template. 922e41f4b71Sopenharmony_ci 923e41f4b71Sopenharmony_ci ``` 924e41f4b71Sopenharmony_ci import("//build/test.gni") 925e41f4b71Sopenharmony_ci ``` 926e41f4b71Sopenharmony_ci 927e41f4b71Sopenharmony_ci 3. Specify the file output path. 928e41f4b71Sopenharmony_ci 929e41f4b71Sopenharmony_ci ``` 930e41f4b71Sopenharmony_ci want_output_path = "developertest/stage_test" 931e41f4b71Sopenharmony_ci ``` 932e41f4b71Sopenharmony_ci > **NOTE** 933e41f4b71Sopenharmony_ci > 934e41f4b71Sopenharmony_ci > The output path is the *Part name*/*Module name*. 935e41f4b71Sopenharmony_ci 936e41f4b71Sopenharmony_ci 4. Set the output build file for the test cases. 937e41f4b71Sopenharmony_ci 938e41f4b71Sopenharmony_ci ``` 939e41f4b71Sopenharmony_ci ohos_js_stage_unittest("ActsBundleMgrStageEtsTest") { 940e41f4b71Sopenharmony_ci } 941e41f4b71Sopenharmony_ci ``` 942e41f4b71Sopenharmony_ci > **NOTE** 943e41f4b71Sopenharmony_ci > 944e41f4b71Sopenharmony_ci > Use the **ohos_js_stage_unittest** template to define the ArkTS test suite for the stage model. 945e41f4b71Sopenharmony_ci 946e41f4b71Sopenharmony_ci 5. Specify the configuration file **module.json**, signature file, part name, and compilation output path, which are all mandatory. 947e41f4b71Sopenharmony_ci 948e41f4b71Sopenharmony_ci ``` 949e41f4b71Sopenharmony_ci ohos_js_stage_unittest("ActsBundleMgrStageEtsTest") { 950e41f4b71Sopenharmony_ci hap_profile = "entry/src/main/module.json" 951e41f4b71Sopenharmony_ci certificate_profile = "signature/openharmony_sx.p7b" 952e41f4b71Sopenharmony_ci subsystem_name = "developertest" 953e41f4b71Sopenharmony_ci part_name = "stage_test" // Part name 954e41f4b71Sopenharmony_ci } 955e41f4b71Sopenharmony_ci ``` 956e41f4b71Sopenharmony_ci 957e41f4b71Sopenharmony_ci 6. Specify the configuration resource file (add the source files, configurations, and dependencies). 958e41f4b71Sopenharmony_ci ``` 959e41f4b71Sopenharmony_ci # Declare an AppScope module for the HAP. Those specified by app_profile and sources will be combined to a specific entry file for compilation. 960e41f4b71Sopenharmony_ci ohos_app_scope("actbmsstageetstest_app_profile") { 961e41f4b71Sopenharmony_ci app_profile = "AppScope/app.json" 962e41f4b71Sopenharmony_ci sources = [ "AppScope/resources" ] 963e41f4b71Sopenharmony_ci } 964e41f4b71Sopenharmony_ci 965e41f4b71Sopenharmony_ci # Place the test case code for the stage model in the ets directory. 966e41f4b71Sopenharmony_ci ohos_js_assets("actbmsstageetstest_js_assets") { 967e41f4b71Sopenharmony_ci source_dir = "entry/src/main/ets" 968e41f4b71Sopenharmony_ci } 969e41f4b71Sopenharmony_ci 970e41f4b71Sopenharmony_ci # Source files are stored in the resources directory after compilation in the stage model. 971e41f4b71Sopenharmony_ci ohos_resources("actbmsstageetstest_resources") { 972e41f4b71Sopenharmony_ci sources = [ "entry/src/main/resources" ] 973e41f4b71Sopenharmony_ci deps = [ ":actbmsstageetstest_app_profile" ] 974e41f4b71Sopenharmony_ci hap_profile = "entry/src/main/module.json" 975e41f4b71Sopenharmony_ci } 976e41f4b71Sopenharmony_ci 977e41f4b71Sopenharmony_ci ``` 978e41f4b71Sopenharmony_ci 979e41f4b71Sopenharmony_ci 7. Group the test case files by test type. 980e41f4b71Sopenharmony_ci 981e41f4b71Sopenharmony_ci ``` 982e41f4b71Sopenharmony_ci group("unittest") { 983e41f4b71Sopenharmony_ci testonly = true 984e41f4b71Sopenharmony_ci deps = [ ":GetAppInfoJsTest" ] 985e41f4b71Sopenharmony_ci } 986e41f4b71Sopenharmony_ci ``` 987e41f4b71Sopenharmony_ci > **NOTE** 988e41f4b71Sopenharmony_ci > 989e41f4b71Sopenharmony_ci > Grouping test cases by test type allows you to execute a specific type of test cases when required. 990e41f4b71Sopenharmony_ci 991e41f4b71Sopenharmony_ci**Configuring bundle.json** 992e41f4b71Sopenharmony_ci 993e41f4b71Sopenharmony_ciConfigure the part build file to associate with specific test cases. 994e41f4b71Sopenharmony_ci``` 995e41f4b71Sopenharmony_ci"build": { 996e41f4b71Sopenharmony_ci "sub_component": [ 997e41f4b71Sopenharmony_ci "//test/testfwk/developer_test/examples/app_info:app_info", 998e41f4b71Sopenharmony_ci "//test/testfwk/developer_test/examples/detector:detector", 999e41f4b71Sopenharmony_ci "//test/testfwk/developer_test/examples/calculator:calculator" 1000e41f4b71Sopenharmony_ci ], 1001e41f4b71Sopenharmony_ci "inner_list": [ 1002e41f4b71Sopenharmony_ci { 1003e41f4b71Sopenharmony_ci "header": { 1004e41f4b71Sopenharmony_ci "header_base": "////test/testfwk/developer_test/examples/detector/include", 1005e41f4b71Sopenharmony_ci "header_files": [ 1006e41f4b71Sopenharmony_ci "detector.h" 1007e41f4b71Sopenharmony_ci ] 1008e41f4b71Sopenharmony_ci }, 1009e41f4b71Sopenharmony_ci "name": "//test/testfwk/developer_test/examples/detector:detector" 1010e41f4b71Sopenharmony_ci } 1011e41f4b71Sopenharmony_ci ], 1012e41f4b71Sopenharmony_ci "test": [ // Test under configuration module calculator. 1013e41f4b71Sopenharmony_ci "//test/testfwk/developer_test/examples/app_info/test:unittest", 1014e41f4b71Sopenharmony_ci "//test/testfwk/developer_test/examples/calculator/test:unittest", 1015e41f4b71Sopenharmony_ci "//test/testfwk/developer_test/examples/calculator/test:fuzztest" 1016e41f4b71Sopenharmony_ci } 1017e41f4b71Sopenharmony_ci``` 1018e41f4b71Sopenharmony_ci> **NOTE** 1019e41f4b71Sopenharmony_ci> 1020e41f4b71Sopenharmony_ci> **test_list** contains the test cases of the corresponding module. 1021e41f4b71Sopenharmony_ci 1022e41f4b71Sopenharmony_ci## Configuring Test Resources 1023e41f4b71Sopenharmony_ci 1024e41f4b71Sopenharmony_ciThe test resources mainly include external file resources such as image files, video files, and third-party libraries required during test case execution. Currently, only static resources can be configured. 1025e41f4b71Sopenharmony_ci 1026e41f4b71Sopenharmony_ciPerform the following steps: 1027e41f4b71Sopenharmony_ci 1028e41f4b71Sopenharmony_ci1. Create the **resource** directory in the **test** directory of the part, and create a directory for the module in the **resource** directory to store resource files of the module. 1029e41f4b71Sopenharmony_ci 1030e41f4b71Sopenharmony_ci2. In the module directory under **resource**, create the **ohos_test.xml** file in the following format: 1031e41f4b71Sopenharmony_ci 1032e41f4b71Sopenharmony_ci ```xml 1033e41f4b71Sopenharmony_ci <?xml version="1.0" encoding="UTF-8"?> 1034e41f4b71Sopenharmony_ci <configuration ver="2.0"> 1035e41f4b71Sopenharmony_ci <target name="CalculatorSubTest"> 1036e41f4b71Sopenharmony_ci <preparer> 1037e41f4b71Sopenharmony_ci <option name="push" value="test.jpg -> /data/test/resource" src="res"/> 1038e41f4b71Sopenharmony_ci <option name="push" value="libc++.z.so -> /data/test/resource" src="out"/> 1039e41f4b71Sopenharmony_ci </preparer> 1040e41f4b71Sopenharmony_ci </target> 1041e41f4b71Sopenharmony_ci </configuration> 1042e41f4b71Sopenharmony_ci ``` 1043e41f4b71Sopenharmony_ci 1044e41f4b71Sopenharmony_ci 3. In the build file of the test cases, configure **resource_config_file** to point to the resource file **ohos_test.xml**. 1045e41f4b71Sopenharmony_ci 1046e41f4b71Sopenharmony_ci ``` 1047e41f4b71Sopenharmony_ci ohos_unittest("CalculatorSubTest") { 1048e41f4b71Sopenharmony_ci resource_config_file = "//system/subsystem/partA/test/resource/calculator/ohos_test.xml" 1049e41f4b71Sopenharmony_ci } 1050e41f4b71Sopenharmony_ci ``` 1051e41f4b71Sopenharmony_ci > **NOTE** 1052e41f4b71Sopenharmony_ci > 1053e41f4b71Sopenharmony_ci >- **target_name** indicates the test suite name defined in the **BUILD.gn** file in the **test** directory. **preparer** indicates the action to perform before the test suite is executed. 1054e41f4b71Sopenharmony_ci >- **src="res"** indicates that the test resources are in the **resource** directory under the **test** directory. **src="out"** indicates that the test resources are in the **out/release/$(*part*)** directory. 1055e41f4b71Sopenharmony_ci 1056e41f4b71Sopenharmony_ci## Test Case Execution 1057e41f4b71Sopenharmony_ci 1058e41f4b71Sopenharmony_ci### Configuration File user_config.xml 1059e41f4b71Sopenharmony_ci 1060e41f4b71Sopenharmony_ciBefore executing test cases, you need to modify the configuration in **developer_test\config\user_config.xml** based on the device used. 1061e41f4b71Sopenharmony_ci 1062e41f4b71Sopenharmony_ci```xml 1063e41f4b71Sopenharmony_ci<user_config> 1064e41f4b71Sopenharmony_ci <build> 1065e41f4b71Sopenharmony_ci <!-- Whether to build a demo case. The default value is false. If a demo case is required, change the value to true. --> 1066e41f4b71Sopenharmony_ci <example>false</example> 1067e41f4b71Sopenharmony_ci <!-- Whether to build the version. The default value is false. --> 1068e41f4b71Sopenharmony_ci <version>false</version> 1069e41f4b71Sopenharmony_ci <!-- Whether to build the test cases. The default value is true. If the build is already complete, change the value to false before executing the test cases.--> 1070e41f4b71Sopenharmony_ci <testcase>true</testcase> 1071e41f4b71Sopenharmony_ci <!--When compiling test cases, select whether the target CPU is of the 64-bit or 32-bit. The default value is null (32-bit). You can select arm64. --> 1072e41f4b71Sopenharmony_ci <parameter> 1073e41f4b71Sopenharmony_ci <target_cpu></target_cpu> 1074e41f4b71Sopenharmony_ci </parameter> 1075e41f4b71Sopenharmony_ci </build> 1076e41f4b71Sopenharmony_ci <environment> 1077e41f4b71Sopenharmony_ci <!-- Configure the IP address and port number of the remote server to support connection to the device through the OpenHarmony Device Connector (HDC).--> 1078e41f4b71Sopenharmony_ci <device type="usb-hdc"> 1079e41f4b71Sopenharmony_ci <ip></ip> 1080e41f4b71Sopenharmony_ci <port></port> 1081e41f4b71Sopenharmony_ci <sn></sn> 1082e41f4b71Sopenharmony_ci </device> 1083e41f4b71Sopenharmony_ci <!-- Configure the serial port information of the device to enable connection through the serial port.--> 1084e41f4b71Sopenharmony_ci <device type="com" label="ipcamera"> 1085e41f4b71Sopenharmony_ci <serial> 1086e41f4b71Sopenharmony_ci <com></com> 1087e41f4b71Sopenharmony_ci <type>cmd</type> 1088e41f4b71Sopenharmony_ci <baud_rate>115200</baud_rate> 1089e41f4b71Sopenharmony_ci <data_bits>8</data_bits> 1090e41f4b71Sopenharmony_ci <stop_bits>1</stop_bits> 1091e41f4b71Sopenharmony_ci <timeout>1</timeout> 1092e41f4b71Sopenharmony_ci </serial> 1093e41f4b71Sopenharmony_ci </device> 1094e41f4b71Sopenharmony_ci </environment> 1095e41f4b71Sopenharmony_ci <!-- Configure the test case path. If the test cases have not been built (<testcase> is true), leave this parameter blank. If the build is complete, enter the path of the test cases.--> 1096e41f4b71Sopenharmony_ci <test_cases> 1097e41f4b71Sopenharmony_ci <dir></dir> 1098e41f4b71Sopenharmony_ci </test_cases> 1099e41f4b71Sopenharmony_ci <!-- Configure the coverage output path.--> 1100e41f4b71Sopenharmony_ci <coverage> 1101e41f4b71Sopenharmony_ci <outpath></outpath> 1102e41f4b71Sopenharmony_ci </coverage> 1103e41f4b71Sopenharmony_ci <!-- Configure the NFS mount information when the tested device supports only the serial port connection. Specify the NFS mapping path. host_dir indicates the NFS directory on the PC, and board_dir indicates the directory created on the board. --> 1104e41f4b71Sopenharmony_ci <NFS> 1105e41f4b71Sopenharmony_ci <host_dir></host_dir> 1106e41f4b71Sopenharmony_ci <mnt_cmd></mnt_cmd> 1107e41f4b71Sopenharmony_ci <board_dir></board_dir> 1108e41f4b71Sopenharmony_ci </NFS> 1109e41f4b71Sopenharmony_ci</user_config> 1110e41f4b71Sopenharmony_ci``` 1111e41f4b71Sopenharmony_ci> **NOTE** 1112e41f4b71Sopenharmony_ci> 1113e41f4b71Sopenharmony_ci> If HDC is connected to the device before the test cases are executed, you only need to configure the device IP address and port number, and retain the default settings for other parameters. 1114e41f4b71Sopenharmony_ci 1115e41f4b71Sopenharmony_ci### Executing Test Cases on Windows 1116e41f4b71Sopenharmony_ci#### **Test Case Building** 1117e41f4b71Sopenharmony_ci 1118e41f4b71Sopenharmony_ciTest cases cannot be built on Windows. You need to run the following command to build test cases on Linux: 1119e41f4b71Sopenharmony_ci``` 1120e41f4b71Sopenharmony_ci./build.sh --product-name {product_name} --build-target make_test 1121e41f4b71Sopenharmony_ci``` 1122e41f4b71Sopenharmony_ci 1123e41f4b71Sopenharmony_ci> **NOTE** 1124e41f4b71Sopenharmony_ci> 1125e41f4b71Sopenharmony_ci>- **product-name**: specifies the name of the product to be compiled. 1126e41f4b71Sopenharmony_ci>- **build-target**: specifies the test case to build. **make_test** indicates all test cases. You can specify the test cases based on requirements. 1127e41f4b71Sopenharmony_ci 1128e41f4b71Sopenharmony_ciWhen the build is complete, the test cases are automatically saved in **out/ohos-arm-release/packages/phone/tests**. 1129e41f4b71Sopenharmony_ci 1130e41f4b71Sopenharmony_ci#### Setting Up the Execution Environment 1131e41f4b71Sopenharmony_ci1. On Windows, create the **Test** directory in the test framework and then create the **testcase** directory in the **Test** directory. 1132e41f4b71Sopenharmony_ci 1133e41f4b71Sopenharmony_ci2. Copy **developertest** and **xdevice** from the Linux environment to the **Test** directory on Windows, and copy the test cases to the **testcase** directory. 1134e41f4b71Sopenharmony_ci 1135e41f4b71Sopenharmony_ci > **NOTE** 1136e41f4b71Sopenharmony_ci > 1137e41f4b71Sopenharmony_ci > Port the test framework and test cases from the Linux environment to the Windows environment for subsequent execution. 1138e41f4b71Sopenharmony_ci 1139e41f4b71Sopenharmony_ci3. Modify the **user_config.xml** file. 1140e41f4b71Sopenharmony_ci ```xml 1141e41f4b71Sopenharmony_ci <build> 1142e41f4b71Sopenharmony_ci <!-- Because the test cases have been built, change the value to false. --> 1143e41f4b71Sopenharmony_ci <testcase>false</testcase> 1144e41f4b71Sopenharmony_ci </build> 1145e41f4b71Sopenharmony_ci <test_cases> 1146e41f4b71Sopenharmony_ci <!-- The test cases are copied to the Windows environment. Change the test case output path to the path of the test cases in the Windows environment.--> 1147e41f4b71Sopenharmony_ci <dir>D:\Test\testcase\tests</dir> 1148e41f4b71Sopenharmony_ci </test_cases> 1149e41f4b71Sopenharmony_ci ``` 1150e41f4b71Sopenharmony_ci > **NOTE** 1151e41f4b71Sopenharmony_ci > 1152e41f4b71Sopenharmony_ci > **\<testcase>** indicates whether to build test cases. **\<dir>** indicates the path for searching for test cases. 1153e41f4b71Sopenharmony_ci 1154e41f4b71Sopenharmony_ci#### Executing Test Cases 1155e41f4b71Sopenharmony_ci 1156e41f4b71Sopenharmony_ci1. Start the test framework. 1157e41f4b71Sopenharmony_ci ``` 1158e41f4b71Sopenharmony_ci start.bat 1159e41f4b71Sopenharmony_ci ``` 1160e41f4b71Sopenharmony_ci2. Select the product. 1161e41f4b71Sopenharmony_ci 1162e41f4b71Sopenharmony_ci After the test framework starts, you are asked to select a product. Select the development board to test. 1163e41f4b71Sopenharmony_ci 1164e41f4b71Sopenharmony_ci If you need to manually add a product, add it within the **\<productform\>** tag to **config/framework_config.xml**. 1165e41f4b71Sopenharmony_ci 1166e41f4b71Sopenharmony_ci3. Execute the test cases. 1167e41f4b71Sopenharmony_ci 1168e41f4b71Sopenharmony_ci Run the following commands to execute test cases: 1169e41f4b71Sopenharmony_ci ``` 1170e41f4b71Sopenharmony_ci run -t UT 1171e41f4b71Sopenharmony_ci run -t UT -tp PartName 1172e41f4b71Sopenharmony_ci run -t UT -tp PartName -tm TestModuleName 1173e41f4b71Sopenharmony_ci run -t UT -tp ability_base -ts base_object_test 1174e41f4b71Sopenharmony_ci run -t UT -tp PartName -tm TestModuleName -ts CalculatorSubTest 1175e41f4b71Sopenharmony_ci run -t UT -ts base_object_test 1176e41f4b71Sopenharmony_ci run -t UT -ts base_object_test -tc AAFwkBaseObjectTest.BaseObject_test_001 1177e41f4b71Sopenharmony_ci run -t UT -ts CalculatorSubTest -tc CalculatorSubTest.interger_sub_00l 1178e41f4b71Sopenharmony_ci run -t UT -cov coverage 1179e41f4b71Sopenharmony_ci run -t UT -ra random 1180e41f4b71Sopenharmony_ci run -t UT -ts base_object_test --repeat 5 1181e41f4b71Sopenharmony_ci run -hl 1182e41f4b71Sopenharmony_ci run -rh 3 1183e41f4b71Sopenharmony_ci run --retry 1184e41f4b71Sopenharmony_ci ``` 1185e41f4b71Sopenharmony_ci 1186e41f4b71Sopenharmony_ci 1187e41f4b71Sopenharmony_ci In the command: 1188e41f4b71Sopenharmony_ci ``` 1189e41f4b71Sopenharmony_ci -**t [TESTTYPE]**: specifies the test type, which can be **UT**, **MST**, **ST**, **PERF**, **FUZZ**, **BENCHMARK**, **ACTS**, **HATS**, and more. This parameter is mandatory. 1190e41f4b71Sopenharmony_ci -**tp [TESTPART]**: specifies the part to test. This parameter can be used independently. 1191e41f4b71Sopenharmony_ci -**tm [TESTMODULE]**: specifies the module to test. This parameter must be specified together with **-tp**. 1192e41f4b71Sopenharmony_ci -**ts [TESTSUITE]**: specifies the test suite. This parameter can be used independently. 1193e41f4b71Sopenharmony_ci -**tc [TESTCASE]**: specifies the test case. This parameter must be specified together with **-ts** to indicate the test suite. 1194e41f4b71Sopenharmony_ci -**cov [COVERAGE]**: specifies the coverage. 1195e41f4b71Sopenharmony_ci -**h**: displays help information. 1196e41f4b71Sopenharmony_ci -**ra [random]**: specifies the out-of-order execution for C++ cases. 1197e41f4b71Sopenharmony_ci --**repeat**: specifies the number of case execution times. 1198e41f4b71Sopenharmony_ci -**hl [HISTORYLIST]**: enables the display of the latest 10 test cases. If there are more than 10 test cases, only the latest 10 test cases are displayed. 1199e41f4b71Sopenharmony_ci -**rh [RUNHISTORY]**: specifies the sequence number of the historical record to execute. 1200e41f4b71Sopenharmony_ci --**retry**: checks the last execution result and re-runs the failed test cases, if any. 1201e41f4b71Sopenharmony_ci ``` 1202e41f4b71Sopenharmony_ci 1203e41f4b71Sopenharmony_ci 1204e41f4b71Sopenharmony_ci### Executing Test Cases on Linux 1205e41f4b71Sopenharmony_ci 1206e41f4b71Sopenharmony_ci 1207e41f4b71Sopenharmony_ci#### Configuring Remote Port Mapping and Modifying Configuration File 1208e41f4b71Sopenharmony_ciTo enable test cases to be executed on a remote Linux server or a Linux VM, map the port to enable communication between the device and the remote server or VM. Configure port mapping as follows: 1209e41f4b71Sopenharmony_ci1. On the HDC server, run the following commands: 1210e41f4b71Sopenharmony_ci ``` 1211e41f4b71Sopenharmony_ci hdc_std kill 1212e41f4b71Sopenharmony_ci hdc_std -m -s 0.0.0.0:8710 1213e41f4b71Sopenharmony_ci ``` 1214e41f4b71Sopenharmony_ci > **NOTE** 1215e41f4b71Sopenharmony_ci > 1216e41f4b71Sopenharmony_ci > The IP address and port number are default values. 1217e41f4b71Sopenharmony_ci 1218e41f4b71Sopenharmony_ci2. On the HDC client, run the following command: 1219e41f4b71Sopenharmony_ci ``` 1220e41f4b71Sopenharmony_ci hdc_std -s xx.xx.xx.xx:8710 list targets 1221e41f4b71Sopenharmony_ci ``` 1222e41f4b71Sopenharmony_ci > **NOTE** 1223e41f4b71Sopenharmony_ci > 1224e41f4b71Sopenharmony_ci > Enter the IP address of the device to test. 1225e41f4b71Sopenharmony_ci 1226e41f4b71Sopenharmony_ci3. Modify the **user_config.xml** file. 1227e41f4b71Sopenharmony_ci ```xml 1228e41f4b71Sopenharmony_ci <build> 1229e41f4b71Sopenharmony_ci <!--If a test case needs to be compiled, set this attribute is true. Otherwise, set it to false. --> 1230e41f4b71Sopenharmony_ci <testcase>true</testcase> 1231e41f4b71Sopenharmony_ci </build> 1232e41f4b71Sopenharmony_ci <environment> 1233e41f4b71Sopenharmony_ci <!-- Configure the IP address, port number, and SN of the remote server to support connection to the device through HDC. --> 1234e41f4b71Sopenharmony_ci <device type="usb-hdc"> 1235e41f4b71Sopenharmony_ci <ip></ip> 1236e41f4b71Sopenharmony_ci <port></port> 1237e41f4b71Sopenharmony_ci <sn></sn> 1238e41f4b71Sopenharmony_ci </device> 1239e41f4b71Sopenharmony_ci <environment> 1240e41f4b71Sopenharmony_ci ``` 1241e41f4b71Sopenharmony_ci 1242e41f4b71Sopenharmony_ci 1243e41f4b71Sopenharmony_ci#### Executing Test Cases 1244e41f4b71Sopenharmony_ci1. Start the test framework. 1245e41f4b71Sopenharmony_ci ``` 1246e41f4b71Sopenharmony_ci ./start.sh 1247e41f4b71Sopenharmony_ci ``` 1248e41f4b71Sopenharmony_ci2. Select the product. 1249e41f4b71Sopenharmony_ci 1250e41f4b71Sopenharmony_ci After the test framework starts, you are asked to select a product. Select the development board to test. 1251e41f4b71Sopenharmony_ci 1252e41f4b71Sopenharmony_ci If the displayed product list does not contain the target one, you can add it in the **\<productform\>** tag in **config/framework_config.xml**. 1253e41f4b71Sopenharmony_ci 1254e41f4b71Sopenharmony_ci ``` 1255e41f4b71Sopenharmony_ci <framework_config> 1256e41f4b71Sopenharmony_ci <productform> 1257e41f4b71Sopenharmony_ci <option name="ipcamera_hispark_aries" /> 1258e41f4b71Sopenharmony_ci <option name="ipcamera_hispark_taurus" /> 1259e41f4b71Sopenharmony_ci <option name="wifiiot_hispark_pegasus" /> 1260e41f4b71Sopenharmony_ci <option name="" /> 1261e41f4b71Sopenharmony_ci </productform> 1262e41f4b71Sopenharmony_ci </framework_config> 1263e41f4b71Sopenharmony_ci 1264e41f4b71Sopenharmony_ci ``` 1265e41f4b71Sopenharmony_ci 1266e41f4b71Sopenharmony_ci3. Execute the test cases. 1267e41f4b71Sopenharmony_ci 1268e41f4b71Sopenharmony_ci 1. TDD commands 1269e41f4b71Sopenharmony_ci 1270e41f4b71Sopenharmony_ci The test framework locates the test cases based on the command, and automatically builds and executes the test cases. 1271e41f4b71Sopenharmony_ci ``` 1272e41f4b71Sopenharmony_ci run -t UT 1273e41f4b71Sopenharmony_ci run -t UT -tp PartName 1274e41f4b71Sopenharmony_ci run -t UT -tp PartName -tm TestModuleName 1275e41f4b71Sopenharmony_ci run -t UT -tp ability_base -ts base_object_test 1276e41f4b71Sopenharmony_ci run -t UT -tp PartName -tm TestModuleName -ts CalculatorSubTest 1277e41f4b71Sopenharmony_ci run -t UT -ts base_object_test 1278e41f4b71Sopenharmony_ci run -t UT -ts base_object_test -tc AAFwkBaseObjectTest.BaseObject_test_001 1279e41f4b71Sopenharmony_ci run -t UT -ts CalculatorSubTest -tc CalculatorSubTest.interger_sub_00l 1280e41f4b71Sopenharmony_ci run -t -cov coverage 1281e41f4b71Sopenharmony_ci run -t UT -ra random 1282e41f4b71Sopenharmony_ci run -t UT -tp PartName -pd partdeps 1283e41f4b71Sopenharmony_ci run -t UT -ts base_object_test --repeat 5 1284e41f4b71Sopenharmony_ci run -hl 1285e41f4b71Sopenharmony_ci run -rh 3 1286e41f4b71Sopenharmony_ci run --retry 1287e41f4b71Sopenharmony_ci ``` 1288e41f4b71Sopenharmony_ci In the command: 1289e41f4b71Sopenharmony_ci ``` 1290e41f4b71Sopenharmony_ci -**t [TESTTYPE]**: specifies the test type, which can be **UT**, **MST**, **ST**, **PERF**, **FUZZ**, and **BENCHMARK**. This parameter is mandatory. 1291e41f4b71Sopenharmony_ci -**tp [TESTPART]**: specifies the part to test. This parameter can be used independently. 1292e41f4b71Sopenharmony_ci -**tm [TESTMODULE]**: specifies the module to test. This parameter must be specified together with **-tp**. 1293e41f4b71Sopenharmony_ci -**ts [TESTSUITE]**: specifies the test suite. This parameter can be used independently. 1294e41f4b71Sopenharmony_ci -**tc [TESTCASE]**: specifies the test case. This parameter must be specified together with **-ts** to indicate the test suite. 1295e41f4b71Sopenharmony_ci -**cov [COVERAGE]**: specifies the coverage. 1296e41f4b71Sopenharmony_ci -**h**: displays help information. 1297e41f4b71Sopenharmony_ci -**ra [random]**: specifies the out-of-order execution for C++ cases. 1298e41f4b71Sopenharmony_ci -**pd [partdeps]**: specifies execution parameter of the level-2 part dependencies. 1299e41f4b71Sopenharmony_ci --**repeat**: specifies the number of case execution times. 1300e41f4b71Sopenharmony_ci -**hl [HISTORYLIST]**: enables the display of the latest 10 test cases. If there are more than 10 test cases, only the latest 10 test cases are displayed. 1301e41f4b71Sopenharmony_ci -**rh [RUNHISTORY]**: specifies the sequence number of the historical record to execute. 1302e41f4b71Sopenharmony_ci --**retry**: checks the last execution result and re-runs the failed test cases, if any. 1303e41f4b71Sopenharmony_ci ``` 1304e41f4b71Sopenharmony_ci 1305e41f4b71Sopenharmony_ci In Linux, you can run the following commands to view the supported product forms, test types, subsystems, and parts. 1306e41f4b71Sopenharmony_ci ``` 1307e41f4b71Sopenharmony_ci To view the help information, run **help**. 1308e41f4b71Sopenharmony_ci To view the **show** command, run **help show**. 1309e41f4b71Sopenharmony_ci To view the supported product forms, run **show productlist**. 1310e41f4b71Sopenharmony_ci To view the supported test types, run **show typelist**. 1311e41f4b71Sopenharmony_ci To view the supported test subsystems, run **show subsystemlist** . 1312e41f4b71Sopenharmony_ci To view the supported test parts, run **show partlist**. 1313e41f4b71Sopenharmony_ci ``` 1314e41f4b71Sopenharmony_ci 2. ACTS/HATS commands 1315e41f4b71Sopenharmony_ci 1316e41f4b71Sopenharmony_ci After selecting the product, you can refer to the following to execute the ACTS or HATS test cases. 1317e41f4b71Sopenharmony_ci ``` 1318e41f4b71Sopenharmony_ci run -t ACTS 1319e41f4b71Sopenharmony_ci run -t HATS 1320e41f4b71Sopenharmony_ci run -t ACTS -ss arkui 1321e41f4b71Sopenharmony_ci run -t ACTS -ss arkui, modulemanager 1322e41f4b71Sopenharmony_ci run -t ACTS -ss arkui -ts ActsAceEtsTest 1323e41f4b71Sopenharmony_ci run -t HATS -ss telephony -ts HatsHdfV1RilServiceTest 1324e41f4b71Sopenharmony_ci run -t ACTS -ss arkui -tp ActsPartName 1325e41f4b71Sopenharmony_ci run -t ACTS -ss arkui -ts ActsAceEtsTest,ActsAceEtsResultTest 1326e41f4b71Sopenharmony_ci run -t HATS -ss powermgr -ts HatsPowermgrBatteryTest,HatsPowermgrThermalTest 1327e41f4b71Sopenharmony_ci run -t ACTS -ss arkui -ts ActsAceEtsTest -ta class:alphabetIndexerTest#alphabetIndexerTest001 1328e41f4b71Sopenharmony_ci run -t ACTS -ss arkui -ts ActsAceEtsTest -ta class:alphabetIndexerTest#alphabetIndexerTest001 --repeat 2 1329e41f4b71Sopenharmony_ci run -hl 1330e41f4b71Sopenharmony_ci run -rh 1 1331e41f4b71Sopenharmony_ci run --retry 1332e41f4b71Sopenharmony_ci ``` 1333e41f4b71Sopenharmony_ci The parameters in the ACTS and HATS commands are the same, but are different from those in TDD commands. 1334e41f4b71Sopenharmony_ci ``` 1335e41f4b71Sopenharmony_ci -**t [TESTTYPE]**: specifies the test case type, which can be **ACTS** or **HATS**. This parameter is mandatory. 1336e41f4b71Sopenharmony_ci -**ss [SUBSYSTEM]**: specifies the subsystem to test. This parameter can be used independently. To specify multiple subsystems, separate them with commas (,). 1337e41f4b71Sopenharmony_ci -**tp [TESTPART]**: specifies the part to test. This parameter can be used independently. 1338e41f4b71Sopenharmony_ci -**ts [TESTSUITE]**: specifies the test suite. This parameter can be used independently. To specify multiple test suites, separate them with commas (,). 1339e41f4b71Sopenharmony_ci -**ta [TESTARGS]**: specifies the test method. This parameter must be used together with **-ts**. 1340e41f4b71Sopenharmony_ci --**repeat**: specifies the number of case execution times. 1341e41f4b71Sopenharmony_ci -**hl [HISTORYLIST]**: enables the display of the latest 10 test cases. If there are more than 10 test cases, only the latest 10 test cases are displayed. 1342e41f4b71Sopenharmony_ci -**rh [RUNHISTORY]**: specifies the sequence number of the historical record to execute. 1343e41f4b71Sopenharmony_ci --**retry**: checks the last execution result and re-runs the failed test cases, if any. 1344e41f4b71Sopenharmony_ci ``` 1345e41f4b71Sopenharmony_ci 1346e41f4b71Sopenharmony_ci## Viewing the Test Result 1347e41f4b71Sopenharmony_ci 1348e41f4b71Sopenharmony_ci 1349e41f4b71Sopenharmony_ciAfter the test is executed, the console automatically generates the test result. 1350e41f4b71Sopenharmony_ci 1351e41f4b71Sopenharmony_ciYou can obtain the test result in the following directory: 1352e41f4b71Sopenharmony_ci``` 1353e41f4b71Sopenharmony_citest/developertest/reports/xxxx_xx_xx_xx_xx_xx 1354e41f4b71Sopenharmony_ci``` 1355e41f4b71Sopenharmony_ci> **NOTE** 1356e41f4b71Sopenharmony_ci> 1357e41f4b71Sopenharmony_ci> The test report folder is automatically generated. 1358e41f4b71Sopenharmony_ci 1359e41f4b71Sopenharmony_ciThe folder contains the following files: 1360e41f4b71Sopenharmony_ci| Type | Description | 1361e41f4b71Sopenharmony_ci| ------------------------------------ | ------------------ | 1362e41f4b71Sopenharmony_ci| result/ | Test cases in standard format.| 1363e41f4b71Sopenharmony_ci| log/plan_log_xxxx_xx_xx_xx_xx_xx.log | Test case logs. | 1364e41f4b71Sopenharmony_ci| summary_report.html | Test report summary. | 1365e41f4b71Sopenharmony_ci| details_report.html | Detailed test report. | 1366e41f4b71Sopenharmony_ci 1367e41f4b71Sopenharmony_ci 1368e41f4b71Sopenharmony_ci 1369e41f4b71Sopenharmony_ci## Executing Coverage Cases 1370e41f4b71Sopenharmony_ciWhen GCDA data is available, you can execute the test cases as follows for subsystems to generate a coverage report: 1371e41f4b71Sopenharmony_ci 1372e41f4b71Sopenharmony_ci1. (Optional) To block redundant branch data generated by non-core code, run the following command in the **/test/testfwk/developer_test/localCoverage/restore_comment** directory before source code compilation: 1373e41f4b71Sopenharmony_ci 1374e41f4b71Sopenharmony_ci python3 build_before_generate.py 1375e41f4b71Sopenharmony_ci 1376e41f4b71Sopenharmony_ci Run the following command to select the parts to be blocked during compilation: 1377e41f4b71Sopenharmony_ci 1378e41f4b71Sopenharmony_ci run -tp partname 1379e41f4b71Sopenharmony_ci run -tp partname1 partname2 1380e41f4b71Sopenharmony_ci 1381e41f4b71Sopenharmony_ci2. Before compiling the version, modify the compilation options. Add **-- coverage** to the **cflags**, **cflags_cc**, and **ldflags** options in the **build.gn** file of the involved subsystem. 1382e41f4b71Sopenharmony_ci 1383e41f4b71Sopenharmony_ci ldflags = [ "--coverage" ] 1384e41f4b71Sopenharmony_ci C: cflags = [ "--coverage" ] 1385e41f4b71Sopenharmony_ci C++: cflags_cc = [ "--coverage" ] 1386e41f4b71Sopenharmony_ci 1387e41f4b71Sopenharmony_ci **Recommended**: You can also refer to the mode for the window subsystem. For details, see the files in this [pull request](https://gitee.com/openharmony/window_window_manager/pulls/1274/files). 1388e41f4b71Sopenharmony_ci 1389e41f4b71Sopenharmony_ci3. To execute coverage test cases, perform the following to install the dependencies: 1390e41f4b71Sopenharmony_ci 1391e41f4b71Sopenharmony_ci 1. Run the **sudo apt install lcov** command to install lcov. 1392e41f4b71Sopenharmony_ci 2. Run the **apt install dos2unix** command to install dos2unix. 1393e41f4b71Sopenharmony_ci 3. Run the **pip install lxml** command to install lxml. 1394e41f4b71Sopenharmony_ci 4. Run the **pip install selectolax** command to install selectolax. 1395e41f4b71Sopenharmony_ci 5. Run the **pip install CppHeaderParser** command to install CppHeaderParser. 1396e41f4b71Sopenharmony_ci 1397e41f4b71Sopenharmony_ci4. To map a remote device, set its IP address in the **usr_config.xml** file. For details about device mapping, see [Configuring Remote Port Mapping and Modifying Configuration File](#configuring-remote-port-mapping-and-modifying-configuration-file). 1398e41f4b71Sopenharmony_ci 1399e41f4b71Sopenharmony_ci <!-- Set the IP address of the remote host to map (IP address of the PC to which the device is mounted).--> 1400e41f4b71Sopenharmony_ci <device type="usb-hdc"> 1401e41f4b71Sopenharmony_ci <ip></ip> 1402e41f4b71Sopenharmony_ci <port></port> 1403e41f4b71Sopenharmony_ci <sn></sn> 1404e41f4b71Sopenharmony_ci </device> 1405e41f4b71Sopenharmony_ci 1406e41f4b71Sopenharmony_ci5. Run the **./start.sh** command. Below are examples: 1407e41f4b71Sopenharmony_ci 1408e41f4b71Sopenharmony_ci run -t UT -tp *Part name* -cov coverage 1409e41f4b71Sopenharmony_ci run -t UT -ss *Subsystem name* -cov coverage 1410e41f4b71Sopenharmony_ci run -t UT -ss *Subsystem name* -tp **Part name** -cov coverage 1411e41f4b71Sopenharmony_ci run -t UT MST ST -tp *Part name* -cov coverage 1412e41f4b71Sopenharmony_ci 1413e41f4b71Sopenharmony_ci > **NOTE** 1414e41f4b71Sopenharmony_ci > 1415e41f4b71Sopenharmony_ci > The **-cov coverage** parameter must be added to the preceding commands. 1416e41f4b71Sopenharmony_ci 1417e41f4b71Sopenharmony_ci6. Obtain the coverage report from the following paths: 1418e41f4b71Sopenharmony_ci 1419e41f4b71Sopenharmony_ci Code coverage report: **/test/testfwk/developer_test/localCoverage/codeCoverage/results/coverage/reports/cxx/html** 1420e41f4b71Sopenharmony_ci 1421e41f4b71Sopenharmony_ci API coverage report: **/test/testfwk/developer_test/localCoverage/interfaceCoverage/results/coverage/interface_kits/html** 1422