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.
* 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>
...
@@ -133,33 +119,20 @@ Example:
...
@@ -133,33 +119,20 @@ Example:
{
{
// Step 1 Call the function to obtain the result.
// Step 1 Call the function to obtain the result.
int actual = Sub(4, 0);
int actual = Sub(4, 0);
// Step 2 Use an assertion to compare the obtained result with the expected result.
// Step 2 Use an assertion to compare the obtained result with the expected result.
EXPECT_EQ(4, actual);
EXPECT_EQ(4, actual);
}
}
```
```
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>
using namespace testing::ext;
using namespace testing::ext;
```
```
3. Add the header file of the test class.
3. Add the header file of the test class.
...
@@ -175,22 +148,22 @@ Example:
...
@@ -175,22 +148,22 @@ Example:
void SetUp();
void SetUp();
void TearDown();
void TearDown();
};
};
void CalculatorSubTest::SetUpTestCase(void)
void CalculatorSubTest::SetUpTestCase(void)
{
{
// Set a setup function, which will be called before all test cases.
// Set a setup function, which will be called before all test cases.
}
}
void CalculatorSubTest::TearDownTestCase(void)
void CalculatorSubTest::TearDownTestCase(void)
{
{
// Set a teardown function, which will be called after all test cases.
// Set a teardown function, which will be called after all test cases.
}
}
void CalculatorSubTest::SetUp(void)
void CalculatorSubTest::SetUp(void)
{
{
// Set a setup function, which will be called before each test case.
// Set a setup function, which will be called before each test case.
}
}
void CalculatorSubTest::TearDown(void)
void CalculatorSubTest::TearDown(void)
{
{
// Set a teardown function, which will be called after each test case.
// Set a teardown function, which will be called after each test case.
...
@@ -210,7 +183,7 @@ Example:
...
@@ -210,7 +183,7 @@ Example:
{
{
// Step 1 Call the function to obtain the test result.
// Step 1 Call the function to obtain the test result.
int actual = Sub(4, 0);
int actual = Sub(4, 0);
// Step 2 Use an assertion to compare the obtained result with the expected result.
// Step 2 Use an assertion to compare the obtained result with the expected result.
EXPECT_EQ(4, actual);
EXPECT_EQ(4, actual);
}
}
...
@@ -234,12 +207,12 @@ Example:
...
@@ -234,12 +207,12 @@ Example:
- 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|
...
@@ -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'
...
@@ -303,7 +262,7 @@ Example:
...
@@ -303,7 +262,7 @@ Example:
it("appInfoTest001", 0, function () {
it("appInfoTest001", 0, function () {
// Step 1 Call the function to obtain the test result.
// Step 1 Call the function to obtain the test result.
var info = app.getInfo()
var info = app.getInfo()
// Step 2 Use an assertion to compare the obtained result with the expected result.
// Step 2 Use an assertion to compare the obtained result with the expected result.
expect(info != null).assertEqual(true)
expect(info != null).assertEqual(true)
})
})
...
@@ -311,26 +270,13 @@ Example:
...
@@ -311,26 +270,13 @@ 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'
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
```
```
3. Define the test suite (test class).
3. Define the test suite (test class).
...
@@ -367,7 +313,7 @@ Example:
...
@@ -367,7 +313,7 @@ Example:
it("appInfoTest001", 0, function () {
it("appInfoTest001", 0, function () {
// Step 1 Call the function to obtain the test result.
// Step 1 Call the function to obtain the test result.
var info = app.getInfo()
var info = app.getInfo()
// Step 2 Use an assertion to compare the obtained result with the expected result.
// Step 2 Use an assertion to compare the obtained result with the expected result.
expect(info != null).assertEqual(true)
expect(info != null).assertEqual(true)
})
})
...
@@ -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")
...
@@ -483,7 +428,6 @@ The following provides templates for different languages for your reference.
...
@@ -483,7 +428,6 @@ The following provides templates for different languages for your reference.
-**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.
```
```
...
@@ -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.
>- 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**.
...
@@ -612,9 +555,9 @@ The following provides templates for different languages for your reference.
...
@@ -612,9 +555,9 @@ The following provides templates for different languages for your reference.
}
}
```
```
> **Note**: Grouping test cases by test type allows you to execute a specific type of test cases when required.
> **Note**: 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": {
...
@@ -658,11 +601,12 @@ Perform the following steps:
...
@@ -658,11 +601,12 @@ Perform the following steps:
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:** 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.
...
@@ -731,6 +675,7 @@ When the build is complete, the test cases are automatically saved in the **out/
...
@@ -731,6 +675,7 @@ 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**: 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,7 +685,7 @@ When the build is complete, the test cases are automatically saved in the **out/
...
@@ -740,7 +685,7 @@ 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>
```
```
...
@@ -763,15 +708,15 @@ When the build is complete, the test cases are automatically saved in the **out/
...
@@ -763,15 +708,15 @@ When the build is complete, the test cases are automatically saved in the **out/
```
```
In the command:
In the command:
```
```
-t [TESTTYPE]: specifies the test type, which can be UT, MST, ST, or PERF. This parameter is mandatory.
-**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.
-**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.
-**tm [TESTMODULE]**: specifies the module to test. This parameter must be used together with **-tp**.
-ts [TESTSUITE]: specifies the test suite. This parameter can be used independently.
-**ts [TESTSUITE]**: specifies a test suite. This parameter can be used independently.
-tc [TESTCASE]: specifies the test case. This parameter must be used together with -ts.
-**tc [TESTCASE]**: specifies a test case. This parameter must be used together with **-ts**.
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:
```
```
...
@@ -803,12 +748,12 @@ To enable test cases to be executed on a remote Linux server or a Linux VM, map
...
@@ -803,12 +748,12 @@ To enable test cases to be executed on a remote Linux server or a Linux VM, map
```
```
In the command:
In the command:
```
```
-t [TESTTYPE]: specifies the test type, which can be UT, MST, ST, or PERF. This parameter is mandatory.
-**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.
-**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.
-**tm [TESTMODULE]**: specifies the module to test. This parameter must be used together with **-tp**.
-ts [TESTSUITE]: specifies the test suite. This parameter can be used independently.
-**ts [TESTSUITE]**: specifies a test suite. This parameter can be used independently.
-tc [TESTCASE]: specifies the test case. This parameter must be used together with -ts.
-**tc [TESTCASE]**: specifies a test case. This parameter must be used together with **-ts**.