ArkTest Generator
Generate unit tests for ArkTS stdlib APIs using the ArkTest framework.
Prerequisites
Review the API source code in std/ directory first to understand the interface, methods, parameters, return types, and behavior.
Workflow:
- •Review existing source code in
std/directory - •Generate test file using patterns from this skill
- •Place test in
../tests/ets_func_tests/std/matching the API location
Quick Start
Basic test file structure:
function main(): int {
let suite = new arktest.ArkTestsuite('MyAPITest');
suite.addTest('basicTest', basicTest);
return suite.run();
}
function basicTest() {
let expected = <some_expected_result>;
let result = API.method();
arktest.assertEQ(result, expected);
}
Test File Template
Required Header
Every test file must include the Apache 2.0 license header:
/* * Copyright (c) 2025-2026 Huawei 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. */
Main Function Pattern
Always return suite.run() - never constants:
function main(): int {
let suite = new arktest.ArkTestsuite('APINameTest');
suite.addTest('testName', testFunction);
return suite.run();
}
Critical: Always return the suite result from main(). Never return true, 0, or constants.
Assertions Reference
Basic Assertions
- •
arktest.assertEQ(value1, value2, comment?)- Equality (===) - •
arktest.assertNE(value1, value2, comment?)- Inequality (!==) - •
arktest.assertTrue(condition, comment?)- Condition is true - •
arktest.assertFalse(condition, comment?)- Condition is false - •
arktest.assertLT(value1, value2, comment?)- Less than - •
arktest.assertLE(value1, value2, comment?)- Less than or equal - •
arktest.assertDoubleEQ(v1, v2, absError, comment?)- Float comparison
Error Testing
- •
arktest.expectError(fn, expect?)- Expects Error thrown- •
expectcan be string message or Error instance
- •
- •
arktest.expectThrow(fn, test?)- Expects exception with custom test - •
arktest.expectNoThrow(fn)- Ensures no error thrown
Test Patterns
Basic Functionality Test
function stringIndexTest() {
let str: string = 'abcd';
arktest.assertEQ(str[0], 'a');
arktest.assertEQ(str[1], 'b');
}
Edge Case Test
function boundsTest() {
let str: string = 'test';
try {
let x = str[-1];
} catch (e) {
arktest.assertTrue(e instanceof StringIndexOutOfBoundsError);
}
}
Async Test
function main(): int {
let suite = new arktest.ArkTestsuite('AsyncTest');
suite.addAsyncTest('asyncOp', asyncTest);
return suite.run();
}
async function asyncTest(): Promise<void> {
let result = await API.asyncMethod();
arktest.assertEQ(result, expected);
}
For Promise<int> functions (0=success, 1=failure):
suite.addTest('operationTest', () => {
arktest.assertEQ(waitForCompletion(testFunction), 0);
});
Container Test
function containerTest() {
let map = new HashMap<int, string>();
map.put(1, 'one');
arktest.assertEQ(map.get(1), 'one');
arktest.assertEQ(map.size(), 1);
}
Error Test
function errorTest() {
arktest.expectError(() => {
throw new Error('Specific message');
}, 'Specific message');
}
File Organization
Location
Place tests in ../tests/ets_func_tests/std/ matching stdlib structure:
- •
std/core/→tests/ets_func_tests/std/core/ - •
std/containers/→tests/ets_func_tests/std/containers/ - •
std/concurrency/→tests/ets_func_tests/std/concurrency/
Naming
- •Test file:
<APIName>Test.ets(e.g.,StringGetTest.ets) - •Test suite: Descriptive name matching API
- •Test functions:
camelCasedescriptive names
Multiple Suites
function main(): int {
let suite1 = new arktest.ArkTestsuite('APIPart1');
let suite2 = new arktest.ArkTestsuite('APIPart2');
suite1.addTest('test1', test1);
suite2.addTest('test2', test2);
let failures = 0;
failures += suite1.run();
failures += suite2.run();
return failures;
}
Code Style
- •4-space indentation (no tabs)
- •PascalCase for types, camelCase for functions
- •Explicit
public/privatemodifiers - •
constoverletwhen possible - •Space after colon:
let x: number - •Single empty line at end of file
- •Always add
overrideif method overrides Base class method
Test Coverage
Include tests for:
- •Basic functionality - Normal usage, happy path
- •Edge cases - Boundary values, empty inputs, special characters
- •Error handling - Invalid inputs, type mismatches, index errors
- •Async behavior - Concurrent operations, timing (if applicable)