Testing your package with different environments

Sviluppare pacchetti Python

James Fulton

Climate informatics researcher

Testing multiple versions of Python

This setup.py allows any version of Python from version 2.7 upwards.

from setuptools import setup, find_packages

setup(
    ...
    python_requires='>=2.7',
)

To test these Python versions you must:

  • Install all these Python versions
  • Install your package and all dependencies into each Python
  • Run pytest
Sviluppare pacchetti Python

Testing multiple versions of Python

This setup.py allows any version of Python from version 2.7 upwards.

from setuptools import setup, find_packages

setup(
    ...
    python_requires='>=2.7',
)

To test these Python versions you must:

  • Install all these Python versions
  • Run tox
Sviluppare pacchetti Python

What is tox?

  • Designed to run tests with multiple versions of Python
Sviluppare pacchetti Python

Configure tox

Configuration file - tox.ini

mysklearn/
|-- mysklearn
|   |-- ...
|-- tests
|   |-- ...
|-- setup.py
|-- LICENSE
|-- MANIFEST.in
|-- tox.ini   <--- configuration file
Sviluppare pacchetti Python

Configure tox

Configuration file - tox.ini

[tox]

envlist = py27, py35, py36, py37
[testenv]
deps = pytest
commands = pytest
echo "run more commands" ...
  • Headings are surrounded by square brackets [...].
  • To test Python version X.Y add pyXY to envlist.
  • The versions of Python you test need to be installed already.
  • The commands parameter lists the terminal commands tox will run.
  • The commands list can be any commands which will run from the terminal, like ls, cd, echo etc.
Sviluppare pacchetti Python

Running tox

tox
mysklearn/    <-- navigate to here
|-- mysklearn
|   |-- ...
|-- tests
|   |-- ...
|-- setup.py
|-- LICENSE
|-- MANIFEST.in
|-- tox.ini
Sviluppare pacchetti Python

tox output

py27 create: /mypackages/mysklearn/.tox/py27
py27 installdeps: pytest
py27 inst: /mypackages/mysklearn/.tox/.tmp/package/1/mysklearn-0.1.0.zip
py27 installed: mysklearn==0.1.0,numpy==1.16.6,pandas==0.24.2,pytest==4.6.11,...
py27 run-test-pre: PYTHONHASHSEED='2837498672'
...
Sviluppare pacchetti Python

tox output

py27 run-test: commands[0] | pytest
======================== test session starts ========================
platform linux2 -- Python 2.7.17, ...
rootdir: /home/workspace/mypackages/mysklearn
collected 6 items                         

tests/preprocessing/test_normalize.py ...                     [ 50%]
tests/preprocessing/test_standardize.py ...                   [100%]

========================= 6 passed in 0.23s =========================
Sviluppare pacchetti Python

tox output

...
__________________________ summary __________________________
  py27: commands succeeded
  py35: commands succeeded
  py36: commands succeeded
  py37: commands succeeded
Sviluppare pacchetti Python

tox output

...
__________________________ summary __________________________
  py27: commands succeeded
  py35: commands succeeded
  py36: commands succeeded
ERROR:   py37: commands failed
Sviluppare pacchetti Python

Let's practice!

Sviluppare pacchetti Python

Preparing Video For Download...