微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在测试中打印值?

如何解决如何在测试中打印值?

我在 decl_module 中有以下代码

#[weight = 10_000 + T::DbWeight::get().reads_writes(1,1)]
pub fn create_deparment(origin,name: Vec<u8>,location: Vec<u8>,detail: Vec<u8>) -> dispatch::dispatchResult {
    let _who = ensure_signed(origin)?;
    let oldcount = DeparmentCount::get();
    debug::info!("Old Deparment Count: {:?}",oldcount);
    // print("Old Deparment Count");
    Ok(())
}

这是我的测试:

#[test]
fn create_deparment_test() {
    new_test_ext().execute_with(|| {
        let _result = TemplateModule::create_deparment(Origin::signed(1),"Education".as_bytes().to_vec(),"India".as_bytes().to_vec(),"hashcode".as_bytes().to_vec());
    });
}

我想在运行货物测试时在终端中打印 debug::info。

cargo test -- --nocapture

有可能吗?

编辑:
打印!如果您仅使用模板箱进行测试,则可以正常工作,因此可以删除 println!在构建基板运行时。

解决方法

要使用 println!,您必须确保您的测试是“std”。 类似的东西:

#[cfg(std)]
mod tests;

// ---

// But some tests are no std
mod tests;

如果您想使用 info!,请尝试 RUST_LOG=info cargo t -- --nocapture

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。