- Mastering Rust
- Rahul Sharma Vesa Kaihlavirta
- 266字
- 2021-07-02 13:35:23
Organizing tests
At a minimum, there are two kinds of tests that we usually write when developing software: unit tests and integration tests. They both serve different purposes and interact differently with the code base under test. Unit tests are always meant to be lightweight, testing individual components so that the developer can run them often, thus providing a shorter feedback loop, while integration tests are heavy and are meant to simulate real-world scenarios, making assertions based on their environment and specification. Rust's built-in testing framework provides us with sane defaults for writing and organizing these tests:
- Unit tests: Unit tests are usually written within the same module that contains the code to be tested. When these tests increase in number, they are organized into one entity as a nested module. One usually creates a child module within the current module, names it tests (by convention) with an annotation of the #[cfg(test)] attribute over it, and puts all the test-related functions inside of it. This attribute simply tells the compiler to include code within the tests module, but only when cargo test is run. More on attributes in a moment.
- Integration tests: Integration tests are written separately in a tests/ directory at the crate root. They are written as if the tests are the consumer of the crate being tested. Any .rs file within the tests/ directory can add a use declaration to bring in any public API that needs to be tested.
To write any of the aforementioned tests, there are some testing primitives we need to be familiar with.
推薦閱讀
- Visual C++程序設計學習筆記
- Learning PostgreSQL
- Full-Stack Vue.js 2 and Laravel 5
- C語言從入門到精通(第4版)
- C++程序設計基礎教程
- Java程序設計
- Hands-On Natural Language Processing with Python
- 深入理解Elasticsearch(原書第3版)
- Learning Probabilistic Graphical Models in R
- Mastering Akka
- Julia 1.0 Programming Complete Reference Guide
- Kubernetes進階實戰(zhàn)
- 深度學習原理與PyTorch實戰(zhàn)(第2版)
- Extending Unity with Editor Scripting
- 百萬在線:大型游戲服務端開發(fā)