可以在集成测试代码的单位测试中进行单元测试中的呼叫功能吗?

发布于 2025-02-13 19:12:45 字数 3174 浏览 0 评论 0原文

单位测试和集成测试中常用的代码。为了在设备测试中公开函数,将Pub关键字添加到模块和功能中。但是,当在集成测试中调用函数时,会发生以下错误。

错误,

>> cargo test tls_get_with_no_body
   
error[E0433]: failed to resolve: could not find `tests` in `register`
  --> tests/server.rs:28:34
   |
28 |             .json_body(register::tests::get_sample_register_response_body());
   |                                  ^^^^^ could not find `tests` in `register`

我的文件结构大致如下:

 engine
     ├── src
     │   ├── admin
     │   │   ├── register.rs // contains unit test
     ├── tests
     │   ├── server.rs // for integration test

下面的测试代码。

/src/admin/register.rs (unit test)

...
#[cfg(test)]
pub mod tests {
    use super::*;
    use httpmock::prelude::*;

    ...
     #[tokio::test(flavor = "multi_thread")]
    async fn register_success() {
        let mock_server = MockServer::start();
        let m = mock_server.mock(|when, then| {
            when.path("/register")
                .header("content-type", "application/json")
                .header_exists("content-type")
                .json_body_partial(
                    r#"
                    {  
                        "engineName": "engine_for_mock"
                    }
                    "#,
                );
            then.status(200)
                .header("content-type", "application/json")
                .json_body(get_sample_register_response_body());
        });
        ....
        assert_eq!(result.unwrap().id, "123b78dd5b504a32ad5f0456");
    }

    pub fn get_sample_register_response_body() -> serde_json::Value {
        let sample = serde_json::json!(
            {
                "id": "123b78dd5b504a32ad5f0456",
                "config":
                {   "threads":"CPU * 2",
                    "listenHttpPort":"5582",
                        "listenHttps":
                        {   "port":"",
                            "certificateFileName":"",
                            "certificateFileData":"",
                            "privateKeyFileName":"",
                            "privateKeyFileData":"",
                            "password":"",
                            "_id":"61c200c329d74b196d48c2a3"
                        },
                    "accessLogFormat":"%h %t \"%r\" %s %b %D %{X-Forwarded-For}i",
                    "systemLogLevel":"Info",
                    "_id":"61c200c329d74b196d48c2a2"
                }
            }
        );
        sample
    }
}

相同用途get_sample_register response_body_body()在集成测试中。

/tests/server.rs(integration test)

use engine::admin::{poll, register};
...
#[tokio::test(flavor = "multi_thread")]
async fn tls_get_with_no_body() {
  ...
    let admin_server = MockServer::start();
    let register_mock = admin_server.mock(|when, then| {
            when.path("/register");
            then.status(200)
                .header("content-type", "application/json")
                .json_body(register::tests::get_sample_register_response_body());// error
    });

}

在编写代码时,IDE不会生成错误,并且可以很好地找到路径。但是,当我运行测试时,会发生错误。测试模块不能公开吗?

There is a code commonly used in unit test and integration test. In order to expose the function on the unit test, the pub keyword is added to the module and function. However, the following error occurs when the function is called in the integration test.

Error

>> cargo test tls_get_with_no_body
   
error[E0433]: failed to resolve: could not find `tests` in `register`
  --> tests/server.rs:28:34
   |
28 |             .json_body(register::tests::get_sample_register_response_body());
   |                                  ^^^^^ could not find `tests` in `register`

My file structure is roughly as follows:

 engine
     ├── src
     │   ├── admin
     │   │   ├── register.rs // contains unit test
     ├── tests
     │   ├── server.rs // for integration test

And test code is below.

/src/admin/register.rs (unit test)

...
#[cfg(test)]
pub mod tests {
    use super::*;
    use httpmock::prelude::*;

    ...
     #[tokio::test(flavor = "multi_thread")]
    async fn register_success() {
        let mock_server = MockServer::start();
        let m = mock_server.mock(|when, then| {
            when.path("/register")
                .header("content-type", "application/json")
                .header_exists("content-type")
                .json_body_partial(
                    r#"
                    {  
                        "engineName": "engine_for_mock"
                    }
                    "#,
                );
            then.status(200)
                .header("content-type", "application/json")
                .json_body(get_sample_register_response_body());
        });
        ....
        assert_eq!(result.unwrap().id, "123b78dd5b504a32ad5f0456");
    }

    pub fn get_sample_register_response_body() -> serde_json::Value {
        let sample = serde_json::json!(
            {
                "id": "123b78dd5b504a32ad5f0456",
                "config":
                {   "threads":"CPU * 2",
                    "listenHttpPort":"5582",
                        "listenHttps":
                        {   "port":"",
                            "certificateFileName":"",
                            "certificateFileData":"",
                            "privateKeyFileName":"",
                            "privateKeyFileData":"",
                            "password":"",
                            "_id":"61c200c329d74b196d48c2a3"
                        },
                    "accessLogFormat":"%h %t \"%r\" %s %b %D %{X-Forwarded-For}i",
                    "systemLogLevel":"Info",
                    "_id":"61c200c329d74b196d48c2a2"
                }
            }
        );
        sample
    }
}

Same use get_sample_register response_body() in integration test.

/tests/server.rs(integration test)

use engine::admin::{poll, register};
...
#[tokio::test(flavor = "multi_thread")]
async fn tls_get_with_no_body() {
  ...
    let admin_server = MockServer::start();
    let register_mock = admin_server.mock(|when, then| {
            when.path("/register");
            then.status(200)
                .header("content-type", "application/json")
                .json_body(register::tests::get_sample_register_response_body());// error
    });

}

When writing code, the IDE does not generate an error and finds the path well. But when I run the test, an error occurs. Can't the test module be made public?

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

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

发布评论

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

评论(1

秋意浓 2025-02-20 19:12:45

CFG(test)仅在编译当前板条箱中的测试时才能启用。 tests/中的“集成”测试是单独的板条箱,因此,运行时,它们使用您的库汇编通常,而没有特殊test> TEST配置。

粗略地说,#[CFG(test)]仅对#[test]在同一箱子中的测试有用。在其他每种情况下,都不会编译带有##[CFG(test)]的项目。

cfg(test) is only enabled when tests in the current crate are being compiled. "Integration" tests in tests/ are separate crates, so when run, they use your library compiled normally, without the special test configuration.

Roughly speaking, #[cfg(test)] is only useful to #[test] tests in the same crate. In every other case, items with #[cfg(test)] will not be compiled.

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