Automated testing with Travis CI

Intermediate Perl shows the basics of Perl testing. We show you how to write test programs and how to run those from the command line. That’s the language-specific stuff and within the scope of the book. You can take it further though. You can set up your code in a “continuous integration” system that runs the tests whenever you commit change (most of these trigger on a source control commit). You commit your code and grab some more coffee while your tests trigger themselves. Martin Fowler has some interesting thoughts on CI and wikipedia has a comparison matrix of CI services.

Travis CI is such a system and it allows free accounts for open source work. It’s easy to login through GitHub and turn on automated testing for one of your GitHub repositories. You add a .travis.yml file to your project to control the testing. They have instructions for a very basic Perl setup

I do a little bit more. Here’s one that I took from test-file repo, but you should check it for the latest version of whatever I’m doing.

The perl key lets me specify which things I want to test. I leave off v5.8 because I also use Devel::Cover—that doesn’t work with v5.8. You can test with the versions that make sense for you (or leave this off and test with the latest version).

I install some helpers. The install phase happens before the tests start. All of these commands run in the top directory of the repository. I use cpanm to install the dependencies (while skipping tests). I install the stuff I need to run Devel::Cover and to report its results to Coveralls to collect historical test data.

Coveralls trends for Module::Release

The script section is the testing phase. The command comes from Devel::Cover. It deletes previous data (although this should be fresh) and then runs the tests.

sudo: false

language: perl
perl:
    - "5.10"
    - "5.12"
    - "5.14"
    - "5.16"
    - "5.18"
    - "5.20"
    - "5.22"

before_install:
    - git clone git://github.com/travis-perl/helpers ~/travis-perl-helpers
    - source ~/travis-perl-helpers/init --auto

install:
    - cpanm --quiet --installdeps --notest .
    - cpanm --quiet --notest Devel::Cover
    - cpanm --quiet --notest Devel::Cover::Report::Coveralls

script:
    - cover -delete && cover -test

after_success:
    - cover -report coveralls