@@ -85,3 +85,92 @@ javascript and a statically-generated search index. No special web server is
85
85
required for the search.
86
86
87
87
[ sundown ] : https://github.com/vmg/sundown/
88
+
89
+ # Testing the Documentation
90
+
91
+ ` rustdoc ` has support for testing code examples which appear in the
92
+ documentation. This is helpful for keeping code examples up to date with the
93
+ source code.
94
+
95
+ To test documentation, the ` --test ` argument is passed to rustdoc:
96
+
97
+ ~~~
98
+ rustdoc --test crate.rs
99
+ ~~~
100
+
101
+ ## Defining tests
102
+
103
+ Rust documentation currently uses the markdown format, and code blocks can refer
104
+ to any piece of code-related documentation, which isn't always rust. Because of
105
+ this, only code blocks with the language of "rust" will be considered for
106
+ testing.
107
+
108
+ ~~~
109
+ ```rust
110
+ // This is a testable code block
111
+ ```
112
+
113
+ ```
114
+ // This is not a testable code block
115
+ ```
116
+
117
+ // This is not a testable code block (4-space indent)
118
+ ~~~
119
+
120
+ In addition to only testing "rust"-language code blocks, there are additional
121
+ specifiers that can be used to dictate how a code block is tested:
122
+
123
+ ~~~
124
+ ```rust,ignore
125
+ // This code block is ignored by rustdoc, but is passed through to the test
126
+ // harness
127
+ ```
128
+
129
+ ```rust,should_fail
130
+ // This code block is expected to generate a failure
131
+ ```
132
+ ~~~
133
+
134
+ Rustdoc also supplies some extra sugar for helping with some tedious
135
+ documentation examples. If a line os prefixed with a ` # ` character, then the
136
+ line will not show up in the HTML documentation, but it will be used when
137
+ testing the code block.
138
+
139
+ ~~~
140
+ ```rust
141
+ # // showing 'fib' in this documentation would just be tedious and detracts from
142
+ # // what's actualy being documented.
143
+ # fn fib(n: int) { n + 2 }
144
+
145
+ do spawn { fib(200); }
146
+ ```
147
+ ~~~
148
+
149
+ The documentation online would look like ` do spawn { fib(200); } ` , but when
150
+ testing this code, the ` fib ` function will be included (so it can compile).
151
+
152
+ ## Running tests (advanced)
153
+
154
+ Running tests often requires some special configuration to filter tests, find
155
+ libraries, or try running ignored examples. The testing framework that rustdoc
156
+ uses is build on ` extra::test ` , which is also used when you compile crates with
157
+ rustc's ` --test ` flag. Extra arguments can be passed to rustdoc's test harness
158
+ with the ` --test-args ` flag.
159
+
160
+ ~~~
161
+ // Only run tests containing 'foo' in their name
162
+ rustdoc --test lib.rs --test-args 'foo'
163
+
164
+ // See what's possible when running tests
165
+ rustdoc --test lib.rs --test-args '--help'
166
+
167
+ // Run all ignored tests
168
+ rustdoc --test lib.rs --test-args '--ignored'
169
+ ~~~
170
+
171
+ When testing a library, code examples will often show how functions are used,
172
+ and this code often requires ` use ` -ing paths from the crate. To accomodate this,
173
+ rustdoc will implicitly add ` extern mod <crate>; ` where ` <crate> ` is the name of
174
+ the crate being tested to the top of each code example. This means that rustdoc
175
+ must be able to find a compiled version of the library crate being tested. Extra
176
+ search paths may be added via the ` -L ` flag to ` rustdoc ` .
0 commit comments