测试你的包

开发 Python 包

James Fulton

Climate informatics researcher

测试的艺术与规范

设想你在写这个函数

def get_ends(x):
    """获取列表的第一个和最后一个元素"""
    return x[0], x[-1]

你可能会测试它以确保正确

# 检查函数
get_ends([1,1,5,39,0])
(1, 0)
开发 Python 包

测试的艺术与规范

优秀的包会"炫耀"测试覆盖率

  • pandas 的代码有 91% 覆盖率!Pandas 覆盖率徽章显示 91% 的函数已测试
开发 Python 包

编写测试

def get_ends(x):
    """获取列表的第一个和最后一个元素"""
    return x[0], x[-1]
def test_get_ends():
    assert get_ends([1,5,39,0]) == (1,0)
test_get_ends()


开发 Python 包

编写测试

def get_ends(x):
    """获取列表的第一个和最后一个元素"""
    return x[0], x[1]
def test_get_ends():
    assert get_ends([1,5,39,0]) == (1,0)
test_get_ends()
AssertionError: 
...
开发 Python 包

编写测试

def get_ends(x):
    """获取列表的第一个和最后一个元素"""
    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')
开发 Python 包

在包内组织测试

mysklearn/
|-- mysklearn   <-- 包
|-- tests       <-- 测试目录
|-- setup.py
|-- LICENSE
|-- MANIFEST.in
开发 Python 包

在包内组织测试

测试目录结构

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

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

代码目录结构

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

| |-- __init__.py | |-- normalize.py
| |-- standardize.py |-- regression | |-- __init__.py | |-- regression.py |-- utils.py
开发 Python 包

组织一个测试模块

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

normalize.py

def find_max(x):
    ...
    return x_max

def find_min(x):
    ...
    return x_min

def normalize_data(x):
    ...
    return x_norm
开发 Python 包

用 pytest 运行测试

pytest
  • pytest 会在 test 目录中查找
  • 查找形如 test_modulename.py 的模块
  • 查找形如 test_functionname() 的函数
  • 运行这些函数并显示输出
mysklearn/ <-- 切到此处
|-- mysklearn
|-- tests
|-- setup.py
|-- LICENSE
|-- MANIFEST.in
开发 Python 包

用 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 =========================
开发 Python 包

用 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                                 <--    找到 6 个测试函数

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

========================= 6 passed in 0.23s =========================
开发 Python 包

用 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 =========================
开发 Python 包

用 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 =========================
开发 Python 包

用 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 ================================
开发 Python 包

Passons à la pratique !

开发 Python 包

Preparing Video For Download...