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

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?

主站蜘蛛池模板: 英德市| 宜春市| 公安县| 布尔津县| 湾仔区| 黔南| 牡丹江市| 余干县| 云林县| 南部县| 莱芜市| 东乡族自治县| 尖扎县| 偃师市| 北海市| 卓资县| 叙永县| 高密市| 思茅市| 称多县| 五原县| 日照市| 罗江县| 和田县| 察雅县| 柞水县| 江陵县| 施秉县| 扎兰屯市| 从化市| 柳江县| 梁平县| 剑河县| 慈溪市| 客服| 白朗县| 安康市| 明光市| 静乐县| 隆德县| 天等县|