鸿蒙 5 单元测试入门:使用 HiLog 输出调试日志

暗雨OL
发布于 2025-6-27 21:12
浏览
0收藏

一、环境配置与基础设置
首先在模块级 build.gradle 中添加测试依赖:

dependencies {
testImplementation ‘org.junit.jupiter:junit-jupiter-api:5.8.1’
testRuntimeOnly ‘org.junit.jupiter:junit-jupiter-engine:5.8.1’
testImplementation ‘ohos.xtest:runner:1.0.0.0’
}
二、创建被测试类
实现一个简单的计算器功能作为测试目标:

// src/main/java/com/example/calculator/Calculator.java
package com.example.calculator;

public class Calculator {
public int add(int a, int b) {
return a + b;
}

public int subtract(int a, int b) {
    if (b > a) {
        return 0; // 故意设置错误逻辑
    }
    return a - b;
}

}
三、创建测试类并使用 HiLog
创建测试类并集成 HiLog 输出:

// src/test/java/com/example/calculator/CalculatorTest.java
package com.example.calculator;

import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class CalculatorTest {
private Calculator calculator;

// HiLog 配置:自定义标签和日志域
private static final HiLogLabel LOG_LABEL = new HiLogLabel(
    HiLog.LOG_APP, 0xD001F00, "UnitTest");

@BeforeEach
public void setUp() {
    calculator = new Calculator();
    HiLog.info(LOG_LABEL, "[Setup] Calculator instance created");
}

@Test
public void testAddition() {
    HiLog.debug(LOG_LABEL, "Starting addition test");
    int result = calculator.add(2, 3);
    
    // 输出中间值用于调试
    HiLog.info(LOG_LABEL, "Addition result: %{public}d", result);
    
    assertEquals(5, result);
    HiLog.debug(LOG_LABEL, "Addition test completed");
}

@Test
public void testSubtraction() {
    HiLog.warn(LOG_LABEL, "Starting subtraction test (expecting issues)");
    int result = calculator.subtract(5, 3);
    
    // 打印详细日志
    HiLog.debug(LOG_LABEL, "Operands: 5 and 3, Result: %{public}d", result);
    
    assertEquals(2, result);
}

}
四、运行测试并查看日志
在 DevEco Studio 中右键点击测试类,选择 Run ‘CalculatorTest’
测试完成后,在 ​​Log​​ 窗口筛选日志:
在搜索框输入 UnitTest 过滤相关日志
或使用命令行:hdc shell hilog -T “UnitTest”
五、查看日志输出示例
[UnitTest] [Setup] Calculator instance created
[UnitTest] [DEBUG] Starting addition test
[UnitTest] [INFO] Addition result: 5
[UnitTest] [DEBUG] Addition test completed
[UnitTest] [WARN] Starting subtraction test (expecting issues)
[UnitTest] [DEBUG] Operands: 5 and 3, Result: 2
六、关键点说明
​​日志级别选择​​:
HiLog.debug():调试信息(开发阶段)
HiLog.info():关键流程信息
HiLog.warn():预期可能出错的场景
​​安全输出​​:
HiLog.info(LOG_LABEL, “Value: %{public}s”, sensitiveData);
使用 %{public} 暴露必要信息
敏感数据使用 %{private} 或直接不输出
​​异常日志​​:
@Test
public void testException() {
try {
calculator.divide(5, 0);
} catch (Exception e) {
HiLog.error(LOG_LABEL, “Division failed: %{public}s”, e.getMessage());
}
}
七、高级技巧
添加自定义日志辅助方法:
protected void logTestStep(String message) {
HiLog.debug(LOG_LABEL, “[STEP] %{public}s”, message);
}
参数化测试日志:
@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void testMultiValues(int value) {
HiLog.info(LOG_LABEL, “Testing value: %{public}d”, value);
// 测试逻辑…
}
总结
通过在鸿蒙单元测试中使用 HiLog 输出日志,您可以:

实时跟踪测试执行流程
查看方法参数和返回值
快速定位断言失败原因
记录测试过程中的异常信息
建议在复杂逻辑测试、边界值检查以及失败用例诊断时启用详细日志输出,日常运行可适当降低日志级别以提高执行效率。结合 HiLog 的灵活输出,可大幅提升鸿蒙应用的测试效率和质量保障能力。

分类
标签
收藏
回复
举报
回复
    相关推荐