首页 > 解决方案 > 如何在测试中打印值?

问题描述

我在 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!在构建基板运行时。

标签: substrate

解决方案


要使用println!你必须确保你的测试是'std'。就像是:

#[cfg(std)]
mod tests;

// ---

// But some tests are no std
mod tests;

如果你想使用info!试试RUST_LOG=info cargo t -- --nocapture


推荐阅读