Oh ut generator

From openharmonyinsight/openharmony-skills · View interactive version

Oh ut generator is an AI agent skill from the GitHub repository openharmonyinsight/openharmony-skills. Generate OpenHarmony C++ unit tests following HWTEST_F framework conventions. Supports Mock strategies, BUILD.gn configuration, NDK/NAPI interfaces, and maintains 75%+ coverage requirements with strict code style consistency. Use when generating unit tests for OpenHarmony C++ source files.

How to use this skill

Skill file

OpenHarmony Unit Test Generator

Overview

Generate comprehensive C++ unit tests for OpenHarmony code following HWTEST_F framework conventions.

Key Features

Usage

Invoke this skill when you need to generate unit tests for OpenHarmony C++ source files.

Supported Scenarios

  1. Single File Testing: Generate tests for all functions in a source file
  2. Specific Function Testing: Generate tests for a specific function only
  3. Test File Extension: Append new test cases to existing test files
  4. Mock Generation: Automatically create mock classes for dependencies

Critical Requirements

Test Implementation Completeness

Coverage Requirements

Code Style Consistency

When existing test files exist:

Consistency Threshold: >=70% for important scenarios

Security Coding

Framework Specifications

HWTEST_F Framework

// Test fixture structure
class YourServiceTest : public testing::Test {
public:
    static void SetUpTestCase(void);
    static void TearDownTestCase(void);
    void SetUp();
    void TearDown();
    std::shared_ptr<YourService> instance_;
};

// Test case format
HWTEST_F(TestClassName, TestCaseName_001, TestSize.Level1)

Test Case Naming

Object Management

// Standard smart pointers
SetUp(): instance_ = std::make_shared<YourService>();
TearDown(): instance_ = nullptr;

// OpenHarmony sptr/wptr
SetUp(): instance_ = MakeSptr<YourService>(); // or instance_ = new YourService();
TearDown(): instance_ = nullptr;

Mock Usage

auto mock = std::make_shared<MockClass>();
EXPECT_CALL(*mock, Method()).WillOnce(Return(value));

Assertions

ASSERT_NE(instance_, nullptr);  // Must check
EXPECT_EQ(result, expected);
EXPECT_TRUE(condition);

NDK Interface Testing

For NDK interfaces (OH_xxx), follow these requirements:

  1. Resource Lifecycle: Create/Destroy pairing is mandatory
  2. Test Structure: Normal path (001), Exception path (002), Boundary (003)
  3. Return Value Verification: Test all error codes
  4. Parameter Validation: Test null pointers, invalid values, boundaries

CJSON Testing

For CJSON library operations:

  1. Parse Tests: Normal, null pointer, invalid JSON
  2. Get Item Tests: Normal get, non-existent item, type checks
  3. Create Tests: Object, array, value creation
  4. Memory Management: All cJSON objects must be deleted

BUILD.gn Requirements

ohos_unittest Target

ohos_unittest("{CamelCase}Test") {
    module_out_path = "subsystem/module"
    branch_protector_ret = "pac_ret"
    sanitize = {
        integer_overflow = true
        cfi = true
        cfi_cross_dso = true
        debug = false
    }
    sources = ["test_file.cpp"]  # Alphabetically sorted
    include_dirs = ["//include/path"]
    deps = [":source_target"]
    external_deps = [
        "hilog:libhilog",
        "c_utils:utils",
        "googletest:gtest_main"
    ]
    defines = ["private=public"]
}

Configuration Matching

The system automatically extracts source file's BUILD.gn configuration:

Reference Files

Detailed specifications are available in the references/ directory:

Examples

See the examples/ directory for:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.