liveBook · Manning

Test

As explained in Rust in Action: Systems programming concepts and techniques

Unit testing is the practice of testing individual program units in isolation against expected behavior.

Unit testing checks individual program units in isolation against expected behavior. In Rust, Cargo provides direct support for running these tests alongside the code they verify. section 5.5

A practical testing workflow includes:

  • Place tests in a test module marked with #[cfg(test)]. section 5.5
  • Import the surrounding implementation with use super::*. section 5.5
  • Mark each test with #[test] and compare actual values with assert_eq!. section 5.5
  • Run cargo test from the crate’s root directory and inspect passed or failed results. section 5.5

Testing Q7 conversions

The Q7 implementation tests conversion from f32, conversion to f32, and out-of-bounds inputs. The out_of_bounds test verifies that converting 10. and 1. produces the same value, while -10. and -1. also match. section 5.5

$ cargo test 
   Compiling ch5-q v0.1.0 (file:///path/to/ch5/ch5-q)
    Finished dev [unoptimized + debuginfo] target(s) in 2.86 s
     Running target\debug\deps\ch5_q-013c963f84b21f92
 
running 3 tests
test tests::f32_to_q7 ... ok
test tests::out_of_bounds ... ok
test tests::q7_to_f32 ... ok
 
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
 
   Doc-tests ch5-q
 
running 0 tests
 
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

The test output shows three named tests passing, with no failures or ignored tests. section 5.5

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Shows test modules, test attributes, assertions, and Cargo execution, without developing a broader testing strategy.
Examples
Uses Q7 conversion tests for f32 values, Q7-to-f32 values, and out-of-bounds inputs.
What it assumes
Assumes a Rust crate and implementation to test, while demonstrating test syntax directly.
What you can do after
Apply it via Cargo to verify implementation behavior and identify failed unit tests.
How it connects
Connects unit testing with fixed-point number formats, type conversions, assertions, and Cargo.

Also explained in

Compare how 53 books explain test →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.