Skip to content

cargo test doesn't capture print from threads #42474

Closed
@kpcyrd

Description

@kpcyrd

I've filed this bug in rust-lang/cargo but I'm afraid rust-lang/rust is the correct repo for this.

I ran the tests for one of my crates that uses threads in its example code. I've noticed it printed some lines to stdout, even though --nocapture wasn't set, but not all of them. It appears that text printed from new threads are not correctly captured, I think this is not intended.

Reproduce

#[cfg(test)]
mod tests {
    use std::thread;
    use std::time::Duration;

    #[test]
    fn it_works() {
        thread::spawn(move || {
            println!("request: {:?}", 123);
        });

        thread::sleep(Duration::from_secs(1));
        println!("response: {:?}", 456);
    }

Output

running 2 tests
request: 123
test tests::it_works ... ok
[...]

Meta

rustc --version --verbose:

rustc 1.17.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.17.0
LLVM version: 4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-libtestArea: `#[test]` / the `test` libraryC-feature-acceptedCategory: A feature request that has been accepted pending implementation.P-lowLow priorityT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions