Loading...
Loading...
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.
npx skill4agent add openharmonyinsight/openharmony-skills oh-ut-generator// 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)_001_002_003_004+// 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;auto mock = std::make_shared<MockClass>();
EXPECT_CALL(*mock, Method()).WillOnce(Return(value));ASSERT_NE(instance_, nullptr); // Must check
EXPECT_EQ(result, expected);
EXPECT_TRUE(condition);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"]
}references/examples/