Bypass Matplotlib plotting during continuous integration

It’s useful to not plot on continuous integration systems such as Travis CI, AppVeyor, GitLab, etc. In many cases the CI doesn’t have an X11 desktop. There are two main ways to accomplish this:

  • Pytest conditional tests detect CI via environment variable, totally avoiding generating plots
  • generate plots, but use xvfb dummy X11 desktop


This method is robust and works in general for Linux.

Add to your .travis.yml:

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

Detect CI inside Python

Pytest handles conditional tests well.

import os
import pytest

CI = bool(os.environ['CI']) if 'CI' in os.environ else False

@pytest.mark.skipif(CI, reason="CI's don't plot well")
def test_myfun():
    from matplotlib.pyplot import figure,show



CI Environment variables

These CI’s and more set the environment variable CI as a de facto standard for easy CI detection. Here are a few listings of CI system environment variables for reference:

Tags: , ,


Written by Michael Hirsch, Ph.D. //