简体   繁体   English

有哪些方法可以运行 Rust cargo test 并让测试运行器将任何日志 output 显示到控制台 stdout 或 stderr?

[英]What are ways to run Rust cargo test and have the test runner display any log output to the console stdout or stderr?

I've learned:我学过:

  • cargo test -- --nocapture which tells Cargo to show output instead of hiding output; cargo test -- --nocapture告诉 Cargo 显示 output 而不是隐藏 output; I'm interested in seeing output for successful tests, not just failed tests.我有兴趣看到 output 成功的测试,而不仅仅是失败的测试。
  • env_logger crate which enables using an environment variable to set the log level. env_logger crate 允许使用环境变量来设置日志级别。

I'm seeking any similar ways, and ideally any official reference guide that shows good ways to accomplish this.我正在寻找任何类似的方法,理想情况下是任何显示完成此操作的好方法的官方参考指南。

Here's one solution that is working for me.这是一个对我有用的解决方案。

Cargo.toml dependency: Cargo.toml 依赖:

env_logger = "*"

Demo source code:演示源代码:

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());
    }
}

Command:命令:

RUST_LOG=info cargo test -- --nocapture   

The Rust book shows how to do this . Rust 一书展示了如何做到这一点

The relevant part of that section:该部分的相关部分:

If we want to see printed values for passing tests as well, we can tell Rust to also show the output of successful tests with --show-output.如果我们还想看到通过测试的打印值,我们可以告诉 Rust 也显示成功测试的 output --show-output。

cargo test -- --show-output

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM