Skip to content

Cargo does not show ouput (e.g. stdout) from doctests with --nocapture #142295

Closed as duplicate of#98550
@w1th0utnam3

Description

@w1th0utnam3

Problem

As described in this closed issue: rust-lang/cargo#1732, there is no output (e.g. stdout) printed from doctests ran through cargo (e.g. cargo test --doc -- --nocapture) even when explicitly providing the --nocapture flag. Unfortunately the previous issue was closed without being fully resolved as indicated by recent comments.

Support for --nocapture was added to rustdoc in this PR: #86230

A PR to support this in cargo was proposed but it was never resolved: rust-lang/cargo#9705

Steps

  1. cargo new --lib doc_testing
  2. Add the following code:
/// This function prints a message to the console.
///
/// ```rust
/// println!("Test");
/// doc_testing::test_function();
/// ```
pub fn test_function() {
    println!("This is a test function.");
}
  1. Run cargo test --doc -- --nocapture

I expect cargo to show the output of the two print statements, however the output is only:

cargo test --doc -- --nocapture
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.02s
   Doc-tests doc_testing

running 1 test
test src/lib.rs - test_function (line 3) ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Possible Solution(s)

No response

Notes

No response

Version

cargo 1.87.0 (99624be96 2025-05-06)
release: 1.87.0
commit-hash: 99624be96e9d213b0e9b1e36451271f24e4a41d8
commit-date: 2025-05-06
host: aarch64-apple-darwin
libgit2: 1.9.0 (sys:0.20.0 vendored)
libcurl: 8.7.1 (sys:0.4.80+curl-8.12.1 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 3.4.1 11 Feb 2025
os: Mac OS 15.5.0 [64-bit]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-doctestsArea: Documentation tests, run by rustdoc

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions