smart contracts - solidity and travis logos

Test Solidity Smart Contracts Using Travis CI

Reading Time: 2 minutes

  •  
  • 1
  •  
  • 29
  •  
  •  
  •  
    30
    Shares

What is Travis CI?

Our smart contract development team is using Travis CI to integrate automated testing into GitHub repositories. This guide for this great tool will not go into detail about Travis CI itself (you can refer to Travis Get Started Guide if you’re unfamiliar with it), but rather explain how to automate tests for Solidity smart contracts. We will be using Truffle for running and building tests and Ganache to set up the network since Travis has built-in support for JavaScript and Node.js.

Travis CI for smart contracts

  1. If you still haven’t given Travis CI access to your repo, follow this guide. Otherwise, just go to the next step.
  2. Create a file in your GitHub repo called .travis.yml. The purpose of this file is to detail which steps should be executed at each step of the build; if it’s not in your repo, Travis CI won’t trigger the build process. Your .travis.yml file should look like this:

    You may have noticed that many of the commands here are specifically for Debian-based distros. Don’t worry if you’re using another distro or a different OS altogether — all of these commands will run on a VM provided by Travis CI (more info here). ./bin/travis-install.sh and ./bin/test.sh are just the scripts you’ll use for installing any necessary modules or dependencies and testing your smart contracts, respectively. As we will see in the following steps, you can name them whatever you want as long as they execute some specific commands and they’re properly referenced in .travis.yml.
  3. Create a file called ./bin/travis-install.sh. In our example, we put shell scripts in ./bin/ to keep the repo organized, but it isn’t strictly necessary. This is what it should look like:

    This will install all the Node packages you need for setting up an Ethereum network and running your tests.
  4. Create another file called ./bin/test.sh. This is what will actually run the tests.
  5. Make sure to replace <yourLimit> with whatever you’d like to set as the gas limit for the network. If you only want to execute some specific test cases, you can just replace truffle test with truffle test the same way you would if you were executing them in the console.
  6. Now everything should be set for pushing your changes!
  7. Optionally, add a Travis status badge to your repo’s README.md.

You can now easily see if your smart contracts are working just by pushing changes to Git. No more manual testing before updating your remote repo — Travis CI will let you know if your project is working properly.


  •  
  • 1
  •  
  • 29
  •  
  •  
  •  
    30
    Shares