在 Rust 中运行集成测试时如何查看日志记录(特别是使用 NEAR Workspaces-rs)

2024-01-10

我在用着https://github.com/near/workspaces-rs/ https://github.com/near/workspaces-rs/我的函数中有一些行,例如log!("Removed {} from {}", &key, &recipient);

(using use near_sdk::{env, log};)

但是当我运行集成测试时,这些日志消息不会出现在终端中。

如何初始化集成测试的记录器? https://stackoverflow.com/questions/30177845/how-to-initialize-the-logger-for-integration-tests#comment129424339_55268274向我指出https://docs.rs/env_logger/0.9.0/env_logger/index.html#capturing-logs-in-tests https://docs.rs/env_logger/0.9.0/env_logger/index.html#capturing-logs-in-tests这给出了这个例子:

#[cfg(test)]
mod tests {
    fn init() {
        let _ = env_logger::builder().is_test(true).try_init();
    }

    #[test]
    fn it_works() {
        init();

        info!("This record will be captured by `cargo test`");

        assert_eq!(2, 1 + 1);
    }
}

但即使我创建并调用它init()功能并取代我的log!()打电话给info!(),我什么也没得到。

(我可以看到测试本身的日志输出,但看不到主实现代码中的日志记录。)


日志将显示在结果中.call()并且可以从receipts返回的数据结构中的元素(receipts -> ExecutionOutcome -> logs).

为了证明这是有效的,我在项目目录中创建了一个新项目npx create-near-app它带有 TypeScript 中的基本 get-set 字符串约定。我删除了integration-tests文件夹,因为实现是在 JS/TS 中,并在其中创建了一个新的测试文件夹,其结构与此处看到的类似:https://github.com/near-examples/NFT/tree/master/integration-tests/rs https://github.com/near-examples/NFT/tree/master/integration-tests/rs

以下是我修改的文件:https://gist.github.com/idea404/5ecbcfaa2b1e41b9e33df15dfdaa0dae https://gist.github.com/idea404/5ecbcfaa2b1e41b9e33df15dfdaa0dae

运行测试后npm test从项目的根目录,我得到以下输出:

cargo run --example test
   Compiling ryan-ex v0.1.0 (/Users/dennis/Code/ryan-ex/tests)
    Finished dev [unoptimized + debuginfo] target(s) in 1.24s
     Running `target/debug/examples/test`
starting test
set_greeting outcome: ExecutionFinalResult {
    total_gas_burnt: 10898323344200,
    transaction: ExecutionOutcome {
        block_hash: `7gFBcM8paTWx9FnDVhZvLdYwHAS5YEyGpQYp9Ve8cEq9`,
        logs: [],
        receipt_ids: [
            `4E4cw7uXM31xpifF1tiDgrfaBV8yTuvDa86ucZHDey2o`,
        ],
        gas_burnt: 2428003729558,
        tokens_burnt: 242800372955800000000,
        executor_id: AccountId(
            "dev-20221025122820-11314570895307",
        ),
        status: SuccessReceiptId(4E4cw7uXM31xpifF1tiDgrfaBV8yTuvDa86ucZHDey2o),
    },
    receipts: [
        ExecutionOutcome {
            block_hash: `7gFBcM8paTWx9FnDVhZvLdYwHAS5YEyGpQYp9Ve8cEq9`,
            logs: [
                "Saving greeting hello there",
            ],
            receipt_ids: [
                `ExWePuAjosnbJRQSXzWczfWmbqcu9RwRzQqTsjnssVdo`,
            ],
            gas_burnt: 8247137052142,
            tokens_burnt: 824713705214200000000,
            executor_id: AccountId(
                "dev-20221025122820-11314570895307",
            ),
            status: SuccessValue(``),
        },
        ExecutionOutcome {
            block_hash: `Ed1wmpFgMQEtFAs5RgBzVBmwvyc3Tow2JBHh2mmD9HiC`,
            logs: [],
            receipt_ids: [],
            gas_burnt: 223182562500,
            tokens_burnt: 0,
            executor_id: AccountId(
                "dev-20221025122820-11314570895307",
            ),
            status: SuccessValue(``),
        },
    ],
    status: SuccessValue(``),
}
--------------
"hello there"
Dev Account ID: dev-20221025122820-11314570895307
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Rust 中运行集成测试时如何查看日志记录(特别是使用 NEAR Workspaces-rs) 的相关文章

随机推荐