Skip links
smart contracts - solidity and travis logos

Test Solidity Smart Contracts Using Travis CI

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:
    dist: xenial
    sudo: required
    language: node_js
      - "9.4.0"
        - os: linux
          dist: xenial
          - sudo add-apt-repository ppa:ethereum/ethereum -y
          - sudo apt-get update
          - sudo apt-get install build-essential -y
          - sudo apt-get install gcc g++ libssl-dev libudev-dev pkg-config
          - curl -sL | sudo -E bash -
          - sudo apt-get install -y nodejs
          - rm -rf node_modules
      - bash ./bin/
      - bash ./bin/

    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/ and ./bin/ 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/ 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:
    npm install
    npm install -g ganache-cli truffle

    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/ This is what will actually run the tests.
    set -e
    ganache-cli --gasLimit <yourLimit> 2> /dev/null 1> /dev/null &
    sleep 5 # to make sure ganache-cli is up and running before compiling
    rm -rf build
    truffle compile
    truffle migrate --reset --network development
    truffle test
    kill -9 $(lsof -t -i:8545)
  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

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.