Skip to content

test: improve unit test coverage to 62% and fix stale expectations#691

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:feat/improve-unittest-coverage
Jun 22, 2026
Merged

test: improve unit test coverage to 62% and fix stale expectations#691
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:feat/improve-unittest-coverage

Conversation

@pengfeixx

Copy link
Copy Markdown
Contributor

Add new unit tests for low-coverage classes to raise line coverage above 60%.

为低覆盖类新增单元测试,将行覆盖率提升至60%以上。

Fix 56 stale test expectations to match current source behavior.

修复56个过时的测试期望,使其匹配当前源码行为。

Exclude vendored 3rdparty from coverage stats in the test script.

测试脚本中排除第三方代码,避免其误计入项目覆盖率统计。

Log: 提升单元测试覆盖率至62%
Influence: 行覆盖率由45.3%提升至62.0%,全部791个测试通过,零失败。

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @pengfeixx, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@github-actions

Copy link
Copy Markdown
  • 敏感词检查失败, 检测到1个文件存在敏感词
详情
{
    "deepin-devicemanager/tests/src/DriverControl/ut_httpdriverinterface.cpp": [
        {
            "line": "    EXPECT_STREQ(\"http://example.com/d.deb\", d0.strDeb.toStdString().c_str());",
            "line_number": 94,
            "rule": "S35",
            "reason": "Url link | 82eb1a6f74"
        }
    ]
}

Add new unit tests for low-coverage classes to raise line coverage above 60%.

为低覆盖类新增单元测试,将行覆盖率提升至60%以上。

Fix 56 stale test expectations to match current source behavior.

修复56个过时的测试期望,使其匹配当前源码行为。

Exclude vendored 3rdparty from coverage stats in the test script.

测试脚本中排除第三方代码,避免其误计入项目覆盖率统计。

Log: 提升单元测试覆盖率至62%
Influence: 行覆盖率由45.3%提升至62.0%,全部791个测试通过,零失败。
@pengfeixx pengfeixx force-pushed the feat/improve-unittest-coverage branch from 48b4c6e to e79c546 Compare June 22, 2026 05:34
@github-actions

Copy link
Copy Markdown
  • 敏感词检查失败, 检测到1个文件存在敏感词
详情
{
    "deepin-devicemanager/tests/src/DriverControl/ut_httpdriverinterface.cpp": [
        {
            "line": "    EXPECT_STREQ(\"http://example.com/d.deb\", d0.strDeb.toStdString().c_str());",
            "line_number": 94,
            "rule": "S35",
            "reason": "Url link | 82eb1a6f74"
        }
    ]
}

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, pengfeixx

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码全面更新了设备管理器各模块的单元测试用例以适配底层逻辑变更,并新增了多个基础类与工具类的测试覆盖
逻辑正确且无安全漏洞,因测试用例中存在轻微的资源未释放及环境依赖问题扣5分

■ 【详细分析】

  • 1.语法逻辑基本正确✓

新增和修改的测试用例能够准确反映业务逻辑的变更(如内存频率单位由MHz改为MT/s、屏幕尺寸计算精度修正、无效值过滤等),断言期望值与实际逻辑严格对应。在ut_dbuswakeupinterface.cppSetUp方法中,通过new QDBusInterface动态创建了一个无效的DBus接口对象并赋值给单例的mp_Iface指针,但在TearDown方法中仅恢复了原始指针,未对动态创建的QDBusInterface对象执行delete操作,存在内存泄漏风险。
潜在问题:ut_dbuswakeupinterface.cppSetUp内分配的QDBusInterface对象在TearDown中未被释放,造成每次执行该测试用例时泄漏一个对象
建议:在ut_dbuswakeupinterface.cppTearDown方法中,先delete m_iface->mp_Iface;再恢复原始指针;或者使用std::unique_ptr管理该测试替身对象的生命周期

  • 2.代码质量良好✓

测试用例命名清晰规范,如UT_Common_formatTotalCache_KiB能够直观表达测试场景。新增的测试覆盖了大量的边界条件(如空字符串、非法JSON格式、空列表、无效数值等),有效提升了代码防御性测试的覆盖率。ut_dbuswakeupinterface.cpp中通过替换单例内部指针来隔离DBus服务依赖的设计思路值得肯定,但直接使用/etc/hostname作为文件存在性测试的探针,引入了对具体操作系统文件状态的硬依赖,降低了测试在极端环境或容器环境中的健壮性。
潜在问题:canInputWakeupMachine_exists测试用例强依赖/etc/hostname文件必须存在,若在精简版系统或特殊容器中运行可能导致测试误报
建议:在测试的SetUp阶段使用QFile创建一个临时的探针文件,在TearDown阶段将其删除,从而解除对系统固有文件的依赖

  • 3.代码性能无性能问题✓

测试代码的执行不涉及高频轮询或大规模数据处理。ut_commonfunction.cpp中通过Common::executeClientCmd调用了真实的/bin/echo系统命令,会产生一次进程创建的开销,但作为验证外部命令执行通路的集成测试,此开销在可接受范围内,不会对整体测试流水线造成显著性能瓶颈。

  • 4.代码安全存在0个安全漏洞✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码中未引入任何安全漏洞。ut_commonfunction.cpp中调用的executeClientCmd函数参数被硬编码为/bin/echo和固定的字符串列表,不存在命令注入风险。ut_httpdriverinterface.cpp中解析的JSON数据均为测试用例内部定义的硬编码字符串,不涉及外部不可信输入。ut_dbuswakeupinterface.cpp中访问的文件路径为硬编码的系统路径或绝对不存在的随机路径,不存在路径遍历风险。

  • 建议:继续保持在测试代码中避免使用外部动态输入的良好实践

■ 【改进建议代码示例】

// ut_dbuswakeupinterface.cpp 修复内存泄漏与文件依赖
void SetUp() override
{
    m_iface = DBusWakeupInterface::getInstance();
    m_origInputIface = m_iface->mp_InputIface;
    m_origIface = m_iface->mp_Iface;
    m_iface->mp_InputIface = nullptr;
    
    // 使用局部对象管理内存,避免泄漏
    m_fakeIface = new QDBusInterface("org.nonexistent.NoService", "/x",
                                     "org.nonexistent.NoService",
                                     QDBusConnection::systemBus());
    m_iface->mp_Iface = m_fakeIface;

    // 创建临时文件替代依赖 /etc/hostname
    m_tmpFile = new QTemporaryFile(this);
    if (m_tmpFile->open()) {
        m_tmpFilePath = m_tmpFile->fileName();
    }
}

void TearDown() override
{
    // 恢复原始指针
    m_iface->mp_InputIface = m_origInputIface;
    m_iface->mp_Iface = m_origIface;
    
    // 释放动态创建的对象
    if (m_fakeIface) {
        delete m_fakeIface;
        m_fakeIface = nullptr;
    }
    
    // 临时文件由 Qt 对象树自动清理
}

// 对应的测试用例修改
TEST_F(UT_DBusWakeupInterface, UT_DBusWakeupInterface_canInputWakeupMachine_exists)
{
    EXPECT_TRUE(m_iface->canInputWakeupMachine(m_tmpFilePath));
}

TEST_F(UT_DBusWakeupInterface, UT_DBusWakeupInterface_canInputWakeupMachine_missing)
{
    EXPECT_FALSE(m_iface->canInputWakeupMachine("/tmp/nonexistent-wakeup-file-12345"));
}

private:
    QDBusInterface *m_fakeIface = nullptr;
    QTemporaryFile *m_tmpFile = nullptr;
    QString m_tmpFilePath;

@pengfeixx

Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot deepin-bot Bot merged commit 80bbb2a into linuxdeepin:master Jun 22, 2026
22 checks passed
@pengfeixx pengfeixx deleted the feat/improve-unittest-coverage branch June 22, 2026 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants