Testing with all version combinations of Python & Django

In Travis CI, you can run tests with multiple versions of Python like this:

python:
- "2.7"
- "3.4"

And you can specify Django versions like this:

env:
- DJANGO=1.7
- DJANGO=1.8

install:
- pip install -q Django==$DJANGO

This ends up testing the project with Python and Django versions 2.7 & 1.7, 2.7 & 1.8, 3.4 & 1.7, and 3.4 & 1.8.

I’m trying to do the same thing in GitLab CI. Here’s my guess of how:

test:2.7:
   image: python:2.7
   variables:
       DJANGO: "1.7"
   script:
   - pip install -q Django==$DJANGO

… and then repeat that 3 more times to get each combination of Python & Django.

This requires 6 LOC * # of Python versions * # of Django versions. That’s more verbose than I’d like, given the lists of Python and Django versions will change. Is there a better way?

Best I can tell from my research, GitLab CI doesn’t support this feature, which I would call “job factors” or “environment factors”. Your best bet is probably to use something like tox, which implements generating environments with multiple factors.

I do it this way, with extends, works pretty well IMO.

stages:
    - "verify"

.verify:
    script:
        - "python -m venv /opt/venv"
        - "source /opt/venv/bin/activate; pip install --upgrade pip"
        - "source /opt/venv/bin/activate; pip install -r requirements/dev.txt"
        - "source /opt/venv/bin/activate; invoke test"

verify-3.6:
    extends: ".verify"
    stage: "verify"
    image: "python:3.6"

verify-3.7:
    extends: ".verify"
    stage: "verify"
    image: "python:3.7"
1 Like

Does the following produce the same result?

stages:
    - "verify"

.verify:
    stage: "verify"
    script:
        - "python -m venv /opt/venv"
        - "source /opt/venv/bin/activate; pip install --upgrade pip"
        - "source /opt/venv/bin/activate; pip install -r requirements/dev.txt"
        - "source /opt/venv/bin/activate; invoke test"

verify-3.6:
    extends: ".verify"
    image: "python:3.6"

verify-3.7:
    extends: ".verify"
    image: "python:3.7"