Solidity Testing – How to Unit Test Contract Private and Internal Functions

soliditytestingtruffle

How can we unit test contract private functions using the Truffle framework?

Since Solidity doesn't have package access or reflection language features, standard private testing approaches don't work.

Creating a test contract which inherits from the contract we're testing seems to be the only approach that works. However these tests would run on the blockchain.

Is there a way to unit test private functions from Javascript?

Best Answer

If they are internal functions you can inherit them and test them, if they are private functions, I believe the only way to unit test them is to make them public/internal, test them and then change them back to private once the tests are passing.

Another way to go about it would be to refactor your code so that the private functions are part of a library that you import into the contract. That way the functions are public in the library but not exposed in the actual contract.

Related Topic