知识卡片
属性标记单元测试
在测试函数上写 #[test],测试构建会发现并执行这类函数。用 assert_eq! 把输入和期望结果固定成可重复检查,例如 gcd(14, 15) 应为 1。属性是附在代码声明上的额外信息,有助于让测试集合跟着源码一起维护。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- A Tour of Rust 让 Cargo 发现并运行测试 · 查看原书依据
The #[test] atop the definition marks test_gcd as a test function, to be skipped in normal compilations, but included and called automatically if
参考来源
- 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第01章《A Tour of Rust》|让 Cargo 发现并运行测试,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch01.xhtml#writing-and-running-unit-tests`
- 结论依据:测试示例说明 #[test] 的构建行为和 cargo test 的发现机制。
- 原始内容:The #[test] atop the definition marks test_gcd as a test function, to be skipped in normal compilations, but included and called automatically if