Using Dapple to test Solidity with Solidity
Posted October 20, 2015 by Jonathan Brown ‐ 3 min read
Originally published on jonathanpatrick.me. Retrieved from the Wayback Machine.
As the Ethereum eco-system is still very immature it can sometimes be very frustrating to develop on this platform. Gradually more tools are being developed that make it much easier, such as Browser Solidity.
Maker have released a new tool called Dapple. It's purpose is to provide a lot of scaffolding around dapp development. It comprises: package management, Solidity preprocessor (cog), Solidity testing, deployment and chain contexts.
I've been experimenting with its Solidity testing suite.
First of all you need to install Dapple.
git clone https://github.com/NexusDevelopment/dapple.git
cd dapple
sudo python setup.py install
The command `dapple` will now be available.
You now need to initialize your dapp as a dapple project:
dapple init
Then edit `.dapple/dappfile` and populate the name and version fields.
Now add the `.dapple` dir to your Git repo:
git add .dapple
git commit -m "Dapplize"
You can now start writing tests for your Solidity code. The great thing about Dapple tests is that they are written in Solidity just like the code you are testing.
Contracts that are tests need to inherit from the Dapple Test contract. For example:
import "core/test.sol";
contract MyContractTest is Test {
}
You can put your test contract in a separate file. You will then need to import the contract you are testing:
import "my_contract.sol";
Every function in the test contract that starts with `test` will be executed in a fresh environment. The function `setUp` will be called before each test is invoked.
Typically you need to instantiate the contract that you are testing:
MyContract myContractInstance = new MyContract();
You can then make calls into the contract and use Dapple's suite of assert functions to ensure that it is operating correctly.
I have created a pastebin contract called Etherbin and written Dapple tests for it: https://github.com/bluedroplet/etherbin-api
The tests complete in just a couple of seconds. They are run entirely in the vm and the blockchain is not utilized at all.