We have some custom pallet unit tests that use multiple mock runtimes that are configured slightly differently. The problem is that when we have to make changes to the runtime, we have to apply those changes to all of them. Furthermore, we'd like to have a set of common functions that all of the unit tests can use. If Rust had inheritance, the solution would be an abstract base runtime and subclasses for each runtime but Rust doesn't have it. The solution likely involves creative use of modules and macros but what's the recommended approach?

I'd like to avoid making changes to the Cargo.toml. I'd like to have some sort of macro level attribute that I could set in each runtime: "runtime1" and "runtime2" and then conditionally compile which value is used based on that attribute.

In our case, we have two runtimes that differ only by the constant values for two of the associated types.


type MaxMessagesStored = ConstU32<25>;  // runtime1


type MaxMessagesStored = ConstU32<1000>;  // runtime2

So, I'd like something like this. This code is for example and does NOT work:

#![cfg_attr(RUNTIME, define = "runtime1")]

    #[cfg(RUNTIME = "runtime1")]
    type MaxMessagesStored = ConstU32<25>;
    #[cfg(RUNTIME = "runtime2")]
    type MaxMessagesStored = ConstU32<1000>;

In C, this would be:

    #define RUNTIME "runtime1"
    #if defined(RUNTIME) && (RUNTIME == "runtime1")
    // code 
    // code

Alternatively, is there a way to change the value at runtime? I assume not because it's a constant but have to ask.

1 Answer 1


I think the best practice is using macro.

Sometimes, we need to speed up the session time for the development runtimes.

So, we have a fast-runtime macro.

Check how fast-runtime works: https://github.com/paritytech/polkadot/blob/a8e6828115f852dc08a90c9fc0698f59a589b977/runtime/common/src/lib.rs#L247-L275

You can simply replace the fast-runtime with something like multi-runtimes and make accepting more args.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.