Here's the contents of `src/lib.rs`:
```rust
-# fn main() {}
-#[test]
-fn it_works() {
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn it_works() {
+ }
}
```
```bash
$ cargo test
- Compiling adder v0.0.1 (file:///home/you/projects/adder)
- Running target/adder-91b3e234d4ed382a
+ Compiling adder v0.1.0 (file:///home/you/projects/adder)
+ Running target/debug/deps/adder-91b3e234d4ed382a
running 1 test
-test it_works ... ok
+test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
those later. For now, see this line:
```text
-test it_works ... ok
+test tests::it_works ... ok
```
Note the `it_works`. This comes from the name of our function:
```bash
$ cargo test
- Compiling adder v0.0.1 (file:///home/you/projects/adder)
- Running target/adder-91b3e234d4ed382a
+ Compiling adder v0.1.0 (file:///home/you/projects/adder)
+ Running target/debug/deps/adder-91b3e234d4ed382a
running 1 test
-test it_works ... FAILED
+test tests::it_works ... FAILED
failures:
----- it_works stdout ----
- thread 'it_works' panicked at 'assertion failed: false', /home/steve/tmp/adder/src/lib.rs:3
-
+---- test::it_works stdout ----
+ thread 'tests::it_works' panicked at 'assertion failed: false', src/lib.rs:5
failures:
- it_works
+ tests::it_works
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
-thread 'main' panicked at 'Some tests failed', /home/steve/src/rust/src/libtest/lib.rs:247
+error: test failed
```
Rust indicates that our test failed:
```text
-test it_works ... FAILED
+test tests::it_works ... FAILED
```
And that's reflected in the summary line:
```bash
$ cargo test
- Compiling adder v0.0.1 (file:///home/you/projects/adder)
- Running target/adder-91b3e234d4ed382a
+ Compiling adder v0.1.0 (file:///home/you/projects/adder)
+ Running target/debug/deps/adder-91b3e234d4ed382a
running 1 test
-test it_works ... ok
+test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
```bash
$ cargo test
- Compiling adder v0.0.1 (file:///home/you/projects/adder)
- Running target/adder-91b3e234d4ed382a
+ Compiling adder v0.1.0 (file:///home/you/projects/adder)
+ Running target/debug/deps/adder-91b3e234d4ed382a
running 1 test
-test it_works ... ok
+test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
```bash
$ cargo test
- Compiling adder v0.0.1 (file:///home/you/projects/adder)
- Running target/adder-91b3e234d4ed382a
+ Compiling adder v0.1.0 (file:///home/you/projects/adder)
+ Running target/debug/deps/adder-91b3e234d4ed382a
running 2 tests
test expensive_test ... ignored
```bash
$ cargo test -- --ignored
- Running target/adder-91b3e234d4ed382a
+ Running target/debug/deps/adder-91b3e234d4ed382a
running 1 test
test expensive_test ... ok
# The `tests` module
There is one way in which our existing example is not idiomatic: it's
-missing the `tests` module. The idiomatic way of writing our example
-looks like this:
+missing the `tests` module. You might have noticed this test module was
+present in the code that was initially generated with `cargo new` but
+was missing from our last example. Let's explain what this does.
+
+The idiomatic way of writing our example looks like this:
```rust,ignore
# fn main() {}
```bash
$ cargo test
Updating registry `https://github.com/rust-lang/crates.io-index`
- Compiling adder v0.0.1 (file:///home/you/projects/adder)
- Running target/adder-91b3e234d4ed382a
+ Compiling adder v0.1.0 (file:///home/you/projects/adder)
+ Running target/debug/deps/adder-91b3e234d4ed382a
running 1 test
test tests::it_works ... ok
# The `tests` directory
-Each file in `tests/*.rs` directory is treated as individual crate.
-So, to write an integration test, let's make a `tests` directory, and
-put a `tests/integration_test.rs` file inside, with this as its contents:
+Each file in `tests/*.rs` directory is treated as an individual crate.
+To write an integration test, let's make a `tests` directory and
+put a `tests/integration_test.rs` file inside with this as its contents:
```rust,ignore
extern crate adder;
```bash
$ cargo test
- Compiling adder v0.0.1 (file:///home/you/projects/adder)
- Running target/adder-91b3e234d4ed382a
+ Compiling adder v0.1.0 (file:///home/you/projects/adder)
+ Running target/debug/deps/adder-91b3e234d4ed382a
running 1 test
test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
- Running target/lib-c18e7d3494509e74
+ Running target/debug/integration_test-68064b69521c828a
running 1 test
test it_works ... ok
```bash
$ cargo test
- Compiling adder v0.0.1 (file:///home/steve/tmp/adder)
- Running target/adder-91b3e234d4ed382a
+ Compiling adder v0.1.0. (file:///home/you/projects/adder)
+ Running target/debug/deps/adder-91b3e234d4ed382a
running 1 test
test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
- Running target/lib-c18e7d3494509e74
+ Running target/debug/integration_test-68064b69521c828a
running 1 test
test it_works ... ok