You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用tarpaulin运行指定子集的单元测试?

使用tarpaulin运行子集测试的方法

如果你想用tarpaulin只运行指定的测试子集,逻辑和cargo test类似,只是参数传递方式稍有不同,具体用法如下:

  • 单个测试函数:直接指定测试函数名即可,命令格式:

    cargo tarpaulin --tests "你的测试函数名"
    

    比如要运行test_login函数,就用:

    cargo tarpaulin --tests "test_login"
    
  • 测试模块/前缀匹配:如果要运行某个模块下的所有测试,或者匹配特定前缀的测试,直接用模块名或前缀即可:

    # 运行auth模块下的所有测试
    cargo tarpaulin --tests "auth::"
    # 运行所有以test_user开头的测试
    cargo tarpaulin --tests "test_user"
    
  • 传递额外测试参数:如果需要像cargo test那样用--nocapture这类参数,需要通过--run-options把参数传给测试进程:

    cargo tarpaulin --tests "test_login" --run-options "--nocapture"
    

    这就对应你之前用的cargo test test_login -- --nocapture命令。

  • 多个测试目标:如果要同时运行多个测试或模块,用逗号分隔即可:

    cargo tarpaulin --tests "test_login,auth::test_logout"
    

注意:tarpaulin的--tests参数支持部分匹配,只要测试名称(或完整路径)包含你指定的字符串,就会被纳入运行范围。如果需要精确匹配,最好写出完整的测试路径,比如crate::auth::test_login。

内容的提问来源于stack exchange,提问作者Coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 11:50:22