I have a large amount of contracts which I'm testing after applying changes. I'd like to add solidity functions which will be compiled only for testing, but not on mainnet nor ropsten. Is this even possible?

The reason I'm doing this is that I need to write a lot of JS code with adding votings, voting out from accounts and some more to get tokens which I would like to avoid and simply add a single test-only function like testOnlyMint(uint pAmount).


Something you can do is create a new contract called something like TestOnlyContract, have it inherit from the base contract and then add the necessary test only functions.


Excellent question. Out of the box, there is no preprocess for Solidity where you could transform your program at precompile-time similar to C macros (#define, #ifdef, #endif, ...). So it will be difficult to include/exclude testcode depending on your build target.

There is an issue in solidity's repository, addressing the preprocessor feature, but it doesn't seem to be active. https://github.com/ethereum/solidity/issues/10

Moreover, there are custom solutions for this like https://github.com/Lamarkaz/psol, but I have no idea how stable they are and how easy it is to integrate them into truffle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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