目录

@ohos.application.testRunner (TestRunner)

TestRunner is a basic template class in the automated testing framework. It provides standard APIs for preparing the test environment and running test cases. By inheriting and implementing the onPrepare() and onRun() methods, you can build custom test execution logic, providing an extensible foundation for the testing framework.

This module is applicable to scenarios where a custom unit testing framework or extended test functions are required. However, it should only be used within the automated testing framework and should not be called in formal service code. If you need to customize the test execution process, you must inherit the class and overwrite all its methods.

The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

The APIs of this module can be used only in JsUnit User Guide.

Modules to Import

import { TestRunner } from '@kit.TestKit';

TestRunner

TestRunner is a template for the unit test framework. You can inherit this class and override all its methods to customize the unit test framework.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Name Type Read-Only Optional Description
onStop OnStopFn No Yes

Called when the test is complete and before the test environment exits.

Atomic service API: This API can be used in atomic services since API version 26.0.0.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

Example

import { TestRunner } from '@kit.TestKit';

// Implement a custom test runner.
export default class UserTestRunner implements TestRunner {
  // Prepare the unit test environment.
  onPrepare() {
    console.info('Trigger onPrepare');
  }

  // Run the test cases.
  onRun() {
    console.info('Trigger onRun');
  }

  // Handle the callback when the test is complete.
  onStop() {
    console.info('Trigger onStop');
  }
}

[h2]onPrepare

onPrepare(): void

Prepares the unit test environment to run test cases.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Atomic service API: This API can be used in atomic services since API version 11.

Example

import { TestRunner } from '@kit.TestKit';

// Implement a custom test runner.
export default class UserTestRunner implements TestRunner {
  // Prepare the unit test environment.
  onPrepare() {
    console.info('Trigger onPrepare');
  }

  onRun() {
  }
}

[h2]onRun

onRun(): void

Called to run the test case when the test framework starts to execute a test.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Atomic service API: This API can be used in atomic services since API version 11.

Example

import { TestRunner } from '@kit.TestKit';

// Implement a custom test runner.
export default class UserTestRunner implements TestRunner {
  onPrepare() {
  }

  // Run the test cases.
  onRun() {
    console.info('Trigger onRun');
  }
}

OnStopFn

type OnStopFn = () => void

Called when the test is complete and before the test environment exits.

Since: 26.0.0

Atomic service API: This API can be used in atomic services since API version 26.0.0.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Ability.AbilityRuntime.Core