Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print values in tests?

Tags:

substrate

I have following code in 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(())
}

This is my test:

#[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());
    });
}

I want to print the debug::info in terminal while running cargo test.

cargo test -- --nocapture

Is it possible?

Edit:
println! is working if you test only using template crate, so one can remove the println! while building the substrate runtime.

like image 769
Amiya Behera Avatar asked Sep 12 '25 16:09

Amiya Behera


1 Answers

To use println! you've to make sure your test is 'std'. Something like:

#[cfg(std)]
mod tests;

// ---

// But some tests are no std
mod tests;

If you want to use info! try RUST_LOG=info cargo t -- --nocapture

like image 120
AurevoirXavier Avatar answered Sep 14 '25 20:09

AurevoirXavier