unit test part of ch11.3
This commit is contained in:
parent
ba917801b6
commit
5015699e0d
7
ch11/test-organization/Cargo.lock
generated
Normal file
7
ch11/test-organization/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 = "test-organization"
|
||||||
|
version = "0.1.0"
|
6
ch11/test-organization/Cargo.toml
Normal file
6
ch11/test-organization/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "test-organization"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
37
ch11/test-organization/src/lib.rs
Normal file
37
ch11/test-organization/src/lib.rs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// Unit tests are small and more focused, testing one module in isolation
|
||||||
|
// Integration tests are entirely external and use code in the same
|
||||||
|
// way that any other external code would, using only the public
|
||||||
|
// interface and potentially exercising mutliple modules per test
|
||||||
|
//
|
||||||
|
// For unit tests, convention is to create a module in each file
|
||||||
|
// named tests for the test functions and annotate the module
|
||||||
|
// with cfg(test). Integration tests don't get this
|
||||||
|
//
|
||||||
|
// For the integration tests section, go back to the adder folder
|
||||||
|
// https://rust-book.cs.brown.edu/ch11-03-test-organization.html#integration-tests
|
||||||
|
|
||||||
|
pub fn add(left: u64, right: u64) -> u64 {
|
||||||
|
left + right
|
||||||
|
}
|
||||||
|
|
||||||
|
// Can test internal functions
|
||||||
|
fn internal_adder(left: usize, right: usize) -> usize {
|
||||||
|
left + right
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
let result = add(2, 2);
|
||||||
|
assert_eq!(result, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn internal() {
|
||||||
|
let result = internal_adder(2, 2);
|
||||||
|
assert_eq!(result, 4);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user