@@ -24,6 +24,7 @@ Cargo will automatically generate a simple test when you make a new project.
24
24
Here's the contents of ` src/lib.rs ` :
25
25
26
26
``` rust
27
+ # fn main () {}
27
28
#[test]
28
29
fn it_works () {
29
30
}
@@ -75,6 +76,7 @@ So why does our do-nothing test pass? Any test which doesn't `panic!` passes,
75
76
and any test that does ` panic! ` fails. Let's make our test fail:
76
77
77
78
``` rust
79
+ # fn main () {}
78
80
#[test]
79
81
fn it_works () {
80
82
assert! (false );
@@ -145,6 +147,7 @@ This is useful if you want to integrate `cargo test` into other tooling.
145
147
We can invert our test's failure with another attribute: ` should_panic ` :
146
148
147
149
``` rust
150
+ # fn main () {}
148
151
#[test]
149
152
#[should_panic]
150
153
fn it_works () {
@@ -175,6 +178,7 @@ Rust provides another macro, `assert_eq!`, that compares two arguments for
175
178
equality:
176
179
177
180
``` rust
181
+ # fn main () {}
178
182
#[test]
179
183
#[should_panic]
180
184
fn it_works () {
@@ -209,6 +213,7 @@ make sure that the failure message contains the provided text. A safer version
209
213
of the example above would be:
210
214
211
215
``` rust
216
+ # fn main () {}
212
217
#[test]
213
218
#[should_panic(expected = " assertion failed" )]
214
219
fn it_works () {
@@ -219,6 +224,7 @@ fn it_works() {
219
224
That's all there is to the basics! Let's write one 'real' test:
220
225
221
226
``` rust,ignore
227
+ # fn main() {}
222
228
pub fn add_two(a: i32) -> i32 {
223
229
a + 2
224
230
}
@@ -238,6 +244,7 @@ Sometimes a few specific tests can be very time-consuming to execute. These
238
244
can be disabled by default by using the ` ignore ` attribute:
239
245
240
246
``` rust
247
+ # fn main () {}
241
248
#[test]
242
249
fn it_works () {
243
250
assert_eq! (4 , add_two (2 ));
@@ -299,6 +306,7 @@ missing the `tests` module. The idiomatic way of writing our example
299
306
looks like this:
300
307
301
308
``` rust,ignore
309
+ # fn main() {}
302
310
pub fn add_two(a: i32) -> i32 {
303
311
a + 2
304
312
}
@@ -327,6 +335,7 @@ a large module, and so this is a common use of globs. Let's change our
327
335
` src/lib.rs ` to make use of it:
328
336
329
337
``` rust,ignore
338
+ # fn main() {}
330
339
pub fn add_two(a: i32) -> i32 {
331
340
a + 2
332
341
}
@@ -377,6 +386,7 @@ put a `tests/lib.rs` file inside, with this as its contents:
377
386
``` rust,ignore
378
387
extern crate adder;
379
388
389
+ # fn main() {}
380
390
#[test]
381
391
fn it_works() {
382
392
assert_eq!(4, adder::add_two(2));
@@ -432,6 +442,7 @@ running examples in your documentation (**note:** this only works in library
432
442
crates, not binary crates). Here's a fleshed-out ` src/lib.rs ` with examples:
433
443
434
444
``` rust,ignore
445
+ # fn main() {}
435
446
//! The `adder` crate provides functions that add numbers to other numbers.
436
447
//!
437
448
//! # Examples
0 commit comments