2015-12-30 06:58:56 +08:00
# The test command
2018-08-03 05:52:54 +08:00
When writing a book, you sometimes need to automate some tests. For example,
[The Rust Programming Book ](https://doc.rust-lang.org/stable/book/ ) uses a lot
of code examples that could get outdated. Therefore it is very important for
them to be able to automatically test these code examples.
2015-12-30 06:58:56 +08:00
2018-08-03 10:34:26 +08:00
mdBook supports a `test` command that will run all available tests in a book. At
the moment, only rustdoc tests are supported, but this may be expanded upon in
the future.
2015-12-30 22:04:24 +08:00
2018-08-03 05:52:54 +08:00
#### Disable tests on a code block
2015-12-30 22:04:24 +08:00
2018-08-03 05:52:54 +08:00
rustdoc doesn't test code blocks which contain the `ignore` attribute:
```rust,ignore
fn main() {}
```
rustdoc also doesn't test code blocks which specify a language other than Rust:
```markdown
**Foo** : _bar_
```
rustdoc *does* test code blocks which have no language specified:
```
This is going to cause an error!
```
#### Specify a directory
2018-08-03 10:34:26 +08:00
The `test` command can take a directory as an argument to use as the book's root
instead of the current working directory.
2015-12-30 22:04:24 +08:00
2015-12-30 06:58:56 +08:00
```bash
2018-08-03 05:52:54 +08:00
mdbook test path/to/book
2015-12-30 06:58:56 +08:00
```
2018-08-03 05:52:54 +08:00
#### --library-path
The `--library-path` (`-L`) option allows you to add directories to the library
search path used by `rustdoc` when it builds and tests the examples. Multiple
directories can be specified with multiple options (`-L foo -L bar`) or with a
2021-06-25 10:08:23 +08:00
comma-delimited list (`-L foo,bar`). The path should point to the Cargo
2021-06-25 23:24:57 +08:00
[build cache ](https://doc.rust-lang.org/cargo/guide/build-cache.html ) `deps` directory that
2021-06-25 10:08:23 +08:00
contains the build output of your project. For example, if your Rust project's book is in a directory
named `my-book` , the following command would include the crate's dependencies when running `test` :
```shell
mdbook test my-book -L target/debug/deps/
```
See the `rustdoc` command-line [documentation ](https://doc.rust-lang.org/rustdoc/command-line-arguments.html#-l--library-path-where-to-look-for-dependencies )
for more information.
2018-08-03 05:52:54 +08:00
#### --dest-dir
2018-08-03 10:34:26 +08:00
The `--dest-dir` (`-d`) option allows you to change the output directory for the
2018-09-06 23:24:42 +08:00
book. Relative paths are interpreted relative to the book's root directory. If
not specified it will default to the value of the `build.build-dir` key in
`book.toml` , or to `./book` .