Skip to content

Python

This guide provides a walkthrough for a Python-based application and how to configure common CI tasks. If you’re new to Razorops please read our Tutorial and configuration guides first.

We will show how to download dependencies, run tests, generate artifacts or coverage reports using various popular tools in Python community. We also provide a CI optimized container image for Python as well.

Specify Python version

You can control Python version using an appropriate docker tag in runner property. To know more about runner, see here.

tasks:
  py3:
    runner: razorci/python:3.7
    ...
tasks:
  py2:
    runner: razorci/python:2.7
    ...

Download dependencies

Downloading python third-party modules/dependencies from the internet can be time-consuming, you can greatly speed up this process by caching it using an unique key. For more information, see Caching dependencies.

In the below example we demonstrate a pipeline that restores a cache, executes pip install, and rebuilds the cache. Cache directory location will vary depending on your tool (pip, wheel or pipenv).

tasks:
  unit-test:
    runner: razorci/python
    steps:
    - cache/pull:
      - key: pip-cache-{{ checksum "requirements.txt" }}
    ...
    - run: pip install -r requirements.txt --user
    ...
    - cache/push:
      - key: pip-cache-{{ checksum "requirements.txt" }}
        paths:
        - ~/.cache/pip ## for pip
tasks:
  unit-test:
    runner: razorci/python
    steps:
    - cache/pull:
      - key: pipenv-cache-{{ checksum "Pipfile.lock" }}
    ...
    - commands: 
      - sudo pip install pipenv
      - pipenv install
    ...
    - cache/push:
      - key: pipenv-cache-{{ checksum "Pipfile.lock" }}
        paths:
        - ~/.local/share/virtualenvs/venv

Execute tests and generate reports

You can use the same command that you use locally with run property in steps and combine with various lint and coverage tools to generate reports. To run the tests, it is recommended that you use tox.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
tasks:
  unit-test:
    runner: razorci/python
    steps:
    ...
    - commands: 
      - pip install -r requirements.txt --user

      # read more at https://nose.readthedocs.io/en/latest/plugins/xunit.html  
      - nosetests tests.py --with-coverage --cover-html --with-xunit
    ...

    # lint step using flake8, add it in requirements.txt
    - run: flake8 .
    ...

    - reports/html: 
        dir: cover # generated from line 10
    - reports/junit:
        paths: nosetests.xml # generated from line 10
tasks:
  unit-test:
    runner: razorci/python
    steps:
    ...
    - commands: 
      - pip install pytest pytest-cov
      - python -m pytest tests.py
    ...
## file: .razorps.yaml
tasks:
  unit-test:
    runner: razorci/python
    steps:
    ...
    - commands:
      - tox
    ...
## file: tox.ini
[tox]
envlist = py36,py37
[testenv]
deps = pytest
commands = pytest

Publish artifacts

You can upload artifacts to view after a workflow completes. For example, you may need to save log files, core dumps, test results, or screenshots. For more information, see "Persisting workflow data using artifacts."

The below example demonstrates how you can use the artifacts/push step to archive test results from running pytest. For more information, see the artifacts/push step.

tasks:
  unit-test:
    runner: razorci/python
    steps:
    ...
    - commands:
      - pip install pytest
      - python -m pytest tests.py --doctest-modules --junitxml=junit/test-results.xml
    ...
    - artifacts/push:
        name: test-results
        paths: [junit/test-results.xml]