패키지 테스트하기

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 패키지 개발하기

Lass uns üben!

Python 패키지 개발하기

Preparing Video For Download...