ch11.02 done
This commit is contained in:
parent
b433eb19d2
commit
ba917801b6
7
ch11/running-tests/Cargo.lock
generated
Normal file
7
ch11/running-tests/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "running-tests"
|
||||
version = "0.1.0"
|
6
ch11/running-tests/Cargo.toml
Normal file
6
ch11/running-tests/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "running-tests"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
73
ch11/running-tests/src/lib.rs
Normal file
73
ch11/running-tests/src/lib.rs
Normal file
@ -0,0 +1,73 @@
|
||||
// Can control which command line arguments go to
|
||||
// cargo test and which go to the actually binary itself
|
||||
// with the `--`. Before the `--` the args go to cargo test
|
||||
// then after go to the binary
|
||||
//
|
||||
// If want the fine-grained control over the number of
|
||||
// threads, use the --test-threads option.
|
||||
// $ cargo test -- --test-threads=1
|
||||
|
||||
fn prints_and_returns_10(a: i32) -> i32 {
|
||||
println!("I got the value {a}");
|
||||
10
|
||||
}
|
||||
|
||||
pub fn add_two(a: usize) -> usize {
|
||||
a + 2
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// by default tests capture stdout and wont print anything
|
||||
// if want to see output, use the `--show-output flag`
|
||||
// $ cargo test -- --show-output
|
||||
#[test]
|
||||
fn this_test_will_pass() {
|
||||
let value = prints_and_returns_10(4);
|
||||
assert_eq!(value, 10);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn this_test_will_fail() {
|
||||
// let value = prints_and_returns_10(4);
|
||||
// assert_eq!(value, 5);
|
||||
// }
|
||||
|
||||
// Can pass a string to `cargo test` to test all
|
||||
// tests that contain said string.
|
||||
// $ cargo test add
|
||||
// will test all functions that have `add` in the
|
||||
// name.
|
||||
#[test]
|
||||
fn add_two_and_two() {
|
||||
let result = add_two(2);
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_three_and_two() {
|
||||
let result = add_two(3);
|
||||
assert_eq!(result, 5);
|
||||
}
|
||||
|
||||
// Can pass a single name to `cargo test` to test only
|
||||
// a single function.
|
||||
// $ cargo test one_hundred
|
||||
#[test]
|
||||
fn one_hundred() {
|
||||
let result = add_two(100);
|
||||
assert_eq!(result, 102);
|
||||
}
|
||||
|
||||
// Can tell rust to ignore specific tests unless requested
|
||||
// Can choose to only run the ignored code with the
|
||||
// `--ignored` flag
|
||||
// $ cargo test -- --ignored
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn expensive_test() {
|
||||
assert!(true);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user