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

  • Mastering Rust
  • Rahul Sharma Vesa Kaihlavirta
  • 444字
  • 2021-07-02 13:35:23

Motivation for testing

"Things that are impossible just take longer."                                                                                                                                                                                                  -  Ian Hickson

Software systems are like machines with small cogs and gears. If any of the individual gears malfunctions, the machine as a whole is most likely to behave in an unreliable manner. In software, the individual gears are functions, modules, or any libraries that you use. Functional testing of the individual components of a software system is an effective and practical way of maintaining high quality code. It doesn't prove that bugs don't exist, but it helps in building confidence when deploying the code to production and maintaining the sanity of the code base when the project is to be maintained for a long time. Furthermore, large-scale refactoring in software is hard to do without unit tests. The benefits of the smart and balanced use of unit testing in software are profound. During the implementation phase, a well-written unit test becomes an informal specification for components of the software. In the maintenance phase, the existing unit tests serve as a harness against regressions in the code base, encouraging an immediate fix. In compiled languages like Rust, this gets even better as the refactors involved (if any) for regressions from unit tests are more guided due to helpful error diagnostics from the compiler.

Another good side effect of unit tests is that they encourage the programmer to write modular code that is mostly dependent on the input parameters, that is, stateless functions. It moves the programmer away from writing code that depends on a global mutable state. Writing tests that depend on a global mutable state are hard to write. Moreover, the act of simply thinking about writing tests for a piece of code helps the programmer figure out silly mistakes in their implementation. They also act as very good documentation for any newcomer trying to understand how different parts of the code base interact with each other.

The takeaway is that tests are indispensable for any software project. Now, let's look at how we can write tests in Rust, starting by learning about organizing tests!

主站蜘蛛池模板: 阿鲁科尔沁旗| 海原县| 杭州市| 汤阴县| 汽车| 常德市| 桐庐县| 隆安县| 门头沟区| 澄城县| 开化县| 莱州市| 石泉县| 桃园市| 磐安县| 荣昌县| 斗六市| 肃宁县| 合肥市| 沂源县| 无锡市| 玉山县| 珠海市| 泸定县| 房产| 榆社县| 泰顺县| 宽城| 丹巴县| 当雄县| 筠连县| 平乐县| 博爱县| 建始县| 吉安县| 无锡市| 拜城县| 蒙自县| 西林县| 霍林郭勒市| 彩票|