Probar tu paquete

Desarrollo de paquetes en Python

James Fulton

Climate informatics researcher

El arte y la disciplina de las pruebas

Imagina que estás trabajando en esta función

def get_ends(x):
    """Get the first and last element in a list"""
    return x[0], x[-1]

Podrías probarla para asegurarte de que funciona

# Check the function
get_ends([1,1,5,39,0])
(1, 0)
Desarrollo de paquetes en Python

El arte y la disciplina de las pruebas

Los buenos paquetes presumen de cuántas pruebas tienen

  • ¡El 91% del código de pandas tiene pruebas! Insignia de cobertura de Pandas indica que el 91% de las funciones están probadas
Desarrollo de paquetes en Python

Escribir pruebas

def get_ends(x):
    """Get the first and last element in a list"""
    return x[0], x[-1]
def test_get_ends():
    assert get_ends([1,5,39,0]) == (1,0)
test_get_ends()


Desarrollo de paquetes en Python

Escribir pruebas

def get_ends(x):
    """Get the first and last element in a list"""
    return x[0], x[1]
def test_get_ends():
    assert get_ends([1,5,39,0]) == (1,0)
test_get_ends()
AssertionError: 
...
Desarrollo de paquetes en Python

Escribir pruebas

def get_ends(x):
    """Get the first and last element in a list"""
    return x[0], x[-1]
def test_get_ends():
    assert get_ends([1,5,39,0]) == (1,0)
    assert get_ends(['n','e','r','d']) == ('n','d')
Desarrollo de paquetes en Python

Organizar las pruebas dentro de tu paquete

mysklearn/
|-- mysklearn   <-- paquete
|-- tests       <-- directorio de pruebas
|-- setup.py
|-- LICENSE
|-- MANIFEST.in
Desarrollo de paquetes en Python

Organizar las pruebas dentro de tu paquete

Estructura del directorio de pruebas

mysklearn/tests/
|-- __init__.py
|-- preprocessing

| |-- __init__.py | |-- test_normalize.py
| |-- test_standardize.py |-- regression | |-- __init__.py | |-- test_regression.py |-- test_utils.py

Estructura del directorio de código

mysklearn/mysklearn/
|-- __init__.py
|-- preprocessing

| |-- __init__.py | |-- normalize.py
| |-- standardize.py |-- regression | |-- __init__.py | |-- regression.py |-- utils.py
Desarrollo de paquetes en Python

Organizar un módulo de pruebas

Dentro de test_normalize.py

from mysklearn.preprocessing.normalize import (
    find_max, find_min, normalize_data
)

def test_find_max(x):
    assert find_max([1,4,7,1])==7

def test_find_min(x):
    assert ...

def test_normalize_data(x):
    assert ...

DataCamp: Unit testing for data science

Dentro de normalize.py

def find_max(x):
    ...
    return x_max

def find_min(x):
    ...
    return x_min

def normalize_data(x):
    ...
    return x_norm
Desarrollo de paquetes en Python

Ejecutar pruebas con pytest

pytest
  • pytest busca dentro del directorio test
  • Busca módulos como test_modulename.py
  • Busca funciones como test_functionname()
  • Ejecuta esas funciones y muestra la salida
mysklearn/ <-- navega aquí
|-- mysklearn
|-- tests
|-- setup.py
|-- LICENSE
|-- MANIFEST.in
Desarrollo de paquetes en Python

Ejecutar pruebas con pytest

pytest
======================== test session starts ========================
platform linux -- Python 3.7.9, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
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 =========================
Desarrollo de paquetes en Python

Ejecutar pruebas con pytest

pytest
======================== test session starts ========================
platform linux -- Python 3.7.9, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: /home/workspace/mypackages/mysklearn     <--    ejecutado en este directorio
collected 6 items                                 <--    se encontraron 6 funciones de prueba

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

========================= 6 passed in 0.23s =========================
Desarrollo de paquetes en Python

Ejecutar pruebas con pytest

pytest
======================== test session starts ========================
platform linux -- Python 3.7.9, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
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 =========================
Desarrollo de paquetes en Python

Ejecutar pruebas con pytest

pytest
======================== test session starts ========================
platform linux -- Python 3.7.9, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
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 =========================
Desarrollo de paquetes en Python

Ejecutar pruebas con pytest

pytest
==================================== test session starts ====================================
...
tests/preprocessing/test_normalize.py .F.                                              [ 50%]
tests/preprocessing/test_standardize.py ...                                            [100%]

========================================= FAILURES ==========================================
________________________________________ test_mymax _________________________________________
...

tests/preprocessing/test_normalize.py:10: AssertionError
================================== short test summary info ==================================
FAILED tests/preprocessing/test_normalize.py::test_mymax - assert -100 == 100       <-- test_mymax
================================ 1 failed, 5 passed in 0.17s ================================
Desarrollo de paquetes en Python

¡Vamos a practicar!

Desarrollo de paquetes en Python

Preparing Video For Download...