@@ -5,6 +5,7 @@ This document explains how to test that all code examples in the book compile an
55## The ` mdbook test ` Command
66
77mdBook has a built-in test command that:
8+
891 . Extracts all Rust code blocks from markdown files
9102 . Compiles them as mini test programs
10113 . Links them against your library
@@ -29,14 +30,15 @@ mdbook test book -L ./target/debug/deps
2930```
3031
3132** Explanation:**
33+
3234- ` mdbook test book ` - Test all code blocks in the book
3335- ` -L ./target/debug/deps ` - Tell the Rust compiler where to find the compiled ` ros-z ` library
3436
3537### Expected Output
3638
3739On success, you'll see:
3840
39- ```
41+ ``` text
40422025-12-16 [INFO] (mdbook::book): Book building has started
41432025-12-16 [INFO] (mdbook::book): Running the html backend
42442025-12-16 [INFO] (mdbook::renderer::html_handlebars::search): Index built in ...
@@ -52,14 +54,14 @@ test result: ok. 15 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
5254
5355If an example fails to compile, you'll see detailed error messages:
5456
55- ```
57+ ``` text
5658test book/src/chapters/example.md - example::rust_1 (line 5) ... FAILED
5759
5860error[E0425]: cannot find value `xyz` in this scope
5961 --> /tmp/.tmpXYZ/main.rs:5:10
6062 |
61635 | let x = xyz;
62- | ^^^ not found in this scope
64+ | ^^^ not found in this scope
6365```
6466
6567## Code Block Annotations
@@ -209,18 +211,20 @@ Or use the Nix pre-commit hooks (already configured in the development shell).
2092112. **Use `no_run` for full programs** : Examples with `main()` that run indefinitely
2102123. **Use `ignore` sparingly** : Only for pseudo-code or intentionally incomplete examples
2112134. **Test locally before pushing** : Run the full workflow:
214+
212215 ` ` ` bash
213216 cargo build
214217 cargo test
215218 mdbook test book -L ./target/debug/deps
216219 ` ` `
220+
2172215. **Keep examples simple** : Complex examples are harder to test in documentation
218222
219223# # Understanding Test Output
220224
221225Each test corresponds to a code block :
222226
223- ` ` `
227+ ` ` ` text
224228test book/src/chapters/demo_talker.md - demo_talker::rust_1 (line 11) ... ok
225229 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^
226230 File path Test name Line number
@@ -235,6 +239,7 @@ When a test fails:
2352391. **Check the file and line number** in the error message
2362402. **Extract the code block** from the markdown
2372413. **Try compiling manually** :
242+
238243 ` ` ` bash
239244 # Create a test file
240245 cat > test.rs << 'EOF'
@@ -247,6 +252,7 @@ When a test fails:
247252 # Compile it
248253 rustc --edition 2021 -L target/debug/deps --extern ros_z test.rs
249254 ` ` `
255+
2502564. **Fix the issue** in the example file (not the markdown!)
2512575. **Update the markdown** if needed (should auto-update if using `{{#include}}`)
2522586. **Re-run** : ` mdbook test book -L ./target/debug/deps`
0 commit comments