Travis CI Test Coverage

📘

Examples

For more example scripts, head to the test reporter repo on GitHub.

Travis CI - single test suite, non-parallel builds

# single test suite, non-parallel build.

env:
  global:
    - CC_TEST_REPORTER_ID=ABC123
language: ruby
rvm:
  - 2.2.0
before_script:
  - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
  - chmod +x ./cc-test-reporter
  - ./cc-test-reporter before-build
script:
  - bundle exec rspec
after_script:
  - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT

Travis CI - multiple test suites

Example 1

# multiple tests suites
# working example can be found at https://github.com/scottohara/loot

language: ruby

# Cache gems
cache:
  bundler: true
  directories:
    - node_modules

addons:
  chrome: stable
  #firefox: latest

env:
  global:
    CC_TEST_REPORTER_ID=ABC123

before_install:
  - nvm install                         # Install node version from .nvmrc
  - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
  - chmod +x ./cc-test-reporter
  #- export DISPLAY=:99.0                # Display number for xvfb (for headless browser testing)
  #- sh -e /etc/init.d/xvfb start        # Start xvfb (for headless browser testing)

install:
  - bundle install --without production --path=${BUNDLE_PATH:-vendor/bundle}  # Install ruby gems, excluding production only gems such as unicorn (already present by default in Travis)
  - npm install                         # Install npm dependencies
  #- npm install karma-firefox-launcher codeclimate-test-reporter

# Setup the database
before_script: bundle exec rake db:create db:migrate

# Run the test suites
script:
  - bundle exec rubocop -DESP           # Backend linting
  - bundle exec rake                    # Backend specs
  #- npm test -- --browsers Firefox      # Frontend specs
  - npm test                            # Frontend linting & specs

# Pipe the coverage data to Code Climate
after_script:
  - ./cc-test-reporter format-coverage -t simplecov -o coverage/codeclimate.backend.json coverage/backend/.resultset.json # Format backend coverage
  - ./cc-test-reporter format-coverage -t lcov -o coverage/codeclimate.frontend.json coverage/frontend/lcov.info  # Format frontend coverage
  - ./cc-test-reporter sum-coverage coverage/codeclimate.*.json -p 2                  # Sum both coverage parts into coverage/codeclimate.json
  - if [[ "$TRAVIS_TEST_RESULT" == 0 ]]; then ./cc-test-reporter upload-coverage; fi  # Upload coverage/codeclimate.json

Example 2

# parallel tests

language: ruby
rvm: "2.5.1"
sudo: true
filter_secrets: false

before_install:
  - 'curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"'
  - 'unzip awscli-bundle.zip'
  - './awscli-bundle/install -b ~/bin/aws'
  - 'export PATH=~/bin:$PATH'
  - gem update --system
  - gem install bundler -v 1.16.1
  - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
  - chmod +x ./cc-test-reporter

before_script:
  - bin/setup
  - ./cc-test-reporter before-build

script:
  - "bundle exec rake knapsack:rspec"
  - "bundle exec rake assets:precompile"

branches:
  only:
    - develop
    - master

cache:
  bundler: true
  yarn: true
  directories:
    - node_modules

env:
  global:
    - CC_TEST_REPORTER_ID=id
    - CI_NODE_TOTAL=5
    - AWS_ACCESS_KEY_ID=foo
    - AWS_SECRET_ACCESS_KEY=bar
    - AWS_DEFAULT_REGION=us-east-1
  matrix:
    - CI_NODE_INDEX=0
    - CI_NODE_INDEX=1
    - CI_NODE_INDEX=2
    - CI_NODE_INDEX=3
    - CI_NODE_INDEX=4

addons:
    postgresql: "9.6"
    elasticsearch: "5.x"
    chrome: stable

services:
  - postgresql
  - redis-server
  - elasticsearch

# Pipe the coverage data to Code Climate
after_script:
  - if [[ "$TRAVIS_TEST_RESULT" == 0 ]]; then ./cc-test-reporter format-coverage -t simplecov -o ./coverage/codeclimate.$CI_NODE_INDEX.json ./coverage/spec/.resultset.json; fi
  - if [[ "$TRAVIS_TEST_RESULT" == 0 ]]; then aws s3 sync coverage/ "s3://s3-bucket/coverage/$TRAVIS_BUILD_NUMBER"; fi
  - if [[ "$TRAVIS_TEST_RESULT" == 0 ]]; then aws s3 sync "s3://s3-bucket/coverage/$TRAVIS_BUILD_NUMBER" coverage/; fi
  - if [[ "$TRAVIS_TEST_RESULT" == 0 ]]; then ./cc-test-reporter sum-coverage --output - --parts $CI_NODE_TOTAL coverage/codeclimate.*.json | ./cc-test-reporter upload-coverage --input -; fi