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

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?

主站蜘蛛池模板: 凤庆县| 宝兴县| 庆安县| 修水县| 濮阳县| 盐城市| 喀什市| 微山县| 平山县| 开江县| 池州市| 城固县| 林甸县| 陇西县| 襄城县| 福泉市| 鄂托克旗| 岚皋县| 临澧县| 新竹市| 隆子县| 渝北区| 策勒县| 外汇| 竹北市| 榆社县| 五原县| 公主岭市| 砀山县| 兰溪市| 平遥县| 隆昌县| 孝昌县| 汝南县| 宜兴市| 滦南县| 平湖市| 汉中市| 巴东县| 绥芬河市| 永平县|