官术网_书友最值得收藏!

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?

主站蜘蛛池模板: 无棣县| 观塘区| 合作市| 咸丰县| 永安市| 德阳市| 梁山县| 呼图壁县| 神池县| 思南县| 桐城市| 图们市| 栾川县| 绥滨县| 鹤山市| 德州市| 定远县| 高清| 巴马| 蒙自县| 吉水县| 阿尔山市| 堆龙德庆县| 稷山县| 永平县| 凌海市| 长宁县| 弥勒县| 于田县| 泽库县| 蒙山县| 湾仔区| 白玉县| 三河市| 鄂伦春自治旗| 松潘县| 临泽县| 新泰市| 密云县| 筠连县| 金山区|