如何在 Rust 中测试私有方法? [英] How do I test private methods in Rust?
问题描述
如何在 Rust 中测试私有方法?我没有找到任何关于它的信息.文档中也没有信息.
How do I test private methods in Rust? I didn't find any information about it. There's no information in the documentation either.
推荐答案
当使用 #[test]
时,私有或公共方法没有什么特别之处——你只是编写完全正常的函数,可以访问他们可以访问的任何内容.
When using #[test]
, there’s nothing special about private or public methods—you’re just writing perfectly normal functions that can access anything they can access.
fn private_function() {
}
#[test]
fn test_private_function() {
private_function()
}
外部测试,例如 tests/*.rs
和 examples/*.rs
如果您使用 Cargo 或 doc 测试,则无法访问私有会员;他们也不应该:此类测试旨在成为公共 API 测试,而不是处理实现细节.
External tests, such as tests/*.rs
and examples/*.rs
if you’re using Cargo, or doc tests, do not get access to private members; nor should they: such tests are designed to be public API tests, not to be dealing with implementation details.
这篇关于如何在 Rust 中测试私有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!