nextest-rs/nextest

Is it possible to display intentionally leaked output from tests?

Veetaha opened this issue · 3 comments

I have a use case where my tests do very heavy setup and teardown process. I measure each of them separately:

  • setup
  • run itself
  • teardown

Then I leak the durations into the stderr directly and thus I see this picture when we run tests with simple cargo test.
image

This is achieved in the tests code with the following macro.

/// Write the diagnostic message directly to the process's stderr FD
/// bypassing `cargo test`'s output suppression, but let the message
/// be captured and displayed in the failed test output too.
#[macro_export]
macro_rules! eprintln_leaky {
    ($($args:tt)*) => {
        match $crate::imp::std::format_args!($($args)*) {
            args => {
                use $crate::imp::std::io::Write;

                // Write directly to `stderr` bypassing `cargo test` output capturing
                #[allow(clippy::explicit_write)]
                $crate::imp::std::writeln!(
                    $crate::imp::std::io::stderr(),
                    "{}",
                    args
                ).unwrap();

                // `cargo test` patches `eprintln!` macro to enable output capturing
                // we repeat the same log twice so that it's also displayed
                // in the failed test output
                $crate::imp::std::eprintln!("{}", args);
            }
        }
    };
}

With cargo nextest this output is no longer visible.

Anyway, this is not a huge loss for me. Nextest is an outstandingly well-designed tool, and solves the CI performance problem just like I wanted it to solve. Very grateful!

Thanks for the kind words!

You should be able to set a per-test override with the success-output = "immediate" parameter: https://nexte.st/book/per-test-overrides.html

@sunshowers I think success-output doesn't solve this problem entirely, because with the intentionally leaked duration logs there will be displayed all other logs from the tests, and there are a lot of them. Maybe we could workaround this if we output these logs to stdout only, but every other logs would go to stderr and would not be displayed on success?

Ah, we currently don't have a way to say "show stdout but not stderr" or vice versa. As a workaround you can write logs to a file and just print out the name of the file, otherwise you're also welcome to try and suggest a UI design for this.