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

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?

主站蜘蛛池模板: 巍山| 蓬溪县| 塘沽区| 磐石市| 磴口县| 遵化市| 西贡区| 兴山县| 贵德县| 永兴县| 玉龙| 万安县| 黄骅市| 潮安县| 盘山县| 北海市| 玉门市| 闽侯县| 多伦县| 镇赉县| 孟津县| 彭州市| 渝北区| 平塘县| 新竹市| 惠安县| 平江县| 威远县| 尼玛县| 绿春县| 清镇市| 浪卡子县| 石河子市| 清流县| 南平市| 土默特左旗| 肥城市| 临澧县| 尉氏县| 新巴尔虎右旗| 商河县|