Solidity – Accessing Test Accounts Created by TestRPC with Truffle

soliditytestingtestrpctruffle

With Javascript tests, it is possible to access the accounts which TestRPC creates using:

contract('MetaCoin', function(accounts) {
   // tests here can access accounts[n] to get the public keys
}

But how can I use those addresses from a test written in Solidity?

Best Answer

I'd like to make a suggestion.

In the TestMetaCoin.sol example, you can see that the contract owner address is used. (See tx.origin) Therefore, I see no reason why you couldn't hard-code other account addresses in a Solidity library file and then import the library into your test. You might be then be able to use $ testrpc --account="<privatekey>,balance" [--account="<privatekey>,balance"] to ensure that the addresses in your library are re-used each time you launch.

You can do some fairly complicated things with Solidity tests. (See http://truffleframework.com/tutorials/testing-for-throws-in-solidity-tests) However, judging by the truffle docs, I get the impression that they may not be designed for your use case.

http://truffleframework.com/docs/getting_started/solidity-tests