目录

Local Test

Currently, testing of C/C++ methods and system APIs is not supported.

Creating a Local Test Case

  1. Open the .ets file of the HAP, HAR, or HSP module to be tested in the project directory, move the cursor to any position in the code, and then right-click the code and choose Show Context Actions > Create Local Test, or press Alt+Enter (Option+Enter on macOS) and click Create Local Test in the dialog box displayed.

  2. In the Create Local Test dialog box, set the following parameters:

    • Testing library: test type. The default value is DECC-ArkTSUnit.
    • ArkTS name: name of the created test file, which contains test cases. The test file name must be unique in the project directory, and allows only letters, digits, underscores (_), and dots (.).
    • Destination package: location where the test file is stored. You are advised to store it in the test directory of the module to be tested.

  3. DevEco Studio automatically generates the corresponding test class in the test directory. In the test class, DevEco Studio generates test case templates for involved methods. You need to develop test code that complies with your service logic. For details, see JsUnit User Guide.

    You can also create a test case under the test directory and add the corresponding test class to the List.test.ets file.

Running Local Test Cases

[h2]Performing Local Tests in Running Mode

You can run all test cases in a project directory (test), run test cases from a file (such as Index.test.ets), run all tests associated with a test suite (describe), and run a specific test method (it). The test entries are as follows:

Directory entry

Test file entry

Test suite entry

Method entry

The following uses a test file as an example. In the project directory, right-click the target file and choose Run 'test file name' from the shortcut menu.

You can also perform a local test as follows:
  • Choose Run > Run 'test name'.
  • In the upper right corner of DevEco Studio, select a test task and click on the right.

View the test result after the test is complete.

[h2]Performing Local Tests in Debug Mode

Compared with the running mode, the debug mode provides breakpoint management. When a breakpoint is hit, you can perform single stepping, step-in, step-out, and stop-over operations as required. You can also view the thread stacks, variables, and expressions to quickly locate faults.

The following uses a test file as an example. After adding breakpoints, right-click the target file in the project directory and choose Debug 'Test file name' from the shortcut menu to perform a test task in debug mode.

When a breakpoint is hit, the Debug window is displayed. You can perform breakpoint management and basic debugging in this window. When a breakpoint is hit, you can view the variables and stack information of the current thread.

Right-click in the code editor window. The shortcut menu shows the functions specific to the debug mode, including Evaluate Expression, Run to Cursor, Force Run to Cursor, Add to Watches, and Add Inline Watch.

After the test is complete, view the test result on the console.

[h2]Performing Local Tests with Code Coverage

DevEco Studio provides the code coverage for local tests. Currently, only ArkTS projects are supported.

You can specify the local files or remote source code HAR packages to be included in or excluded from the coverage statistics. For details, see Filtering Files for Coverage Testing.

As with test cases without coverage, test cases with coverage can also be started from various entries. For example, to start a test case from a test file, use either of the following methods:

After the test starts, the build process begins, and the Cover tab appears at the bottom. Once the build is complete, the Cover tab will automatically open. After the test task is finished, the path to the test report will be printed on the tab page.

Click the path link to open the report and view the code coverage details. For details about the coverage rate, see Viewing Coverage Reports.

On the Cover tab page, you can click the rerun button to re-execute the test case based on the previous settings.

(Optional) Customizing a Test Case Task

By default, test cases can be run directly. To customize a test case task, perform the following steps:

  1. Choose Run > Edit Configurations from the main menu on the toolbar.
  2. On the Run/Debug Configurations page, click + and choose Local Test from the drop-down list.

  3. Set parameters for the task, and click OK.

Running Tests via the CLI

Run the following command in the root directory of the project to perform the local test:
hvigorw test -p module={moduleName} -p coverage={true | false} -p scope={suiteName}#{methodName} -p patch={patchPath}
  • module: module on which the test is performed. By default, the test cases of all modules are performed.
  • coverage: whether to generate a coverage report. The default value is true. When this feature is enabled (true), two reports are generated in the <module-path>/.test/default/outputs/test/reports directory, one in HTML format (index.html) and the other in JSON format (coverageReport.json). For details, see Viewing Coverage Reports.
  • scope: test case, in the format of {suiteName}#{methodName}, or test suite, in the format of {suiteName} for execution. By default, all cases of the current module are executed.
  • patch: (optional) path to the code patch file for incremental code coverage statistics. Only absolute paths are supported, and the file suffix must be .patch or .diff. Such files can be generated using commands such as git diff. This field is supported since version 26.0.0.
  • Multiple values of module and scope can be specified, separated by commas (,).
  • The command cannot be executed on Linux.

Path of the test result file: <module-path>/.test/default/intermediates/test/coverage_data/test_result.txt