Testarea pachetului tău

Dezvoltarea pachetelor Python

James Fulton

Climate informatics researcher

Arta și disciplina testării

Presupuneți că lucrați la această funcție

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

Puteți să o testați pentru a verifica dacă funcționează

# Check the function
get_ends([1,1,5,39,0])
(1, 0)
Dezvoltarea pachetelor Python

Arta și disciplina testării

Pachetele de calitate se laudă cu numărul de teste

  • 91% din codul pachetului pandas este acoperit de teste Insigna de acoperire Pandas indică faptul că 91% din funcții sunt testate
Dezvoltarea pachetelor Python

Scrierea testelor

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()


Dezvoltarea pachetelor Python

Scrierea testelor

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: 
...
Dezvoltarea pachetelor Python

Scrierea testelor

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')
Dezvoltarea pachetelor Python

Organizarea testelor în pachet

mysklearn/
|-- mysklearn   <-- package
|-- tests       <-- tests directory
|-- setup.py
|-- LICENSE
|-- MANIFEST.in
Dezvoltarea pachetelor Python

Organizarea testelor în pachet

Structura directorului de teste

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

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

Structura directorului de cod

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

| |-- __init__.py | |-- normalize.py
| |-- standardize.py |-- regression | |-- __init__.py | |-- regression.py |-- utils.py
Dezvoltarea pachetelor Python

Organizarea unui modul de testare

În 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

În normalize.py

def find_max(x):
    ...
    return x_max

def find_min(x):
    ...
    return x_min

def normalize_data(x):
    ...
    return x_norm
Dezvoltarea pachetelor Python

Rularea testelor cu pytest

pytest
  • pytest caută în directorul test
  • Caută module de tip test_modulename.py
  • Caută funcții de tip test_functionname()
  • Rulează aceste funcții și afișează rezultatul
mysklearn/ <-- navigate to here
|-- mysklearn
|-- tests
|-- setup.py
|-- LICENSE
|-- MANIFEST.in
Dezvoltarea pachetelor Python

Rularea testelor cu 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 =========================
Dezvoltarea pachetelor Python

Rularea testelor cu 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     <--    ran in this directory
collected 6 items                                 <--    found 6 test functions

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

========================= 6 passed in 0.23s =========================
Dezvoltarea pachetelor Python

Rularea testelor cu 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 =========================
Dezvoltarea pachetelor Python

Rularea testelor cu 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 =========================
Dezvoltarea pachetelor Python

Rularea testelor cu 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 ================================
Dezvoltarea pachetelor Python

Să exersăm!

Dezvoltarea pachetelor Python

Preparing Video For Download...