For anyone using Foundry, the recommended advice for testing internal
functions is to inherit and expose them as external
ones.
This can be done directly in the .t.sol
file, so you don't have to worry about adding extra files / contracts to your project. See below for the example from the Foundry Book.
// file: src/MyContract.sol
contract MyContract {
function myInternalMethod() internal returns (uint) {
return 42;
}
}
// file: test/MyContract.t.sol
import {MyContract} from "src/MyContract.sol";
contract MyContractHarness is MyContract {
// Deploy this contract then call this method to test `myInternalMethod`.
function exposed_myInternalMethod() external returns (uint) {
return myInternalMethod();
}
}
In Foundry, there isn't a good way of testing private
functions yet. Besides renaming them as internal
, the Foundry book also suggests copying and pasting them into your .t.sol
file and running a script to ensure both functions implementations match.