rust-tests-guidelines
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGuidelines for Writing Rust Tests
Rust测试编写指南
Guidelines for writing new tests for Rust code.
为Rust代码编写新测试的指南。
Guidelines
指南
- Test against the public API of the code under test.
- Test private APIs if and only if the private component is highly complex and difficult to test through the public API.
- Use whenever you are testing output that is difficult to predict or compare.
insta - Where appropriate, use to add property-based tests for key invariants.
proptest - Testing code should be written with the same care reserved to production code. Avoid unnecessary duplication, introduce helpers to reduce boilerplate and ensure readability. The intent of a test should be obvious or, if not possible, clearly documented.
- Do not reference exact line numbers in comments, as they may change over time.
- 针对被测代码的公开API进行测试。
- 仅当私有组件高度复杂且难以通过公开API测试时,才对私有API进行测试。
- 每当测试难以预测或比较的输出时,使用。
insta - 适当时,使用为关键不变量添加基于属性的测试。
proptest - 测试代码的编写应与生产代码同样用心。 避免不必要的重复,引入辅助工具以减少样板代码并确保可读性。 测试的意图应清晰明了,若无法做到,则需明确记录。
- 不要在注释中引用确切的行号,因为这些行号可能会随时间变化。
Code organization
代码组织
- Put tests under the directory of the relevant crate if they don't rely on private APIs.
tests - The directory should be organized as a crate, with a
testsfile and all tests in modules. Refer to themain.rsdirectory as an example.trie_rs/tests - If the test must rely on private APIs, co-locate them with the code they test, using a module.
#[cfg(test)]
- 如果测试不依赖私有API,请将其放在相关crate的目录下。
tests - 目录应作为一个crate进行组织,包含
tests文件和所有测试模块。 可参考main.rs目录作为示例。trie_rs/tests - 如果测试必须依赖私有API,请将它们与被测代码放在一起,使用模块。
#[cfg(test)]
Dealing with extern C symbols
处理外部C符号
Check out CONTRIBUTING.md for instructions on how to deal with
undefined C symbols in Rust tests.
有关如何在Rust测试中处理未定义C符号的说明,请查看CONTRIBUTING.md。