- Learning Rust
- Paul Johnson Vesa Kaihlavirta
- 208字
- 2021-07-02 23:07:14
Using Cargo to help with your unit testing
Hopefully, unit testing is not something you will be unfamiliar with. A unit test is a test that operates on a specific function or method rather than an entire class or namespace. It ensures that the function operates correctly on the data it is presented with.
Unit tests within Rust are very simple to create (two examples are given in the assert_unittest and unittest directories). The following has been taken from the unittest example:
fn main() { println!("Tests have not been compiled, use rustc --test instead (or cargo test)"); } #[test] fn multiply_test() { if 2 * 3 == 5 { println!("The multiply worked"); } }
When this is built and executed, you may be surprised by the following result:

The reason why this unit test has passed despite 2 x 3 not being 5 is because the unit test is not testing the result of the operation, but that the operation itself is working. It is very important that this distinction is understood from an early stage to prevent confusion later.
We have hit a limitation of unit testing: if we are not testing the data but the operation, how can we know that the result itself is correct?
- Python概率統(tǒng)計
- Python程序設(shè)計教程(第2版)
- C++程序設(shè)計(第3版)
- 新一代通用視頻編碼H.266/VVC:原理、標(biāo)準(zhǔn)與實現(xiàn)
- 自己動手寫Java虛擬機
- TypeScript圖形渲染實戰(zhàn):基于WebGL的3D架構(gòu)與實現(xiàn)
- Mastering LibGDX Game Development
- 網(wǎng)站構(gòu)建技術(shù)
- Building Android UIs with Custom Views
- 蘋果的產(chǎn)品設(shè)計之道:創(chuàng)建優(yōu)秀產(chǎn)品、服務(wù)和用戶體驗的七個原則
- 微信小程序開發(fā)與實戰(zhàn)(微課版)
- iPhone應(yīng)用開發(fā)從入門到精通
- Python編程基礎(chǔ)教程
- Unity 5 Game Optimization
- React.js實戰(zhàn)