Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add stuff about running subsets of tests
  • Loading branch information
steveklabnik authored and carols10cents committed Nov 25, 2016
commit a49d644aadcba1166966adb74491fbc7d2558f3b
94 changes: 94 additions & 0 deletions src/chXX-01-unit-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,97 @@ mod tests {
In this scenario, we have a non-`pub` function, `internal_adder`. Because tests
are just Rust code, and the `tests` module is just another module, we can
import and call `internal_adder` in a test just fine.

## Running a subset of tests

Sometimes, running a full test suite can take a long time. `cargo test` takes
an argument that allows you to only run certain tests, if you'd prefer to do that.
Let's say we had two tests of `add_two`:

```rust
pub fn add_two(a: i32) -> i32 {
a + 2
}

#[cfg(test)]
mod tests {
use add_two;

#[test]
fn add_two_and_two() {
assert_eq!(4, add_two(2));
}

#[test]
fn add_three_and_two() {
assert_eq!(5, add_two(3));
}

#[test]
fn one_hundred() {
assert_eq!(102, add_two(100));
}
}
```

Running with different arguments will run different subsets of the tests.
No arguments, as we've already seen, runs all the tests:

```text
$ cargo test
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
Running target/debug/deps/lol-06a75b4a1f2515e9

running 3 tests
test tests::add_three_and_two ... ok
test tests::one_hundred ... ok
test tests::add_two_and_two ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured

Doc-tests lol

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
```

We can pass the name of any test function to run only that test:

```text
$ cargo test one_hundred
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
Running target/debug/deps/lol-06a75b4a1f2515e9

running 1 test
test tests::one_hundred ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

Doc-tests lol

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
```

We can also pass part of a name, and `cargo test` will run all tests
that match:

```text
$ cargo test add
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
Running target/debug/deps/lol-06a75b4a1f2515e9

running 2 tests
test tests::add_three_and_two ... ok
test tests::add_two_and_two ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured

Doc-tests lol

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
```