Guide
Using Travis CI Matrix Builds to Test Multiple Python Versions
Learn how to define a matrix in .travis.yml to run unit and integration tests across Python 3.8, 3.9, and 3.10 without duplicating job definitions.
Published by Tasadduq Burney
28 Oct 2025, 06:32 UTC
2 min118.2K views0

Quick answer: use a matrix to generate jobs
Add a matrix section to your .travis.yml file. Travis will expand each combination of the values you list (language versions, environment variables, etc.) into separate build jobs, letting you test multiple configurations without copying the job definition.
Worked example: Python unit and integration tests
language: python
python:
- "3.8"
- "3.9"
- "3.10"
env:
global:
- TEST_SUITE=unit
matrix:
include:
- python: "3.8"
env: TEST_SUITE=integration
- python: "3.9"
env: TEST_SUITE=integration
- python: "3.10"
env: TEST_SUITE=integration
script:
- if [ "$TEST_SUITE" = "unit" ]; then pytest tests/unit; else pytest tests/integration; fi
Explanation:
- The top‑level
pythonlist creates three base jobs (one per version) that inherit the globalTEST_SUITE=unit. - The
matrix.includeblock adds three additional jobs, one for each Python version, overridingTEST_SUITEtointegration. - Result: six parallel jobs – three unit‑test jobs and three integration‑test jobs.
Limits and common mistakes
- Job explosion: Each matrix entry multiplies the total job count. With the example above you get 6 jobs; adding more versions or variables can quickly exceed the concurrency limit of your Travis account (free tier allows 5 concurrent jobs).
- Incorrect indentation: The
matrixblock must be at the same level aslanguageandenv. Mis‑indenting it underenvorscriptcauses Travis to ignore the matrix or produce parsing errors. - Mixing
includeandexclude: Using both in the same matrix can lead to unexpected combinations becauseexcludeis applied after the full Cartesian product. If you need to remove specific combos, preferexcludeonly, or restructure the matrix to avoid the need for both. - Variable scope: Variables defined under the top‑level
env:are global to all jobs unless they are placed inside a matrix item. In the example,TEST_SUITE=unitis global; theincludeitems override it for integration tests.
How to verify the matrix works
- Push the
.travis.ymlto a branch and watch the Travis dashboard; you should see six separate jobs labeled with the corresponding Python version andTEST_SUITEvalue. - In each job’s log, look for lines like
Using Python 3.9andTEST_SUITE=integrationto confirm the variables were applied. - Optionally run
travis lintlocally (requires the Travis CLI) to catch YAML syntax errors before pushing.
Remember that matrix changes only affect new builds; any builds already queued or running will keep the matrix they were started with.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.