Bypass Matplotlib plotting on Travis CI

less than 1 minute read

It’s useful to not plot on Travis-CI in most cases, because Travis doesn’t have an X11 desktop. There are two main ways to accompish this:

  • if statement(s) in the Python code, totally avoiding generating plots
  • generate plots, but use xvfb dummy X11 desktop

The xvfb method is a more complete test of your code and is easier to implement.

Xvfb

This method is more robust and recommended.

Add to your .travis.yml:

# Fake X11 server
before_script:
  - "export DISPLAY=:99.0"
  - "sh -e /etc/init.d/xvfb start"
  - sleep 3 # give xvfb some time to start

Detect Travis-CI inside Python

Put self-test Python code that plots AND the matplotlib import under this if statement:

import os

def test_myfun():
    if not 'TRAVIS' in os.environ or not os.environ['TRAVIS']:
        from matplotlib.pyplot import figure,show
        
        ... disabled plotting code on Travis CI

Notes

Leave a Comment