test.md 28.8 KB
Newer Older
A
Annie_wang 已提交
1
# Test
A
annie_wangli 已提交
2 3 4 5
OpenHarmony provides a comprehensive auto-test framework for designing test cases. Detecting defects in the development process can improve code quality.

This document describes how to use the OpenHarmony test framework.
## Setting Up the Environment
A
Annie_wang 已提交
6 7
The test framework depends on Python. Before using the test framework, set up the environment as follows:
 - [Configuring the Environment](../device-dev/device-test/xdevice.md)
A
annie_wangli 已提交
8 9 10 11 12 13 14
 - [Obtaining Source Code](../device-dev/get-code/sourcecode-acquire.md)


## Directory Structure
The directory structure of the test framework is as follows:
```
test # Test subsystem
A
Annie_wang 已提交
15 16
├── developertest             # Developer test module
│   ├── aw                    # Static library of the test framework
A
Annie_wang 已提交
17
│   ├── config                # Test framework configuration
A
annie_wangli 已提交
18
│   │   │ ...
A
Annie_wang 已提交
19 20 21 22 23 24 25 26 27
│   │   └── user_config.xml   # User configuration
│   ├── examples              # Examples of test cases
│   ├── src                   # Source code of the test framework
│   ├── third_party           # Adaptation code for third-party components on which the test framework depends
│   ├── reports               # Test reports
│   ├── BUILD.gn              # Build entry of the test framework
│   ├── start.bat             # Test entry for Windows
│   └── start.sh              # Test entry for Linux
└── xdevice                   # Modules on which the test framework depends
A
annie_wangli 已提交
28 29 30 31 32
```
## Writing Test Cases
###  Designing the Test Case Directory
Design the test case directory as follows:
```
A
Annie_wang 已提交
33 34 35
subsystem                                  # Subsystem
├── partA                                  # Part A
│   ├── moduleA                            # Module A
A
annie_wangli 已提交
36
│   │   ├── include       
A
Annie_wang 已提交
37 38 39 40 41 42 43 44 45 46
│   │   ├── src                            # Service code
│   │   └── test                           # Test directory
│   │       ├── unittest                   # Unit tests
│   │       │   ├── common                 # Common test cases
│   │       │   │   ├── BUILD.gn           # Build file of test cases
│   │       │   │   └── testA_test.cpp     # Source code of unit test cases
│   │       │   ├── phone                  # Test cases for smart phones
│   │       │   ├── ivi                    # Test cases for head units
│   │       │   └── liteos-a               # Test cases for IP cameras that use the LiteOS kernel
│   │       ├── moduletest                 # Module tests
A
Annie_wang 已提交
47 48
│   │       ...
│   │            
A
Annie_wang 已提交
49
│   ├── moduleB                      # Module B   
A
annie_wangli 已提交
50
│   ├── test               
A
Annie_wang 已提交
51 52 53 54
│   │   └── resource                 # Dependency resources
│   │       ├── moduleA              # Module A
│   │       │   ├── ohos_test.xml    # Resource configuration file
│   │       ... └── 1.txt            # Resource file  
A
Annie_wang 已提交
55
│   │            
A
Annie_wang 已提交
56
│   ├── ohos_build                   # Build entry configuration  
A
Annie_wang 已提交
57 58
│   ...

A
annie_wangli 已提交
59 60
...
```
G
Gloria 已提交
61 62 63
> **NOTE**
>
> Test cases are classified into common test cases and device-specific test cases. You are advised to place common test cases in the **common** directory and device-specific test cases in the directories of the related devices.
A
annie_wangli 已提交
64 65 66 67

###  Writing Test Cases
This test framework supports test cases written in multiple programming languages and provides different templates for different languages.

W
wusongqing 已提交
68
#### C++ Test Case Example
A
annie_wangli 已提交
69 70 71 72

- Naming rules for source files

    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.
G
Gloria 已提交
73 74
    Example:

A
annie_wangli 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    ```
    calculator_sub_test.cpp
    ```

- Test case example
    ```
    
    #include "calculator.h"
    #include <gtest/gtest.h>
    
    using namespace testing::ext;
    
    class CalculatorSubTest : public testing::Test {
    public:
        static void SetUpTestCase(void);
        static void TearDownTestCase(void);
        void SetUp();
        void TearDown();
    };
    
    void CalculatorSubTest::SetUpTestCase(void)
    {
        // Set a setup function, which will be called before all test cases.
    }
    
    void CalculatorSubTest::TearDownTestCase(void)
    {
        // Set a teardown function, which will be called after all test cases.
    }
    
    void CalculatorSubTest::SetUp(void)
    {
A
annie_wangli 已提交
107
        // Set a setup function, which will be called before each test case.
A
annie_wangli 已提交
108 109 110 111
    }
    
    void CalculatorSubTest::TearDown(void)
    {
A
annie_wangli 已提交
112
        // Set a teardown function, which will be called after each test case.
A
annie_wangli 已提交
113 114 115 116
    }
    
    /**
     * @tc.name: integer_sub_001
A
Annie_wang 已提交
117
     * @tc.desc: Verify the sub function.
A
annie_wangli 已提交
118 119 120 121 122 123 124
     * @tc.type: FUNC
     * @tc.require: Issue Number
     */
    HWTEST_F(CalculatorSubTest, integer_sub_001, TestSize.Level1)
    {
        // Step 1 Call the function to obtain the result.
        int actual = Sub(4, 0);
A
Annie_wang 已提交
125
    
A
annie_wangli 已提交
126 127 128 129 130 131
        // Step 2 Use an assertion to compare the obtained result with the expected result.
        EXPECT_EQ(4, actual);
    }
    ```
    The procedure is as follows:
    1. Add comment information to the test case file header.
A
annie_wangli 已提交
132

G
Gloria 已提交
133
        Enter the header comment in the standard format. For details, see [Code Specifications](https://gitee.com/openharmony/docs/blob/master/en/contribute/code-contribution.md).
A
annie_wangli 已提交
134

A
annie_wangli 已提交
135 136 137
    2. Add the test framework header file and namespace.
	    ```
    	#include <gtest/gtest.h>
A
Annie_wang 已提交
138
    	
A
annie_wangli 已提交
139 140
    	using namespace testing::ext;
    	```
G
Gloria 已提交
141 142
    3. Add the header file of the test class.
	    ```
A
annie_wangli 已提交
143 144 145
    	#include "calculator.h"
    	```
    4. Define the test suite (test class).
G
Gloria 已提交
146
	    ```
A
annie_wangli 已提交
147 148 149 150 151 152 153
    	class CalculatorSubTest : public testing::Test {
    	public:
    	    static void SetUpTestCase(void);
    	    static void TearDownTestCase(void);
    	    void SetUp();
    	    void TearDown();
    	};
A
Annie_wang 已提交
154
        
A
annie_wangli 已提交
155 156 157 158
    	void CalculatorSubTest::SetUpTestCase(void)
    	{
    	    // Set a setup function, which will be called before all test cases.
    	}
A
Annie_wang 已提交
159
        
A
annie_wangli 已提交
160 161 162 163
    	void CalculatorSubTest::TearDownTestCase(void)
    	{
    	    // Set a teardown function, which will be called after all test cases.
    	}
A
Annie_wang 已提交
164
        
A
annie_wangli 已提交
165 166
    	void CalculatorSubTest::SetUp(void)
    	{
A
annie_wangli 已提交
167
    	    // Set a setup function, which will be called before each test case.
A
annie_wangli 已提交
168
    	}
A
Annie_wang 已提交
169
        
A
annie_wangli 已提交
170 171
    	void CalculatorSubTest::TearDown(void)
    	{
A
annie_wangli 已提交
172
    	    // Set a teardown function, which will be called after each test case.
G
Gloria 已提交
173 174 175 176 177
    	}
    	```
    	> **NOTE**
    	>
    	> 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.
A
annie_wangli 已提交
178
	
A
Annie_wang 已提交
179
    5. Add implementation of the test cases, including test case comments and logic.
G
Gloria 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	    ```
    	/**
    	 * @tc.name: integer_sub_001
    	 * @tc.desc: Verify the sub function.
    	 * @tc.type: FUNC
    	 * @tc.require: Issue Number
    	 */
    	HWTEST_F(CalculatorSubTest, integer_sub_001, TestSize.Level1)
    	{
    	    // Step 1 Call the function to obtain the test result.
    	    int actual = Sub(4,0);
        
    	    // Step 2 Use an assertion to compare the obtained result with the expected result.
    	    EXPECT_EQ(4, actual);
    	}
    	```
A
Annie_wang 已提交
196 197 198
	
	   The following test case templates are provided for your reference.
	
G
Gloria 已提交
199
       | Template        | Description                                                  |
A
Annie_wang 已提交
200
	   | --------------- | ------------------------------------------------------------ |
G
Gloria 已提交
201
	   | HWTEST(A,B,C)   | Use this template if the test case execution does not depend on setup or teardown. |
A
Annie_wang 已提交
202 203 204
	   | HWTEST_F(A,B,C) | Use this template if the test case execution (excluding parameters) depends on setup and teardown. |
	   | HWTEST_P(A,B,C) | Use this template if the test case execution (including parameters) depends on setup and teardown. |
	
G
Gloria 已提交
205
       In the template names:
A
Annie_wang 已提交
206 207
	
       - *A* indicates the test suite name.
G
Gloria 已提交
208
       
A
Annie_wang 已提交
209 210 211 212 213 214 215
       - *B* indicates the test case name, which is in the *Function*\_*No.* format. The *No.* is a three-digit number starting from **001**.
    
       - *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.
       
       > **NOTE**
       >
       > - The expected result of each test case must have an assertion.
G
Gloria 已提交
216
       > - The test case level must be specified.
A
Annie_wang 已提交
217
       > - It is recommended that the test be implemented step by step according to the template.
G
Gloria 已提交
218 219
       > - The comment must contain the test case name, description, type, and requirement number, which are in the @tc.*xxx*: *value* format. The test case description must be in the @tc.xxx format. The test case type @tc.type can be any of the following:
      
A
Annie_wang 已提交
220
       | Test Case Type   | Code |
G
Gloria 已提交
221
    | ---------------- | ---- |
A
Annie_wang 已提交
222 223 224 225 226
       | Function test    | FUNC |
       | Performance test | PERF |
       | Reliability test | RELI |
       | Security test    | SECU |
       | Fuzz test        | FUZZ |
A
annie_wangli 已提交
227

W
wusongqing 已提交
228
#### JavaScript Test Case Example
A
annie_wangli 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

- Naming rules for source files

    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.
Example:
    ```
    AppInfoTest.js
    ```

- Test case example
    ```
    import app from '@system.app'
    
    import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
    
    describe("AppInfoTest", function () {
        beforeAll(function() {
            // Set a setup function, which will be called before all test cases.
L
liuhui 已提交
247
             console.info('beforeAll called')
A
annie_wangli 已提交
248 249 250 251
        })
        
        afterAll(function() {
             // Set a teardown function, which will be called after all test cases.
L
liuhui 已提交
252
             console.info('afterAll called')
A
annie_wangli 已提交
253 254 255
        })
        
        beforeEach(function() {
A
annie_wangli 已提交
256
            // Set a setup function, which will be called before each test case.
L
liuhui 已提交
257
             console.info('beforeEach called')
A
annie_wangli 已提交
258 259 260
        })
        
        afterEach(function() {
A
annie_wangli 已提交
261
            // Set a teardown function, which will be called after each test case.
L
liuhui 已提交
262
             console.info('afterEach called')
A
annie_wangli 已提交
263 264 265 266 267 268 269 270 271 272 273
        })
    
        /*
         * @tc.name:appInfoTest001
         * @tc.desc:verify app info is not null
         * @tc.type: FUNC
         * @tc.require: Issue Number
         */
        it("appInfoTest001", 0, function () {
            // Step 1 Call the function to obtain the test result.
            var info = app.getInfo()
A
Annie_wang 已提交
274
    
A
annie_wangli 已提交
275 276 277 278 279 280 281
            // Step 2 Use an assertion to compare the obtained result with the expected result.
            expect(info != null).assertEqual(true)
        })
    })
    ```
    The procedure is as follows:
    1. Add comment information to the test case file header.
A
annie_wangli 已提交
282

A
Annie_wang 已提交
283
        Enter the header comment in the standard format. For details, see [Code Specifications] (https://gitee.com/openharmony/docs/blob/master/en/contribute/code-contribution.md).
A
annie_wangli 已提交
284

A
annie_wangli 已提交
285 286 287
    2. Import the APIs and JSUnit test library to test.
	    ```
    	import app from '@system.app'
A
Annie_wang 已提交
288
    	
A
annie_wangli 已提交
289 290 291 292 293 294 295
    	import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
    	```
    3. Define the test suite (test class).
	    ```
    	describe("AppInfoTest", function () {
    	    beforeAll(function() {
    	        // Set a setup function, which will be called before all test cases.
L
liuhui 已提交
296
    	         console.info('beforeAll called')
A
annie_wangli 已提交
297 298 299 300
    	    })
    	    
    	    afterAll(function() {
    	         // Set a teardown function, which will be called after all test cases.
L
liuhui 已提交
301
    	         console.info('afterAll called')
A
annie_wangli 已提交
302 303 304
    	    })
    	    
    	    beforeEach(function() {
A
annie_wangli 已提交
305
    	        // Set a setup function, which will be called before each test case.
L
liuhui 已提交
306
    	         console.info('beforeEach called')
A
annie_wangli 已提交
307 308 309
    	    })
    	    
    	    afterEach(function() {
A
annie_wangli 已提交
310
    	        // Set a teardown function, which will be called after each test case.
L
liuhui 已提交
311
    	         console.info('afterEach called')
A
annie_wangli 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324
    	    })
    	```
    4. Add implementation of the test cases.
	    ```
    	/*
    	 * @tc.name:appInfoTest001
    	 * @tc.desc:verify app info is not null
    	 * @tc.type: FUNC
    	 * @tc.require: Issue Number
    	 */
    	 it("appInfoTest001", 0, function () {
    	    // Step 1 Call the function to obtain the test result.
            var info = app.getInfo()
A
Annie_wang 已提交
325
    	
A
annie_wangli 已提交
326 327 328 329 330 331 332 333 334 335 336 337
            // Step 2 Use an assertion to compare the obtained result with the expected result.
            expect(info != null).assertEqual(true)
    	 })
    	```

### Writing the Build File for Test Cases
When 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.

#### Writing Build Files for Test Cases
The following provides templates for different languages for your reference.

- **Test case build file example (C++)**
G
Gloria 已提交
338
    ```c++
A
annie_wangli 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
    
    import("//build/test.gni")
    
    module_output_path = "subsystem_examples/calculator"
    
    config("module_private_config") {
      visibility = [ ":*" ]
    
      include_dirs = [ "../../../include" ]
    }
    
    ohos_unittest("CalculatorSubTest") {
      module_out_path = module_output_path
    
      sources = [
        "../../../include/calculator.h",
        "../../../src/calculator.cpp",
      ]
    
      sources += [ "calculator_sub_test.cpp" ]
    
      configs = [ ":module_private_config" ]
    
      deps = [ "//third_party/googletest:gtest_main" ]
    }
    
    group("unittest") {
      testonly = true
      deps = [":CalculatorSubTest"]
    }
    ```
A
annie_wangli 已提交
370
    The procedure is as follows:
A
annie_wangli 已提交
371 372

    1. Add comment information for the file header.
A
annie_wangli 已提交
373

A
Annie_wang 已提交
374
        Enter the header comment in the standard format. For details, see [Code Specifications] (https://gitee.com/openharmony/docs/blob/master/en/contribute/code-contribution.md).
A
annie_wangli 已提交
375

A
annie_wangli 已提交
376 377 378 379
    2. Import the build template.
	    ```
    	import("//build/test.gni")
    	```
A
Annie_wang 已提交
380
    	
A
annie_wangli 已提交
381 382 383 384
    3. Specify the file output path.
    	```
    	module_output_path = "subsystem_examples/calculator"
    	```
G
Gloria 已提交
385 386 387 388
    	> **NOTE**
    	>
    	> The output path is ***Part name*/*Module name***.
    	
A
annie_wangli 已提交
389 390 391 392 393 394 395 396 397
    4. Configure the directories for dependencies.
    
    	```
    	config("module_private_config") {
    	  visibility = [ ":*" ]
    	   
    	  include_dirs = [ "../../../include" ]
    	}
    	```
G
Gloria 已提交
398 399 400
    	> **NOTE**
    	>
    	> Generally, the dependency directories are configured here and directly referenced in the build script of the test case.
A
annie_wangli 已提交
401 402 403 404 405 406 407
    
    5. Set the output build file for the test cases.
    
    	```
    	ohos_unittest("CalculatorSubTest") {
    	}
    	```
A
Annie_wang 已提交
408
    	
A
annie_wangli 已提交
409
    6. Write the build script (add the source file, configuration, and dependencies) for the test cases.
A
Annie_wang 已提交
410
    	
A
annie_wangli 已提交
411 412 413 414 415 416 417 418 419 420
    	```
    	ohos_unittest("CalculatorSubTest") {
    	  module_out_path = module_output_path
    	  sources = [
    	    "../../../include/calculator.h",
    	    "../../../src/calculator.cpp",
    	    "../../../test/calculator_sub_test.cpp"
    	  ]
    	  sources += [ "calculator_sub_test.cpp" ]
    	  configs = [ ":module_private_config" ]
G
Gloria 已提交
421 422
    	  deps = [ "//third_party/googletest:gtest_main" ]
    	}
A
annie_wangli 已提交
423
    	```
A
Annie_wang 已提交
424
    	
A
Annie_wang 已提交
425 426 427 428 429 430 431
    	> **NOTE**
    	>
    	> - **ohos_unittest**: unit test
    	> - **ohos_moduletest**: module test
    	> - **ohos_systemtest**: system test
    	> - **ohos_performancetest**: performance test
    	> - **ohos_securitytest**: security test
G
Gloria 已提交
432
    	> - **ohos_reliabilitytest**: reliability test
A
Annie_wang 已提交
433
    	> - **ohos_distributedtest**: distributed test
G
Gloria 已提交
434
    
A
annie_wangli 已提交
435
    7. Group the test case files by test type.
G
Gloria 已提交
436 437 438 439 440 441 442
    
    	```
    	group("unittest") {
    	  testonly = true
    	  deps = [":CalculatorSubTest"]
    	}
    	```
A
Annie_wang 已提交
443 444 445 446 447
       
       > **NOTE**
       >
       > Grouping test cses by test type allows you to execute a specific type of test cases when required.
    
G
Gloria 已提交
448
- **Test case build file example (JavaScript)**
A
annie_wangli 已提交
449

G
Gloria 已提交
450
    ```javascript
A
annie_wangli 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
    
    import("//build/test.gni")
    
    module_output_path = "subsystem_examples/app_info"
    
    ohos_js_unittest("GetAppInfoJsTest") {
      module_out_path = module_output_path
    
      hap_profile = "./config.json"
      certificate_profile = "//test/developertest/signature/openharmony_sx.p7b"
    }
    
    group("unittest") {
      testonly = true
      deps = [ ":GetAppInfoJsTest" ]
    }
    ```

    The procedure is as follows:

    1. Add comment information for the file header.
A
annie_wangli 已提交
472

A
Annie_wang 已提交
473
        Enter the header comment in the standard format. For details, see [Code Specifications] (https://gitee.com/openharmony/docs/blob/master/en/contribute/code-contribution.md).
A
annie_wangli 已提交
474

A
annie_wangli 已提交
475 476 477 478 479 480 481 482 483 484
    2. Import the build template.
    
    	```
    	import("//build/test.gni")
    	```
    3. Specify the file output path.
    
    	```
    	module_output_path = "subsystem_examples/app_info"
    	```
G
Gloria 已提交
485 486 487
    	> **NOTE**
    	>
    	> The output path is ***Part name*/*Module name***.
A
annie_wangli 已提交
488 489 490 491 492 493 494
    	
    4. Set the output build file for the test cases.
    
    	```
    	ohos_js_unittest("GetAppInfoJsTest") {
    	}
    	```
G
Gloria 已提交
495 496 497 498
    	> **NOTE**
    	>
    	> - Use the **ohos\_js\_unittest** template to define the JavaScript test suite. Pay attention to the difference between JavaScript and C++.
    	> - 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**.
A
annie_wangli 已提交
499 500 501 502 503 504 505 506 507 508 509
    
    5. Configure the **config.json** file and signature file, which are mandatory.
    
    	```
    	ohos_js_unittest("GetAppInfoJsTest") {
    	  module_out_path = module_output_path
    	   
    	  hap_profile = "./config.json"
    	  certificate_profile = "//test/developertest/signature/openharmony_sx.p7b"
    	}
    	```
A
Annie_wang 已提交
510
    	**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:
A
annie_wangli 已提交
511 512 513 514 515 516 517 518 519 520 521
    
    	```
    	{
    	  "app": {
    	    "bundleName": "com.example.myapplication",
    	    "vendor": "example",
    	    "version": {
    	      "code": 1,
    	      "name": "1.0"
    	    },
    	    "apiVersion": {
A
Annie_wang 已提交
522 523
    	      "compatible": 4,
    	      "target": 5 // Set it based on the tested SDK version. In this example, SDK5 is used.
A
annie_wangli 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
    	    }
    	  },
    	  "deviceConfig": {},
    	  "module": {
    	    "package": "com.example.myapplication",
    	    "name": ".MyApplication",
    	    "deviceType": [
    	      "phone"
    	    ],
    	    "distro": {
    	      "deliveryWithInstall": true,
    	      "moduleName": "entry",
    	      "moduleType": "entry"
    	    },
    	    "abilities": [
    	      {
    	      "skills": [
    	          {
    	            "entities": [
    	              "entity.system.home"
    	            ],
    	            "actions": [
    	              "action.system.home"
    	            ]
    	          }
    	        ],
    	        "name": "com.example.myapplication.MainAbility",
    	        "icon": "$media:icon",
    	        "description": "$string:mainability_description",
    	        "label": "MyApplication",
    	        "type": "page",
    	        "launchType": "standard"
    	      }
    	    ],
    	    "js": [
    	      {
    	        "pages": [
    	          "pages/index/index"
    	        ],
    	        "name": "default",
    	          "window": {
    	             "designWidth": 720,
    	             "autoDesignWidth": false
    	          }
    	        }
    	      ]
    	    }
    	  }
    	```
    6. Group the test case files by test type.
    	```
    	group("unittest") {
    	  testonly = true
    	  deps = [ ":GetAppInfoJsTest" ]
    	}
    	```
G
Gloria 已提交
580 581 582
    	> **NOTE**
    	>
    	> Grouping test cases by test type allows you to execute a specific type of test cases when required.
A
annie_wangli 已提交
583
    
A
annie_wangli 已提交
584 585
#### Configuring ohos.build

A
annie_wangli 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
Configure the part build file to associate with specific test cases.
```
"partA": {
    "module_list": [
          
    ],
    "inner_list": [
          
    ],
    "system_kits": [
          
    ],
    "test_list": [
      "//system/subsystem/partA/calculator/test:unittest"  // Configure test under calculator.
    ]
 }
```
G
Gloria 已提交
603 604 605
> **NOTE**
>
> **test_list** contains the test cases of the corresponding module.
A
annie_wangli 已提交
606 607 608 609 610

### Configuring Test Case Resources
Test case resources include external file resources, such as image files, video files, and third-party libraries, required for test case execution.

Perform the following steps:
A
Annie_wang 已提交
611
1. 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.
A
annie_wangli 已提交
612

A
Annie_wang 已提交
613
2. In the module directory under **resource**, create the **ohos_test.xml** file in the following format:
A
annie_wangli 已提交
614 615 616 617 618 619 620 621 622 623 624 625 626 627
	```
	<?xml version="1.0" encoding="UTF-8"?>
	<configuration ver="2.0">
	    <target name="CalculatorSubTest">
	        <preparer>
	            <option name="push" value="test.jpg -> /data/test/resource" src="res"/>
	            <option name="push" value="libc++.z.so -> /data/test/resource" src="out"/>
	        </preparer>
	    </target>
	</configuration>
	```
3. In the build file of the test cases, configure **resource\_config\_file** to point to the resource file **ohos\_test.xml**.
	```
	ohos_unittest("CalculatorSubTest") {
A
Annie_wang 已提交
628
	  resource_config_file = "//system/subsystem/partA/test/resource/calculator/ohos_test.xml"
A
annie_wangli 已提交
629 630
	}
	```
G
Gloria 已提交
631 632
	>**NOTE**
	>
A
annie_wangli 已提交
633 634
	>- **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.
A
annie_wangli 已提交
635 636 637
	>- **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.

A
annie_wangli 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
## Executing Test Cases
Before executing test cases, you need to modify the configuration based on the device used.

### Modifying user_config.xml
```
<user_config>
  <build>
    <!-- Whether to build a demo case. The default value is false. If a demo case is required, change the value to true. -->
    <example>false</example>
    <!-- Whether to build the version. The default value is false. -->
    <version>false</version>
    <!-- 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.-->
    <testcase>true</testcase>
  </build>
  <environment>
S
stivn 已提交
653
    <!-- Configure the IP address and port number of the remote server to support connection to the device through the OpenHarmony Device Connector (HDC).-->
A
annie_wangli 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
    <device type="usb-hdc">
      <ip></ip>
      <port></port>
      <sn></sn>
    </device>
    <!-- Configure the serial port information of the device to enable connection through the serial port.-->
    <device type="com" label="ipcamera">
      <serial>
        <com></com>
        <type>cmd</type>
        <baud_rate>115200</baud_rate>
        <data_bits>8</data_bits>
        <stop_bits>1</stop_bits>
        <timeout>1</timeout>
      </serial>
    </device>
  </environment>
  <!-- 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.-->
  <test_cases>
    <dir></dir>
  </test_cases>
  <!-- Configure the coverage output path.-->
  <coverage>
    <outpath></outpath>
  </coverage>
  <!-- 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. -->
  <NFS>
    <host_dir></host_dir>
    <mnt_cmd></mnt_cmd>
    <board_dir></board_dir>
  </NFS>
</user_config>
```
G
Gloria 已提交
687 688 689
>**NOTE**
>
>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.
A
annie_wangli 已提交
690 691 692 693 694 695

### Executing Test Cases on Windows
#### Building Test Cases

Test cases cannot be built on Windows. You need to run the following command to build test cases on Linux:
```
L
libing3@huawei.com 已提交
696
./build.sh --product-name hispark_taurus_standard --build-target make_test
A
annie_wangli 已提交
697
```
L
libing3@huawei.com 已提交
698
When the build is complete, the test cases are automatically saved in **out/hispark_taurus/packages/phone/images/tests**.
A
annie_wangli 已提交
699

G
Gloria 已提交
700 701 702 703 704 705
>**NOTE**
>
>In the command, **hispark_taurus_standard** indicates the product supported by the current version, and **make_test** indicates all test cases. You can set the build options based on requirements:<br>
>
>-  --**product-name**: specifies the name of the product to build. It is mandatory.
>- --**build-target**: specifies the target to build. It is optional. 
A
annie_wangli 已提交
706 707 708 709 710

#### Setting Up the Execution Environment
1. On Windows, create the **Test** directory in the test framework and then create the **testcase** directory in the **Test** directory.

2. Copy **developertest** and **xdevice** from the Linux environment to the **Test** directory on Windows, and copy the test cases to the **testcase** directory.
A
annie_wangli 已提交
711
	
G
Gloria 已提交
712
	> **NOTE**
A
Annie_wang 已提交
713 714
	>
	> Port the test framework and test cases from the Linux environment to the Windows environment for subsequent execution.
A
annie_wangli 已提交
715 716 717
3. Modify the **user_config.xml** file.
	```
	<build>
A
Annie_wang 已提交
718
	  <!-- Because the test cases have been built, change the value to false. -->
A
annie_wangli 已提交
719 720 721
	  <testcase>false</testcase>
	</build>
	<test_cases>
A
annie_wangli 已提交
722
	  <!-- 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.-->
A
annie_wangli 已提交
723 724 725
	  <dir>D:\Test\testcase\tests</dir>
	</test_cases>
	```
G
Gloria 已提交
726 727 728
	>**NOTE**
	>
	>`<testcase>` indicates whether to build test cases. `<dir>` indicates the path for searching for test cases.
A
annie_wangli 已提交
729 730 731 732 733 734 735 736

#### Executing Test Cases
1. Start the test framework.
	```
	start.bat
	```
2. Select the product.

A
Annie_wang 已提交
737
    After the test framework starts, you are asked to select a product. Select the development board to test, for example, **Hi3516DV300**.
A
annie_wangli 已提交
738 739 740 741 742

3. Execute test cases.

    Run the following command to execute test cases:
	```
JoyboyCZ's avatar
JoyboyCZ 已提交
743
	run -t UT -ts CalculatorSubTest -tc integer_sub_00l
A
annie_wangli 已提交
744 745 746
	```
	In the command:
	```
A
annie_wangli 已提交
747 748 749 750 751 752
	-t [TESTTYPE]: specifies the test type, which can be UT, MST, ST, or PERF. This parameter is mandatory.
	-tp [TESTPART]: specifies the part to test. This parameter can be used independently.
	-tm [TESTMODULE]: specifies the module to test. This parameter must be used together with -tp.
	-ts [TESTSUITE]: specifies a test suite. This parameter can be used independently.
	-tc [TESTCASE]: specifies a test case. This parameter must be used together with -ts.
	You can run -h to display help information.
A
annie_wangli 已提交
753 754
	```
### Executing Test Cases on Linux
A
annie_wangli 已提交
755
#### Mapping the Remote Port
A
Annie_wang 已提交
756
To 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:
A
annie_wangli 已提交
757 758 759 760 761
1. On the HDC server, run the following commands:
	```
	hdc_std kill
	hdc_std -m -s 0.0.0.0:8710
	```
G
Gloria 已提交
762 763 764
	>**NOTE**
	>
	>The IP address and port number are default values.
A
annie_wangli 已提交
765 766 767 768 769

2. On the HDC client, run the following command:
	```
	hdc_std -s xx.xx.xx.xx:8710 list targets
	```
G
Gloria 已提交
770 771 772
	>**NOTE**
	>
	>Enter the IP address of the device to test.
A
annie_wangli 已提交
773 774 775 776 777 778 779 780

#### Executing Test Cases
1. Start the test framework.
	```
	./start.sh
	```
2. Select the product.

A
Annie_wang 已提交
781
    After the test framework starts, you are asked to select a product. Select the development board to test, for example, **Hi3516DV300**.
A
annie_wangli 已提交
782 783 784 785 786

3. Execute test cases.

    The test framework locates the test cases based on the command, and automatically builds and executes the test cases.
	```
A
Annie_wang 已提交
787
	run -t UT -ts CalculatorSubTest -tc interger_sub_00l
A
annie_wangli 已提交
788 789 790
	```
	In the command:
	```
A
annie_wangli 已提交
791 792 793 794 795 796
	-t [TESTTYPE]: specifies the test type, which can be UT, MST, ST, or PERF. This parameter is mandatory.
	-tp [TESTPART]: specifies the part to test. This parameter can be used independently.
	-tm [TESTMODULE]: specifies the module to test. This parameter must be used together with -tp.
	-ts [TESTSUITE]: specifies a test suite. This parameter can be used independently.
	-tc [TESTCASE]: specifies a test case. This parameter must be used together with -ts.
	You can run -h to display help information.
A
annie_wangli 已提交
797 798 799 800 801 802 803 804 805 806
	```

## Viewing the Test Report
After the test cases are executed, the test result will be automatically generated. You can view the detailed test result in the related directory.

### Test Result
You can obtain the test result in the following directory:
```
test/developertest/reports/xxxx_xx_xx_xx_xx_xx
```
G
Gloria 已提交
807 808 809
>**NOTE**
>
>The folder for test reports is automatically generated.
A
annie_wangli 已提交
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831

The folder contains the following files:
| Type| Description|
| ------------ | ------------ |
| result/ |Test cases in standard format|
| log/plan_log_xxxx_xx_xx_xx_xx_xx.log | Test case logs|
| summary_report.html | Test report summary|
| details_report.html | Detailed test report|

### Test Framework Logs
```
reports/platform_log_xxxx_xx_xx_xx_xx_xx.log
```

### Latest Test Report
```
reports/latest
```

## Repositories Involved

[test\_xdevice](https://gitee.com/openharmony/test_xdevice/blob/master/README.md)