To accelerate test automation of OpenHarmony, arkXtest — an automated unit and UI test framework that supports both the JavaScript (JS) and TypeScript (TS) programming languages — is provided.
arkXtest is an automated test framework that supports both the JavaScript (JS) and TypeScript (TS) programming languages. It consists of JsUnit and UiTest.
In this document you will learn about the key functions of arkXtest and how to use it to perform unit testing on application or system APIs and to write UI automated test scripts.
JsUnit is a unit test framework that provides basic APIs for compiling test cases and generating test reports for testing system and application APIs.
UiTest is a UI test framework that provides the UI component search and operation capabilities through simple and easy-to-use APIs, and allows you to develop automated test scripts based on GUI operations.
### Introduction
This document describes the main functions, implementation principles, environment setup, and test script compilation and execution of arkXtest.
arkXtest is part of the OpenHarmony toolkit and provides basic capabilities of writing and running OpenHarmony automated test scripts. In terms of test script writing, arkXtest offers a wide range of APIs, including basic process APIs, assertion APIs, and APIs related to UI operations. In terms of test script running, arkXtest offers such features as identifying, scheduling, and executing test scripts, as well as summarizing test script execution results.
## Implementation
### Implementation
arkXtest is divided into two parts: unit test framework and UI test framework.
arkXtest is divided into two parts: unit test framework and UI test framework.
- Unit Test Framework
As the backbone of arkXtest, the unit test framework offers such features as identifying, scheduling, and executing test scripts, as well as summarizing test script execution results.
The UI test framework provides UiTest APIs for you to call in different test scenarios. The UI test scripts are executed on top of the unit test framework.
### Unit Test Framework
As the backbone of arkXtest, the unit test framework offers such features as identifying, scheduling, and executing test scripts, as well as summarizing test script execution results. The figure below shows the main functions of the unit test framework.
Figure 1 Main functions of the unit test framework
![](figures/UnitTest.PNG)
![](figures/UnitTest.PNG)
The following figure shows the basic unit test process. To start the unit test framework, run the **aa test** command.
Figure 2 Basic script process
![](figures/TestFlow.PNG)
- UI Test Framework
![](figures/TestFlow.PNG)
The UI test framework provides [UiTest APIs](../reference/apis/js-apis-uitest.md) for you to call in different test scenarios. The UI test scripts are executed on top of the aformentioned unit test framework.
> **NOTE**
>
> For details about the API in the unit test framework, see [Function Definition](https://gitee.com/openharmony/testfwk_arkxtest/blob/master/README_en.md#how-to-use).
The figure below shows the main functions of the UI test framework.
### UI Test Framework
![](figures/Uitest.PNG)
Figure 3 Main functions of the UI test framework
![](figures/Uitest.PNG)
### Constraints
- The features of the UI test framework are available only in OpenHarmony 3.1 and later versions.
## Constraints
- The features of the UI test framework are available only in OpenHarmony 3.1 Release and later versions.
- The feature availability of the unit test framework varies by version. For details about the mappings between the features and versions, see [arkXtest](https://gitee.com/openharmony/testfwk_arkxtest/blob/master/README_en.md).
- The feature availability of the unit test framework varies by version. For details about the mappings between the features and versions, see [arkXtest](https://gitee.com/openharmony/testfwk_arkxtest/blob/master/README_en.md).
...
@@ -54,13 +60,24 @@ Hardware: PC connected to an OpenHarmony device, such as the RK3568 development
...
@@ -54,13 +60,24 @@ Hardware: PC connected to an OpenHarmony device, such as the RK3568 development
[Download DevEco Studio](https://developer.harmonyos.com/cn/develop/deveco-studio#download) and set it up as instructed on the official website.
[Download DevEco Studio](https://developer.harmonyos.com/cn/develop/deveco-studio#download) and set it up as instructed on the official website.
## Creating and Compiling a Test Script
## Creating a Test Script
### Creating a Test Script
1. Open DevEco Studio and create a project, in which the **ohos** directory is where the test script is located.
1. Open DevEco Studio and create a project, in which the **ohos** directory is where the test script is located.
2. Open the .ets file of the module to be tested in the project directory. Move the cursor to any position in the code, and then right-click and choose **Show Context Actions** > **Create Ohos Test** or press **Alt+Enter** and choose **Create Ohos Test** to create a test class.
2. Open the .ets file of the module to be tested in the project directory. Move the cursor to any position in the code, and then right-click and choose **Show Context Actions** > **Create Ohos Test** or press **Alt+Enter** and choose **Create Ohos Test** to create a test class.
## Writing a Unit Test Script
### Writing a Unit Test Script
The unit test script must contain the following basic elements:
1. Import of the dependencies so that the dependent test APIs can be used.
2. Test code, mainly about the related logic, such as API invoking.
3. Invoking of the assertion APIs and setting of checkpoints. If there is no checkpoint, the test script is considered as incomplete.
The following sample code is used to start the test page to check whether the page displayed on the device is the expected page.
@@ -93,25 +110,17 @@ export default function abilityTest() {
...
@@ -93,25 +110,17 @@ export default function abilityTest() {
}
}
```
```
The unit test script must contain the following basic elements:
### Writing a UI Test Script
1. Import of the dependencies so that the dependent test APIs can be used.
2. Test code, mainly about the related logic, such as API invoking.
The UI test is based on the unit test. The UI test script adds the invoking of the UiTest interface (providing a link) to the unit test script to complete the corresponding test activities. In this example, the UI test script is written based on the preceding unit test script. It implements the click operation on the started application page and checks whether the page changes as expected.
3. Invoking of the assertion APIs and setting of checkpoints. If there is no checkpoint, the test script is considered as incomplete.
1. Import the dependency.
## Writing a UI Test Script
You write a UI test script based on the unit test framework, adding the invoking of APIs provided by the UI test framework to implement the corresponding test logic.
In this example, the UI test script is written based on the preceding unit test script. First, import the dependency, as shown below:
@@ -158,9 +167,11 @@ export default function abilityTest() {
...
@@ -158,9 +167,11 @@ export default function abilityTest() {
You can run a test script in DevEco Studio in any of the following modes:
You can run a test script in DevEco Studio in any of the following modes:
- Test package level: All test cases in the test package are executed.
1. Test package level: All test cases in the test package are executed.
- Test suite level: All test cases defined in the **describe** method are executed.
- Test method level: The specified **it** method, that is, a single test case, is executed.
2. Test suite level: All test cases defined in the **describe** method are executed.
3. Test method level: The specified **it** method, that is, a single test case, is executed.
![](figures/Execute.PNG)
![](figures/Execute.PNG)
...
@@ -170,9 +181,17 @@ After the test is complete, you can view the test result in DevEco Studio, as sh
...
@@ -170,9 +181,17 @@ After the test is complete, you can view the test result in DevEco Studio, as sh
![](figures/TestResult.PNG)
![](figures/TestResult.PNG)
**Viewing the Test Case Coverage**
After the test is complete, you can view the test case coverage.
### In the CLI
### In the CLI
To run a test script in the CLI, execute **aa** commands with different execution control keywords.
Install the application test package on the test device and run the **aa** command with different execution control keywords in the CLI.
> **NOTE**
>
> Before running commands in the CLI, make sure hdc-related environment variables have been configured.
The table below lists the keywords in **aa** test commands.
The table below lists the keywords in **aa** test commands.
...
@@ -196,15 +215,13 @@ The framework supports multiple test case execution modes, which are triggered b
...
@@ -196,15 +215,13 @@ The framework supports multiple test case execution modes, which are triggered b
| random | Whether to execute test cases in random sequence.| **true**/**false** (default value) | -s random true |
| random | Whether to execute test cases in random sequence.| **true**/**false** (default value) | -s random true |
| testType | Type of the test case to be executed. | function, performance, power, reliability, security, global, compatibility, user, standard, safety, resilience| -s testType function |
| testType | Type of the test case to be executed. | function, performance, power, reliability, security, global, compatibility, user, standard, safety, resilience| -s testType function |
| level | Level of the test case to be executed. | 0, 1, 2, 3, 4 | -s level 0 |
| level | Level of the test case to be executed. | 0, 1, 2, 3, 4 | -s level 0 |
| size | Size of the test case to be executed. | small, medium, large | -s size small
| size | Size of the test case to be executed. | small, medium, large | -s size small |
| stress | Number of times that the test case is executed. | Positive integer | -s stress 1000 |
| stress | Number of times that the test case is executed. | Positive integer | -s stress 1000 |
**Running Commands**
**Running Commands**
> Before running commands in the CLI, make sure hdc-related environment variables have been configured.