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

  • 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.

主站蜘蛛池模板: 金沙县| 庆城县| 普兰店市| 开远市| 长治县| 无锡市| 旬邑县| 焦作市| 循化| 安庆市| 塘沽区| 永泰县| 永昌县| 根河市| 苍南县| 乐山市| 林周县| 东乡| 西吉县| 丰台区| 山阴县| 景泰县| 乌恰县| 方城县| 陇川县| 日土县| 炉霍县| 边坝县| 上犹县| 卢湾区| 呼图壁县| 固原市| 武冈市| 临朐县| 吉木萨尔县| 普兰店市| 桑日县| 乌兰察布市| 改则县| 灌阳县| 西吉县|