Writing tests
To run tests from the command line, type cargo test
. We will be doing this a lot.
Unit testing
Unit testing focuses on testing internal interfaces and components of a program. It is also called whitebox testing. To first create unit tests, it is a good idea to look at all of the top-level types, traits, and functions. All top-level identifiers make for good test cases. Depending on the structure of the program, it may also be a good idea to test combinations of these components to cover expected use cases.
We have one utility function, the statistic calculation, which would be a good candidate to write a unit test for. However, this function doesn't return any result. Instead, it immediately prints output to the console. To test this, we should break the function into two components—one that calculates the statistics, and a second function that prints the statistics. This would look as follows:
fn variable_summary<W: Write>(stdout: &mut raw::RawTerminal<W>, vname...