如何生成#[CFG(TEST)]背后模块的文档?

发布于 2025-01-31 22:38:17 字数 236 浏览 4 评论 0原文

在一个生锈项目中,有一个带有实用程序支持测试的模块,包装在模块test_utils中:

#[cfg(test)]
pub mod test_utils;

是否有一种方法来制作货物doc也生成的文档test_utils模块和内部的东西?

In a Rust project there is a module with utilities to support testing, packed in a module test_utils:

#[cfg(test)]
pub mod test_utils;

Is there a way to make cargo doc generate also the documentation for test_utils module and the things inside?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柳絮泡泡 2025-02-07 22:38:17

可能有多种用于测试文档的方法,但是我认为更容易的方法是使用cargo Rustdoc生成文档,并传递-CFG Test通过:

cargo rustdoc -- --cfg test

There are probably multiple ways of generating documentation for tests, but I think the easier approach is to generate the documentation with cargo rustdoc and pass the --cfg test flag through:

cargo rustdoc -- --cfg test
初见终念 2025-02-07 22:38:17

我刚刚发现这也有效:

#[cfg(any(test, doc))]
pub mod test_utils;

虽然文档是常规的

cargo doc

I just found that this works too:

#[cfg(any(test, doc))]
pub mod test_utils;

While documentation is generated with regular

cargo doc
茶色山野 2025-02-07 22:38:17

我认为#[CFG(ANY(test,doc))]可能还不够,它将在没有功能信息的情况下生成DOC。

/// The module should work
#[cfg(any(test, doc))]
pub mod test_utils {

    /// The function about test1
    #[test]
    fn test1() {
        assert!(true);
    }
}

为了与实用程序进行文档支持测试,也许[CFG_ATTR(非(doc),test)]是必要的。

/// The module should work
#[cfg(any(test, doc))]
pub mod test_utils {

    /// The function about test1
    #[cfg_attr(not(doc), test)]
    fn test1() {
        assert!(true);
    }
}

添加之后[CFG_ATTR(非(doc),test)]上面的测试用例:

“在此处输入图像说明”

I think #[cfg(any(test, doc))] is probably not enough, it will generate doc with no function information.

/// The module should work
#[cfg(any(test, doc))]
pub mod test_utils {

    /// The function about test1
    #[test]
    fn test1() {
        assert!(true);
    }
}

enter image description here

In order to doc with utilities to support testing, perhaps [cfg_attr(not(doc), test)] is necessary.

/// The module should work
#[cfg(any(test, doc))]
pub mod test_utils {

    /// The function about test1
    #[cfg_attr(not(doc), test)]
    fn test1() {
        assert!(true);
    }
}

After added [cfg_attr(not(doc), test)] above test cases:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文