パッケージのテスト

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
  • pytesttest ディレクトリ内を探索します
  • 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...