未验证 提交 0a4ada0c 编写于 作者: O openharmony_ci 提交者: Gitee

!5578 [翻译完成】#I5BL82

Merge pull request !5578 from Annie_wang/PR2725
# Test Subsystem # Test
OpenHarmony provides a comprehensive auto-test framework for designing test cases. Detecting defects in the development process can improve code quality. 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. This document describes how to use the OpenHarmony test framework.
...@@ -12,53 +12,53 @@ The test framework depends on the Python running environment. Before using the t ...@@ -12,53 +12,53 @@ The test framework depends on the Python running environment. Before using the t
The directory structure of the test framework is as follows: The directory structure of the test framework is as follows:
``` ```
test # Test subsystem test # Test subsystem
├── developertest # Developer test module ├── developertest # Developer test module
│ ├── aw # Static library of the test framework │ ├── aw # Static library of the test framework
│ ├── config # Test framework configuration │ ├── config # Test framework configuration
│ │ │ ... │ │ │ ...
│ │ └── user_config.xml # User configuration │ │ └── user_config.xml # User configuration
│ ├── examples # Examples of test cases │ ├── examples # Examples of test cases
│ ├── src # Source code of the test framework │ ├── src # Source code of the test framework
│ ├── third_party # Adaptation code for third-party components on which the test framework depends │ ├── third_party # Adaptation code for third-party components on which the test framework depends
│ ├── reports # Test reports │ ├── reports # Test reports
│ ├── BUILD.gn # Build entry of the test framework │ ├── BUILD.gn # Build entry of the test framework
│ ├── start.bat # Test entry for Windows │ ├── start.bat # Test entry for Windows
│ └── start.sh # Test entry for Linux │ └── start.sh # Test entry for Linux
└── xdevice # Modules on which the test framework depends └── xdevice # Modules on which the test framework depends
``` ```
## Writing Test Cases ## Writing Test Cases
### Designing the Test Case Directory ### Designing the Test Case Directory
Design the test case directory as follows: Design the test case directory as follows:
``` ```
subsystem # Subsystem subsystem # Subsystem
├── partA # Part A ├── partA # Part A
│ ├── moduleA # Module A │ ├── moduleA # Module A
│ │ ├── include │ │ ├── include
│ │ ├── src # Service code │ │ ├── src # Service code
│ │ └── test # Test directory │ │ └── test # Test directory
│ │ ├── unittest # Unit test │ │ ├── unittest # Unit test
│ │ │ ├── common # Common test cases │ │ │ ├── common # Common test cases
│ │ │ │ ├── BUILD.gn # Build file of test cases │ │ │ │ ├── BUILD.gn # Build file of test cases
│ │ │ │ └── testA_test.cpp # Source code of unit test cases │ │ │ │ └── testA_test.cpp # Source code of unit test cases
│ │ │ ├── phone # Test cases for mobile phones │ │ │ ├── phone # Test cases for mobile phones
│ │ │ ├── ivi # Test cases for head units │ │ │ ├── ivi # Test cases for head units
│ │ │ └── liteos-a # Test cases for the IP cameras that use the LiteOS kernel │ │ │ └── liteos-a # Test cases for the IP cameras that use the LiteOS kernel
│ │ ├── moduletest # Module test │ │ ├── moduletest # Module test
│ │ ... │ │ ...
│ │ │ │
│ ├── moduleB # Module B │ ├── moduleB # Module B
│ ├── test │ ├── test
│ │ └── resource # Dependency resources │ │ └── resource # Dependency resources
│ │ ├── moduleA # Module A │ │ ├── moduleA # Module A
│ │ │ ├── ohos_test.xml # Resource configuration file │ │ │ ├── ohos_test.xml # Resource configuration file
│ │ ... └── 1.txt # Resource │ │ ... └── 1.txt # Resource file
│ │ │ │
│ ├── ohos_build # Build entry configuration │ ├── ohos_build # Build entry configuration
│ ... │ ...
... ...
``` ```
> **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. > **NOTE**<br>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.
### Writing Test Cases ### Writing Test Cases
This test framework supports test cases written in multiple programming languages and provides different templates for different languages. This test framework supports test cases written in multiple programming languages and provides different templates for different languages.
...@@ -75,20 +75,6 @@ Example: ...@@ -75,20 +75,6 @@ Example:
- Test case example - Test case example
``` ```
/*
* Copyright (c) 2021 XXXX Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "calculator.h" #include "calculator.h"
#include <gtest/gtest.h> #include <gtest/gtest.h>
...@@ -140,22 +126,9 @@ Example: ...@@ -140,22 +126,9 @@ Example:
``` ```
The procedure is as follows: The procedure is as follows:
1. Add comment information to the test case file header. 1. Add comment information to the test case file header.
```
/* 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).
* Copyright (c) 2021 XXXX Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
```
2. Add the test framework header file and namespace. 2. Add the test framework header file and namespace.
``` ```
#include <gtest/gtest.h> #include <gtest/gtest.h>
...@@ -196,7 +169,7 @@ Example: ...@@ -196,7 +169,7 @@ Example:
// Set a teardown function, which will be called after each test case. // Set a teardown function, which will be called after each test case.
} }
``` ```
> **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. > **NOTE**<br>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.
5. Add implementation of the test cases, including test case comments and logic. 5. Add implementation of the test cases, including test case comments and logic.
``` ```
...@@ -228,19 +201,19 @@ Example: ...@@ -228,19 +201,19 @@ Example:
- *B* indicates the test case name, which is in the *Function*\_*No.* format. The *No.* is a three-digit number starting from **001**. - *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. - *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**: **NOTE**<br>
- The expected result of each test case must have an assertion. - The expected result of each test case must have an assertion.
- The test case level must be specified. - The test case level must be specified.
- It is recommended that the test be implemented step by step according to the template. - It is recommended that the test be implemented step by step according to the template.
- 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: - 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:
| Test Case Type|Code| | Test Case Type|Code|
| ------------|------------| | ------------|------------|
|Function test |FUNC| |Function test |FUNC|
|Performance test |PERF| |Performance test |PERF|
|Reliability test |RELI| |Reliability test |RELI|
|Security test |SECU| |Security test |SECU|
|Fuzz test |FUZZ| |Fuzz test |FUZZ|
#### JavaScript Test Case Example #### JavaScript Test Case Example
...@@ -255,20 +228,6 @@ Example: ...@@ -255,20 +228,6 @@ Example:
- Test case example - Test case example
``` ```
/*
* Copyright (C) 2021 XXXX Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import app from '@system.app' import app from '@system.app'
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
...@@ -311,22 +270,9 @@ Example: ...@@ -311,22 +270,9 @@ Example:
``` ```
The procedure is as follows: The procedure is as follows:
1. Add comment information to the test case file header. 1. Add comment information to the test case file header.
```
/* 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).
* Copyright (C) 2021 XXXX Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
```
2. Import the APIs and JSUnit test library to test. 2. Import the APIs and JSUnit test library to test.
``` ```
import app from '@system.app' import app from '@system.app'
...@@ -381,7 +327,6 @@ The following provides templates for different languages for your reference. ...@@ -381,7 +327,6 @@ The following provides templates for different languages for your reference.
- **Test case build file example (C++)** - **Test case build file example (C++)**
``` ```
# Copyright (c) 2021 XXXX Device Co., Ltd.
import("//build/test.gni") import("//build/test.gni")
...@@ -413,12 +358,12 @@ The following provides templates for different languages for your reference. ...@@ -413,12 +358,12 @@ The following provides templates for different languages for your reference.
deps = [":CalculatorSubTest"] deps = [":CalculatorSubTest"]
} }
``` ```
The build file is configured as follows: The procedure is as follows:
1. Add comment information for the file header. 1. Add comment information for the file header.
```
# Copyright (c) 2021 XXXX Device Co., Ltd. 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).
```
2. Import the build template. 2. Import the build template.
``` ```
import("//build/test.gni") import("//build/test.gni")
...@@ -427,7 +372,7 @@ The following provides templates for different languages for your reference. ...@@ -427,7 +372,7 @@ The following provides templates for different languages for your reference.
``` ```
module_output_path = "subsystem_examples/calculator" module_output_path = "subsystem_examples/calculator"
``` ```
> **Note**: The output path is ***Part name*/*Module name***. > **NOTE**<br>The output path is ***Part name*/*Module name***.
4. Configure the directories for dependencies. 4. Configure the directories for dependencies.
...@@ -438,7 +383,7 @@ The following provides templates for different languages for your reference. ...@@ -438,7 +383,7 @@ The following provides templates for different languages for your reference.
include_dirs = [ "../../../include" ] include_dirs = [ "../../../include" ]
} }
``` ```
> **Note**: Generally, the dependency directories are configured here and directly referenced in the build script of the test case. > **NOTE**<br>Generally, the dependency directories are configured here and directly referenced in the build script of the test case.
5. Set the output build file for the test cases. 5. Set the output build file for the test cases.
...@@ -461,7 +406,7 @@ The following provides templates for different languages for your reference. ...@@ -461,7 +406,7 @@ The following provides templates for different languages for your reference.
} }
``` ```
> **Note:** Set the test type based on actual requirements. The following test types are available: > **NOTE**<br>Set the test type based on actual requirements. The following test types are available:
> - **ohos_unittest**: unit test > - **ohos_unittest**: unit test
> - **ohos_moduletest**: module test > - **ohos_moduletest**: module test
> - **ohos_systemtest**: system test > - **ohos_systemtest**: system test
...@@ -478,12 +423,11 @@ The following provides templates for different languages for your reference. ...@@ -478,12 +423,11 @@ The following provides templates for different languages for your reference.
deps = [":CalculatorSubTest"] deps = [":CalculatorSubTest"]
} }
``` ```
> **Note**: Grouping test cases by test type allows you to execute a specific type of test cases when required. > **NOTE**<br>Grouping test cases by test type allows you to execute a specific type of test cases when required.
- **Test case build file example (JavaScript)** - **Test case build file example (JavaScript)**
``` ```
# Copyright (C) 2021 XXXX Device Co., Ltd.
import("//build/test.gni") import("//build/test.gni")
...@@ -505,10 +449,9 @@ The following provides templates for different languages for your reference. ...@@ -505,10 +449,9 @@ The following provides templates for different languages for your reference.
The procedure is as follows: The procedure is as follows:
1. Add comment information for the file header. 1. Add comment information for the file header.
``` 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).
# Copyright (C) 2021 XXXX Device Co., Ltd.
```
2. Import the build template. 2. Import the build template.
``` ```
...@@ -519,7 +462,7 @@ The following provides templates for different languages for your reference. ...@@ -519,7 +462,7 @@ The following provides templates for different languages for your reference.
``` ```
module_output_path = "subsystem_examples/app_info" module_output_path = "subsystem_examples/app_info"
``` ```
> **Note**: The output path is ***Part name*/*Module name***. > **NOTE**<br>The output path is in the ***Part name*/*Module name*** format.
4. Set the output build file for the test cases. 4. Set the output build file for the test cases.
...@@ -527,7 +470,7 @@ The following provides templates for different languages for your reference. ...@@ -527,7 +470,7 @@ The following provides templates for different languages for your reference.
ohos_js_unittest("GetAppInfoJsTest") { ohos_js_unittest("GetAppInfoJsTest") {
} }
``` ```
> **Note:** > **NOTE**<br>
>- Use the **ohos\_js\_unittest** template to define the JavaScript test suite. Pay attention to the difference between JavaScript and C++. >- 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**. >- 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**.
...@@ -611,10 +554,10 @@ The following provides templates for different languages for your reference. ...@@ -611,10 +554,10 @@ The following provides templates for different languages for your reference.
deps = [ ":GetAppInfoJsTest" ] deps = [ ":GetAppInfoJsTest" ]
} }
``` ```
> **Note**: Grouping test cases by test type allows you to execute a specific type of test cases when required. > **NOTE**<br>Grouping test cases by test type allows you to execute a specific type of test cases when required.
#### Configuring ohos.build
#### Configuring ohos.build
Configure the part build file to associate with specific test cases. Configure the part build file to associate with specific test cases.
``` ```
"partA": { "partA": {
...@@ -632,7 +575,7 @@ Configure the part build file to associate with specific test cases. ...@@ -632,7 +575,7 @@ Configure the part build file to associate with specific test cases.
] ]
} }
``` ```
> **Note**: **test_list** contains the test cases of the corresponding module. > **NOTE**<br>**test_list** contains the test cases of the corresponding module.
### Configuring Test Case Resources ### 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. Test case resources include external file resources, such as image files, video files, and third-party libraries, required for test case execution.
...@@ -712,7 +655,7 @@ Before executing test cases, you need to modify the configuration based on the d ...@@ -712,7 +655,7 @@ Before executing test cases, you need to modify the configuration based on the d
</NFS> </NFS>
</user_config> </user_config>
``` ```
>**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. > **NOTE**<br>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.
### Executing Test Cases on Windows ### Executing Test Cases on Windows
#### Building Test Cases #### Building Test Cases
...@@ -721,9 +664,9 @@ Test cases cannot be built on Windows. You need to run the following command to ...@@ -721,9 +664,9 @@ Test cases cannot be built on Windows. You need to run the following command to
``` ```
./build.sh --product-name Hi3516DV300 --build-target make_test ./build.sh --product-name Hi3516DV300 --build-target make_test
``` ```
When the build is complete, the test cases are automatically saved in the **out/hi3516dv300/packages/phone/images/tests** directory. When the build is complete, the test cases are automatically saved in **out/hi3516dv300/packages/phone/images/tests**.
>**Note:** In the command, **Hi3516DV300** is the platform supported by the current version, and **make_test** indicates all test cases. You can set the build options based on requirements: >**NOTE**<br>In the command, **Hi3516DV300** is the platform supported by the current version, and **make_test** indicates all test cases. You can set the build options based on requirements:
> - --**product-name**: specifies the name of the product to build. It is mandatory. > - --**product-name**: specifies the name of the product to build. It is mandatory.
> - --**build-target**: specifies the target to build. It is optional. > - --**build-target**: specifies the target to build. It is optional.
...@@ -731,7 +674,8 @@ When the build is complete, the test cases are automatically saved in the **out/ ...@@ -731,7 +674,8 @@ When the build is complete, the test cases are automatically saved in the **out/
1. On Windows, create the **Test** directory in the test framework and then create the **testcase** directory in the **Test** directory. 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. 2. Copy **developertest** and **xdevice** from the Linux environment to the **Test** directory on Windows, and copy the test cases to the **testcase** directory.
>**Note**: Port the test framework and test cases from the Linux environment to the Windows environment for subsequent execution.
> **NOTE**<br>Port the test framework and test cases from the Linux environment to the Windows environment for subsequent execution.
3. Modify the **user_config.xml** file. 3. Modify the **user_config.xml** file.
``` ```
...@@ -740,11 +684,11 @@ When the build is complete, the test cases are automatically saved in the **out/ ...@@ -740,11 +684,11 @@ When the build is complete, the test cases are automatically saved in the **out/
<testcase>false</testcase> <testcase>false</testcase>
</build> </build>
<test_cases> <test_cases>
<!-- 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. --> <!-- 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.-->
<dir>D:\Test\testcase\tests</dir> <dir>D:\Test\testcase\tests</dir>
</test_cases> </test_cases>
``` ```
>**Note**: `<testcase>` indicates whether to build test cases. `<dir>` indicates the path for searching for test cases. > **NOTE**<br>`<testcase>` indicates whether to build test cases. `<dir>` indicates the path for searching for test cases.
#### Executing Test Cases #### Executing Test Cases
1. Start the test framework. 1. Start the test framework.
...@@ -771,20 +715,20 @@ When the build is complete, the test cases are automatically saved in the **out/ ...@@ -771,20 +715,20 @@ When the build is complete, the test cases are automatically saved in the **out/
You can run -h to display help information. You can run -h to display help information.
``` ```
### Executing Test Cases on Linux ### Executing Test Cases on Linux
#### Mapping Remote Port #### Mapping the Remote Port
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: 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:
1. On the HDC server, run the following commands: 1. On the HDC server, run the following commands:
``` ```
hdc_std kill hdc_std kill
hdc_std -m -s 0.0.0.0:8710 hdc_std -m -s 0.0.0.0:8710
``` ```
>**Note**: The IP address and port number are default values. > **NOTE**<br>The IP address and port number are default values.
2. On the HDC client, run the following command: 2. On the HDC client, run the following command:
``` ```
hdc_std -s xx.xx.xx.xx:8710 list targets hdc_std -s xx.xx.xx.xx:8710 list targets
``` ```
>**Note**: Enter the IP address of the device to test. > **NOTE**<br>Enter the IP address of the device to test.
#### Executing Test Cases #### Executing Test Cases
1. Start the test framework. 1. Start the test framework.
...@@ -819,7 +763,7 @@ You can obtain the test result in the following directory: ...@@ -819,7 +763,7 @@ You can obtain the test result in the following directory:
``` ```
test/developertest/reports/xxxx_xx_xx_xx_xx_xx test/developertest/reports/xxxx_xx_xx_xx_xx_xx
``` ```
>**Note**: The folder for test reports is automatically generated. > **NOTE**<br>The folder for test reports is automatically generated.
The folder contains the following files: The folder contains the following files:
| Type| Description| | Type| Description|
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册