- Mastering Rust
- Rahul Sharma Vesa Kaihlavirta
- 263字
- 2021-07-02 13:35:25
First integration test
As we stated previously, Rust expects all integration tests to live in the tests/ directory. Files within the tests/ directory are compiled as if they are separate binary crates while using our library under test. For the following example, we'll create a new crate by running cargo new integration_test --lib, with the same function, sum ,as in the previous unit test, but now we have added a tests/ directory, which has an integration test function defined as follows:
// integration_test/tests/sum.rs
use integration_test::sum;
#[test]
fn sum_test() {
assert_eq!(sum(6, 8), 14);
}
We first bring the function sum in scope. Second, we have a function, sum_test , that calls sum and asserts on the return value. When we try to run cargo test, we are presented with the following error:

This error seems reasonable. We want the users of our crate to use the sum function, but in our crate we have it defined as a private function by default. So, after adding the pub modifier before the sum function and running cargo test, our test is green again:

Here's a view of the directory tree of our integration_test example crate:
.
├── Cargo.lock
├── Cargo.toml
├── src
│ └── lib.rs
└── tests
└── sum.rs
As an example of an integration test, this was very trivial, but the gist of it is that when we write integration tests, we use the crate that's being tested, like any other user of a library would use it.
- 零基礎PHP學習筆記
- Instant QlikView 11 Application Development
- 機械工程師Python編程:入門、實戰(zhàn)與進階
- Mastering Unity Shaders and Effects
- HTML5+CSS3+JavaScript Web開發(fā)案例教程(在線實訓版)
- Visual FoxPro程序設計習題集及實驗指導(第四版)
- Python大學實用教程
- 創(chuàng)意UI:Photoshop玩轉(zhuǎn)APP設計
- Python硬件編程實戰(zhàn)
- 你真的會寫代碼嗎
- HTML5 Canvas核心技術:圖形、動畫與游戲開發(fā)
- Visual FoxPro數(shù)據(jù)庫程序設計
- 流暢的Python
- 開發(fā)者測試
- Mastering Swift 4(Fourth Edition)