今天是学习rust的第四天,学习材料为官网的《Rust Programming Language》,本笔记的主要内容为第11章:Writing Automated Tests。
今日学习的第九章、第十章笔记在这里:
https://blog.csdn.net/weixin_38243001/article/details/104373832
Chapter 11, Writing Automated Tests
Program testing can be a very effective way to show the presence of bugs, but it is hopelessly inadequate for showing their absence. ——Edsger W. Dijkstra
11.1 How to write Tests
test 函数的功能基本包括以下三点:
- 设置任何需要的数据和状态
- 运行你想要测试的代码
- 判断你获得的结果是希望获得的
The Anatomy of a Test Function
在函数fn标识符前加上#[test]
将函数编程test function。当给出cargo test
命令时,rust构建一个test runner binary,利用标有test的函数进行测试并返回成功与否。
使用cargo命令构建新的library project时&#