2

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).

3

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.

| improve this answer | |
  • Do you know if there is a way to create this test-only contract in a separate source directory and have it built in a separate build directory? (So that we can avoid accidentally deploying such contract in production) – Waterlink Mar 27 at 17:48
  • 1
    Do your deployment scripts automatically deploy all contracts? If you use e.g. Truffle's migration, you have to explicitly deploy every contract, so you won't accidentally deploy a contract that you don't want to deploy. Regardless of the directory. – Rosco Kalis Mar 31 at 12:39
  • We do use migrations. I’m just curious, how I can prevent a junior developer in the future from noticing this contract in the main directory and adding it in the new migration file. Of course, code review is useful for that, but I’m looking for a way to make that just not possible. Something like a different package or module, that’s just not available to deployer in the migration. – Waterlink Apr 2 at 18:09
1

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.

| improve this answer | |

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.