How to run Tox with Travis-CI

Cerin picture Cerin · May 4, 2016 · Viewed 7.8k times · Source

How do you test different Python versions with Tox from within Travis-CI?

I have a tox.ini:

[tox]
envlist = py{27,33,34,35}
recreate = True

[testenv]
basepython =
    py27: python2.7
    py33: python3.3
    py34: python3.4
    py35: python3.5
deps =
    -r{toxinidir}/pip-requirements.txt
    -r{toxinidir}/pip-requirements-test.txt
commands = py.test

which runs my Python unittests in several Python versions and works perfectly.

I want to setup a build in Travis-CI to automatically run this when I push changes to Github, so I have a .travis.yml:

language: python
python:
-   "2.7"
-   "3.3"
-   "3.4"
-   "3.5"
install:
-   pip install tox
script:
-   tox

This technically seems to work, but it redundantly runs all my tests in each version of Python...from each version of Python. So a build that takes 5 minutes now takes 45 minutes.

I tried removing the python list from my yaml file, so Travis will only run a single Python instance, but that causes my Python3.5 tests to fail because the 3.5 interpreter can't be found. Apparently, that's a known limitation as Travis-CI won't install Python3.5 unless you specify that exact version in your config...but it doesn't do that for the other versions.

Is there a way I can workaround this?

Answer

Gabe DeFelippis picture Gabe DeFelippis · May 6, 2016

For this I would consider using tox-travis. This is a plugin which allows use of Travis CI’s multiple python versions and Tox’s full configurability. To do this you will configure the .travis.yml file to test with Python:

sudo: false
language: python
python:
    - "2.7"
    - "3.4"
install: pip install tox-travis
script: tox

This will run the appropriate testenvs, which are any declared env with py27 or py34 as factors of the name by default. Py27 or py34 will be used as fallback if no environments match the given factor.

Further Reading