温馨提示×

Debian中Rust测试怎么跑

小樊
41
2026-10-11 02:20:38
栏目: 编程语言

在 Debian 上跑 Rust 测试,基本步骤和在其他 Linux 发行版上一样,前提是已经装好了 Rust 工具链。


1. 安装 Rust(如果还没装)

推荐用官方方式安装(Debian 仓库里的版本通常偏旧):

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"

验证:

rustc --version
cargo --version

2. 运行测试(最常见方式)

在 Rust 项目目录下:

cargo test

这会:

  • 编译测试代码
  • 运行所有单元测试、集成测试和文档测试

3. 常见用法

运行全部测试

cargo test

只运行某个测试(按名称过滤)

cargo test test_name

运行某个模块的测试

cargo test module_name::

显示 println! 输出

cargo test -- --nocapture

单线程运行(有时排查问题用)

cargo test -- --test-threads=1

4. 单元测试示例

src/lib.rs 或 src/main.rs 中:

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}

然后:

cargo test

5. 集成测试

放在 tests/ 目录:

tests/integration.rs
use my_crate::add;

#[test]
fn test_add() {
    assert_eq!(add(1, 2), 3);
}

运行方式一样:

cargo test

6. Debian 可能遇到的问题

缺少 C 编译器(某些 crate 需要)

sudo apt install build-essential

SSL / 网络相关依赖

sudo apt install pkg-config libssl-dev

如果你是指 Debian 上跑 Rust 的 Debian 包测试、CI 环境测试,或者是 交叉编译测试,可以告诉我具体场景,我可以进一步说明。

0 踩