如何编写内部API的文档测试? [英] How to write documentation tests for an internal API?

查看:94
本文介绍了如何编写内部API的文档测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个包含私有结构和方法的库:

I'm writing a library which contains private structs and methods:

/// Constructs a new `Object`
///
/// Internal API
///
/// # Example
/// ```rust
/// use lib::object::Object;
///
/// let tn = Object::new();
/// ```

当我运行货物测试时,文档测试失败,因为 Object 是私有结构。

When I run cargo test, the doctest fails because Object is a private struct.

是否有可能使它编译并运行

推荐答案

如果您想要测试以编译和运行,请参见这个相关问题

I don't think it is possible if you want the test to compile and run, see this related question.

我只想将代码作为示例包含在文档中,而无需尝试编译和运行它,则可以排除通过添加 ignore 标志从测试中删除:

I you only want to include the code as a sample in the documentation, without trying to compile and run it, you can exclude it from the tests by adding the ignore flag:

/// ```rust,ignore
/// use lib::object::Object;
///
/// let tn = Object::new();
/// ```

这篇关于如何编写内部API的文档测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆