Test del tuo pacchetto in ambienti diversi

Sviluppare pacchetti Python

James Fulton

Climate informatics researcher

Testare più versioni di Python

Questo setup.py consente qualsiasi versione di Python dalla 2.7 in su.

from setuptools import setup, find_packages

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

Per testare queste versioni di Python devi:

  • Installare tutte queste versioni di Python
  • Installare il tuo pacchetto e tutte le dipendenze in ciascun Python
  • Eseguire pytest
Sviluppare pacchetti Python

Testare più versioni di Python

Questo setup.py consente qualsiasi versione di Python dalla 2.7 in su.

from setuptools import setup, find_packages

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

Per testare queste versioni di Python devi:

  • Installare tutte queste versioni di Python
  • Eseguire tox
Sviluppare pacchetti Python

Che cos'è tox?

  • Progettato per eseguire test con più versioni di Python
Sviluppare pacchetti Python

Configurare tox

File di configurazione - tox.ini

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

Configurare tox

File di configurazione - tox.ini

[tox]

envlist = py27, py35, py36, py37
[testenv]
deps = pytest
commands = pytest
echo "run more commands" ...
  • Le intestazioni sono tra parentesi quadre [...].
  • Per testare Python versione X.Y aggiungi pyXY a envlist.
  • Le versioni di Python da testare devono essere già installate.
  • Il parametro commands elenca i comandi di terminale che tox eseguirà.
  • L'elenco commands può includere qualsiasi comando eseguibile da terminale, come ls, cd, echo ecc.
Sviluppare pacchetti Python

Eseguire tox

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

output di tox

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

output di tox

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

output di tox

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

output di tox

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

Esercitiamoci!

Sviluppare pacchetti Python

Preparing Video For Download...