【问题标题】:What are ways to run Rust cargo test and have the test runner display any log output to the console stdout or stderr?有哪些方法可以运行 Rust cargo 测试并让测试运行器将任何日志输出显示到控制台 stdout 或 stderr?
【发布时间】:2020-09-27 16:38:05
【问题描述】:

我学到了:

  • cargo test -- --nocapture 告诉 Cargo 显示输出而不是隐藏输出;我有兴趣看到成功测试的输出,而不仅仅是失败的测试。
  • env_logger crate 允许使用环境变量设置日志级别。

我正在寻找任何类似的方法,最好是任何官方参考指南,显示实现此目的的好方法。

【问题讨论】:

  • 如果测试失败,Cargo 会显示输出,所以一般不需要这样做。无论如何,您的解决方案没问题。
  • 谢谢两位!我会按照您的建议将信息从问题转移到答案。与其他问题一样,理想情况下请不要关闭,因为这个信息花了我 +2 个小时才弄清楚,并且在文档和各种博客文章中并不明显(恕我直言)。
  • cargo --no-capture 很容易找到,stackoverflow.com/questions/25106554/…

标签: testing logging rust rust-cargo


【解决方案1】:

这是一个适合我的解决方案。

Cargo.toml 依赖:

env_logger = "*"

演示源码:

use log::*;

pub fn foo() -> bool {
    info!("hello world");
    true
}

#[cfg(test)]
mod tests {
    use super::*;
    use env_logger;
    #[test]
    fn test_foo() {
        env_logger::init();
        assert!(foo());
    }
}

命令:

RUST_LOG=info cargo test -- --nocapture   

【讨论】:

    猜你喜欢
    • 2017-04-18
    • 1970-01-01
    • 2015-01-11
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多