Kiểm thử gói của bạn

Phát triển gói Python

James Fulton

Climate informatics researcher

Nghệ thuật và kỷ luật của kiểm thử

Hãy hình dung bạn đang làm hàm này

def get_ends(x):
    """Lấy phần tử đầu và cuối của danh sách"""
    return x[0], x[-1]

Bạn có thể kiểm tra để đảm bảo nó hoạt động

# Kiểm tra hàm
get_ends([1,1,5,39,0])
(1, 0)
Phát triển gói Python

Nghệ thuật và kỷ luật của kiểm thử

Các gói tốt tự hào về số lượng bài kiểm thử

  • 91% mã của pandas có kiểm thử Huy hiệu độ bao phủ của Pandas hiển thị 91% hàm được kiểm thử
Phát triển gói Python

Viết kiểm thử

def get_ends(x):
    """Lấy phần tử đầu và cuối của danh sách"""
    return x[0], x[-1]
def test_get_ends():
    assert get_ends([1,5,39,0]) == (1,0)
test_get_ends()


Phát triển gói Python

Viết kiểm thử

def get_ends(x):
    """Lấy phần tử đầu và cuối của danh sách"""
    return x[0], x[1]
def test_get_ends():
    assert get_ends([1,5,39,0]) == (1,0)
test_get_ends()
AssertionError: 
...
Phát triển gói Python

Viết kiểm thử

def get_ends(x):
    """Lấy phần tử đầu và cuối của danh sách"""
    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')
Phát triển gói Python

Tổ chức kiểm thử trong gói của bạn

mysklearn/
|-- mysklearn   <-- package
|-- tests       <-- thư mục tests
|-- setup.py
|-- LICENSE
|-- MANIFEST.in
Phát triển gói Python

Tổ chức kiểm thử trong gói của bạn

Bố cục thư mục tests

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

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

Bố cục thư mục mã nguồn

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

| |-- __init__.py | |-- normalize.py
| |-- standardize.py |-- regression | |-- __init__.py | |-- regression.py |-- utils.py
Phát triển gói Python

Tổ chức một mô-đun kiểm thử

Trong 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

Trong normalize.py

def find_max(x):
    ...
    return x_max

def find_min(x):
    ...
    return x_min

def normalize_data(x):
    ...
    return x_norm
Phát triển gói Python

Chạy kiểm thử với pytest

pytest
  • pytest tìm trong thư mục test
  • Tìm các mô-đun dạng test_modulename.py
  • Tìm các hàm dạng test_functionname()
  • Chạy các hàm này và hiển thị kết quả
mysklearn/ <-- điều hướng đến đây
|-- mysklearn
|-- tests
|-- setup.py
|-- LICENSE
|-- MANIFEST.in
Phát triển gói Python

Chạy kiểm thử với 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 =========================
Phát triển gói Python

Chạy kiểm thử với 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     <--    chạy trong thư mục này
collected 6 items                                 <--    tìm thấy 6 hàm test

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

========================= 6 passed in 0.23s =========================
Phát triển gói Python

Chạy kiểm thử với 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 =========================
Phát triển gói Python

Chạy kiểm thử với 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 =========================
Phát triển gói Python

Chạy kiểm thử với 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 ================================
Phát triển gói Python

Vamos praticar!

Phát triển gói Python

Preparing Video For Download...