Unit testing in rust

How to perform unit testing in binary cargo package with this structure below

|-- Cargo.lock
|-- Cargo.toml
|-- src
|   |-- bin
|   |   |-- clean.rs
|   |   |-- csv2json.rs
|   |   |-- csvsql.rs
|   |   |-- cut.rs
|   |   |-- format.rs
|   |   |-- grep.rs
|   |   |-- in2csv.rs
|   |   |-- join.rs
|   |   |-- look.rs
|   |   |-- sort.rs
|   |   |-- sql2csv.rs
|   |   |-- stack.rs
|   |   |-- stat.rs
|   |-- common
|   |   |-- help_descriptions.rs
|   |   |-- input.rs
|   |   |-- input_2.rs
|   |   |-- input_3.rs
|   |   |-- lib.rs
|   |   |-- number_decriptive.rs
|   |   |-- number_decriptives.rs
|   |   |-- number_description.rs
|   |   |-- number_descriptions.rs
|   |   |-- number_descriptive.rs
|   |   |-- number_descriptives.rs
|   |   |-- sql_functions.rs
|   |   |-- text_descriptives.rs
|   |-- data.csv
|  -- target
    |-- debug
        |-- 2018.xlsx
        |-- 2019.xlsx

Any function with the #[test] annotation becomes part of the unit test suite; any panic will result in a test failure. These can live anywhere within the module tree but often they're in the same file as the unit they're testing.

cargo test will compile and run the entire suite by default.

1 Like

The Rust Programming Language has a chapter on writing tests:

https://doc.rust-lang.org/book/ch11-01-writing-tests.html

1 Like