- 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?
- 兩周自制腳本語言
- 構建移動網站與APP:HTML 5移動開發入門與實戰(跨平臺移動開發叢書)
- Vue.js快速入門與深入實戰
- Hands-On Data Structures and Algorithms with JavaScript
- Mastering Kali Linux for Web Penetration Testing
- FFmpeg入門詳解:音視頻原理及應用
- C語言程序設計實驗指導 (第2版)
- HTML5+CSS3+jQuery Mobile APP與移動網站設計從入門到精通
- Java程序設計教程
- Clojure High Performance Programming(Second Edition)
- 百萬在線:大型游戲服務端開發
- 程序員必會的40種算法
- Laravel Design Patterns and Best Practices
- Raspberry Pi開發實戰
- Skill Up:A Software Developer's Guide to Life and Career