Skip to content

Commit 369b996

Browse files
Rollup merge of rust-lang#37766 - tarka:book-testing-concurrency-capture, r=steveklabnik
Add sections about testing concurrency and stdout/err capture
2 parents b633767 + 5a76fe4 commit 369b996

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/doc/book/testing.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,3 +586,45 @@ you add more examples.
586586

587587
We haven’t covered all of the details with writing documentation tests. For more,
588588
please see the [Documentation chapter](documentation.html).
589+
590+
# Testing and concurrency
591+
592+
One thing that is important to note when writing tests are run concurrently
593+
using threads. For this reason you should take care that your tests are written
594+
in such a way as to not depend on each-other, or on any shared state. "Shared
595+
state" can also include the environment, such as the current working directory,
596+
or environment variables.
597+
598+
If this is an issue it is possible to control this concurrency, either by
599+
setting the environment variable `RUST_TEST_THREADS`, or by passing the argument
600+
`--test-threads` to the tests:
601+
602+
```bash
603+
$ RUST_TEST_THREADS=1 cargo test # Run tests with no concurrency
604+
...
605+
$ cargo test -- --test-threads=1 # Same as above
606+
...
607+
```
608+
609+
# Test output
610+
611+
By default Rust's test library captures and discards output to standard
612+
out/error, e.g. output from `println!()`. This too can be controlled using the
613+
environment or a switch:
614+
615+
616+
```bash
617+
$ RUST_TEST_NOCAPTURE=1 cargo test # Preserve stdout/stderr
618+
...
619+
$ cargo test -- --nocapture # Same as above
620+
...
621+
```
622+
623+
However a better method avoiding capture is to use logging rather than raw
624+
output. Rust has a [standard logging API][log], which provides a frontend to
625+
multiple logging implementations. This can be used in conjunction with the
626+
default [env_logger] to output any debugging information in a manner that can be
627+
controlled at runtime.
628+
629+
[log]: https://crates.io/crates/log
630+
[env_logger]: https://crates.io/crates/env_logger

0 commit comments

Comments
 (0)