| 1 |
# The test command |
| 2 |
|
| 3 |
When writing a book, you sometimes need to automate some tests. For example, |
| 4 |
[The Rust Programming Book](https://doc.rust-lang.org/stable/book/) uses a lot |
| 5 |
of code examples that could get outdated. Therefore it is very important for |
| 6 |
them to be able to automatically test these code examples. |
| 7 |
|
| 8 |
mdBook supports a `test` command that will run all available tests in a book. At |
| 9 |
the moment, only Rust tests are supported. |
| 10 |
|
| 11 |
#### Disable tests on a code block |
| 12 |
|
| 13 |
rustdoc doesn't test code blocks which contain the `ignore` attribute: |
| 14 |
|
| 15 |
```rust,ignore |
| 16 |
fn main() {} |
| 17 |
``` |
| 18 |
|
| 19 |
rustdoc also doesn't test code blocks which specify a language other than Rust: |
| 20 |
|
| 21 |
```markdown |
| 22 |
**Foo**: _bar_ |
| 23 |
``` |
| 24 |
|
| 25 |
rustdoc *does* test code blocks which have no language specified: |
| 26 |
|
| 27 |
``` |
| 28 |
This is going to cause an error! |
| 29 |
``` |
| 30 |
|
| 31 |
#### Specify a directory |
| 32 |
|
| 33 |
The `test` command can take a directory as an argument to use as the book's root |
| 34 |
instead of the current working directory. |
| 35 |
|
| 36 |
```bash |
| 37 |
mdbook test path/to/book |
| 38 |
``` |
| 39 |
|
| 40 |
#### `--library-path` |
| 41 |
|
| 42 |
The `--library-path` (`-L`) option allows you to add directories to the library |
| 43 |
search path used by `rustdoc` when it builds and tests the examples. Multiple |
| 44 |
directories can be specified with multiple options (`-L foo -L bar`) or with a |
| 45 |
comma-delimited list (`-L foo,bar`). The path should point to the Cargo |
| 46 |
[build cache](https://doc.rust-lang.org/cargo/guide/build-cache.html) `deps` directory that |
| 47 |
contains the build output of your project. For example, if your Rust project's book is in a directory |
| 48 |
named `my-book`, the following command would include the crate's dependencies when running `test`: |
| 49 |
|
| 50 |
```shell |
| 51 |
mdbook test my-book -L target/debug/deps/ |
| 52 |
``` |
| 53 |
|
| 54 |
See the `rustdoc` command-line [documentation](https://doc.rust-lang.org/rustdoc/command-line-arguments.html#-l--library-path-where-to-look-for-dependencies) |
| 55 |
for more information. |
| 56 |
|
| 57 |
#### `--chapter` |
| 58 |
|
| 59 |
The `--chapter` (`-c`) option allows you to test a specific chapter of the |
| 60 |
book using the chapter name or the relative path to the chapter. |