

Since 5.0.3.403, the ohosTest directory in newly created projects and modules no longer includes the testability, testrunner, and resources directories by default. (These directories are retained in existing projects.) If new projects require the use of abilities or resources, you need to create them manually.
To use ability capabilities, you must also create the testrunner directory and the OpenHarmonyTestRunner.ets file.
New version | Old version |
|
|
in the upper-right corner of DevEco Studio.{
"module": {
"name": "entry_test",
"type": "feature",
"description": "$string:module_test_desc",
"mainElement": "TestAbility", // Corresponds to the ability name in the abilities section below.
"deviceTypes": [
"phone",
"tablet",
"2in1"
],
"deliveryWithInstall": true,
"installationFree": false,
"pages": "$profile:test_pages", // Corresponds to the test_pages.json file under resources > base > profile.
"abilities": [ // Configuration of the ability to add.
{
"name": "TestAbility",
"srcEntry": "./ets/testability/TestAbility.ets",
"description": "$string:TestAbility_desc",
"icon": "$media:icon", // Ensure that the referenced resources exist.
"label": "$string:TestAbility_label",
"exported": true,
"startWindowIcon": "$media:icon",
"startWindowBackground": "$color:start_window_background"
}
]
}
} Before running test cases, connect the device and your PC, build your project into a signed HAP, and install and run the HAP on the device or Emulator. For details, see Running Your App on a Local Real Device or Running Your App on the Emulator.


on the right.
View the test result after the test is complete.

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.

You can set the type of code to be debugged in DevEco Studio. For details, see Setting the Debug Type.
DevEco Studio provides the code coverage for instrument tests.
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.
You can run all test cases in a project directory (test), run test cases from a file (such as Ability.test.ets), run all tests associated with a test suite (describe), and run a specific test method (it).
To run test cases from a file, use either of the following methods:

on the right.
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 ArkTS 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.

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



in Test Args and add test parameters in the Test Args dialog box displayed.
For example, set level to 1 and size to medium.

Key | Description | Value Range |
|---|---|---|
level | Level of the test case. | 0 to 4, for example, -s level 1. |
size | Granularity of the test case. | small, medium, or large, for example, -s size small. |
testType | Type of the test case. | function, performance, power, reliability, security, global, compatibility, user, standard, safety, or resilience, for example, -s testType function. |

Choose Run > Edit Configurations. In the Run/Debug Configurations dialog box, select Instrument Test, click the Debugger tab, and set Debug type.

By default, the debug type is Detect Automatically. The table below describes the debug types.
Debug Type | Code Type |
|---|---|
Detect Automatically | Code automatically detected. The debugger is selected based on the programming languages involved in the project module and its dependent modules. If a native module is detected, both the PandaDebugger and Native debug windows are displayed; if no native module is detected, only the PandaDebugger debug window is displayed. |
ArkTS/JS | ArkTS/JS. The PandaDebugger debug window is displayed. |
Native | C++. The Native debug window is displayed. |
Dual(ArkTS/JS + Native) | ArkTS/JS and C/C++. Both the PandaDebugger and Native debug windows are displayed. |
When debugging C++ code, ensure that the Address Sanitizer configuration of the current module is the same as that of all dependent HSP modules. Otherwise, the breakpoint of the C++ code may fail to be entered.
In instrument tests, you can run ASan detection for C/C++ methods. For details about ASan, see ASan. This feature does not apply to JavaScript.



As of DevEco Studio 6.0.0 Beta5, support for testing C++ code has been added, including the ability to run or debug C++ test code and collect code coverage statistics for C++.
Since the C++ test shared library (.so) cannot be executed directly on the device, it must be launched via Node-API from ArkTS/JS code to run the C/C++ test cases.
The C++ test directory generated by DevEco Studio does not include a C++ testing framework. You need to select and use an open-source testing framework.

// ArkTS test file Ability.test.ets
import entryTest from 'libentry_test.so';
export default function abilityTest() {
describe('ActsAbilityTest', () => {
// ...
it('testNative', 0, () => {
hilog.info(0x0000, 'testTag', '%{public}s', 'testNative it begin');
let result = entryTest.runNativeTest();
hilog.info(0x0000, 'testTag', '%{public}s', result)
expect(result).assertContain("ended");
})
})
} 
DevEco Studio does not collect C++ code coverage by default and needs to be enabled as follows:
// For versions earlier than DevEco Studio 6.0.2 Beta1
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
// For DevEco Studio 6.0.2 Beta1 and later versions, OHOS_TEST_COVERAGE is true in coverage mode and false in debug/run mode.
if(OHOS_TEST_COVERAGE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
endif() In the RunNativeTest method of the napi_init.cpp file, call the __llvm_profile_write_file method to save the coverage data to the c++_coverage.profraw file under the /data/storage/el2/base path on the device. This path and file name cannot be modified. Example code is as follows:
extern "C" {
void __llvm_profile_set_filename(char *);
int __llvm_profile_write_file(void);
}
static napi_value RunNativeTest(napi_env env, napi_callback_info info)
{
char filename[256];
snprintf(filename, sizeof(filename), "/data/storage/el2/base/c++_coverage.profraw"); // Coverage report file path and file name, which cannot be changed.
__llvm_profile_set_filename(filename);
// Start the test.
// ...
// End the test and save the data.
__llvm_profile_write_file();
// ...
}

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 link to open the report and view details of C++ code coverage.

hvigorw onDeviceTest -p module={moduleName} -p coverage={true|false} -p scope={suiteName}#{methodName} -p ohos-debug-asan={true|false} -p patch={patchPath} If C++ code coverage testing is enabled, a coverage report will be generated at the path: <module-path>/.test/default/outputs/ohosTest/cpp_reports/index.html.
ASan log path: <module-path>/.test/default/intermediates/ohosTest/coverage_data
Path of the test result file: <module-path>/.test/default/intermediates/ohosTest/coverage_data/test_result.txt