다양한 환경에서 패키지 테스트하기

Python 패키지 개발하기

James Fulton

Climate informatics researcher

여러 Python 버전 테스트하기

setup.py는 Python 2.7 이상 모든 버전을 허용합니다.

from setuptools import setup, find_packages

setup(
    ...
    python_requires='>=2.7',
)

이 Python 버전들을 테스트하려면 다음이 필요합니다:

  • 해당 모든 Python 버전 설치
  • 각 Python에 패키지와 모든 의존성 설치
  • pytest 실행
Python 패키지 개발하기

여러 Python 버전 테스트하기

setup.py는 Python 2.7 이상 모든 버전을 허용합니다.

from setuptools import setup, find_packages

setup(
    ...
    python_requires='>=2.7',
)

이 Python 버전들을 테스트하려면:

  • 해당 모든 Python 버전 설치
  • tox 실행
Python 패키지 개발하기

tox란?

  • 여러 Python 버전으로 테스트 실행을 지원하도록 설계됨
Python 패키지 개발하기

tox 구성하기

구성 파일 - tox.ini

mysklearn/
|-- mysklearn
|   |-- ...
|-- tests
|   |-- ...
|-- setup.py
|-- LICENSE
|-- MANIFEST.in
|-- tox.ini   <--- 구성 파일
Python 패키지 개발하기

tox 구성하기

구성 파일 - tox.ini

[tox]

envlist = py27, py35, py36, py37
[testenv]
deps = pytest
commands = pytest
echo "run more commands" ...
  • 제목은 대괄호 [...]로 둘러쌉니다.
  • Python X.Y를 테스트하려면 envlistpyXY를 추가합니다.
  • 테스트할 Python 버전은 미리 설치되어 있어야 합니다.
  • commands 매개변수에는 tox가 실행할 터미널 명령을 나열합니다.
  • commands에는 ls, cd, echo 등 터미널에서 실행 가능한 어떤 명령이든 넣을 수 있습니다.
Python 패키지 개발하기

tox 실행하기

tox
mysklearn/    <-- 여기로 이동
|-- mysklearn
|   |-- ...
|-- tests
|   |-- ...
|-- setup.py
|-- LICENSE
|-- MANIFEST.in
|-- tox.ini
Python 패키지 개발하기

tox 출력

py27 create: /mypackages/mysklearn/.tox/py27
py27 installdeps: pytest
py27 inst: /mypackages/mysklearn/.tox/.tmp/package/1/mysklearn-0.1.0.zip
py27 installed: mysklearn==0.1.0,numpy==1.16.6,pandas==0.24.2,pytest==4.6.11,...
py27 run-test-pre: PYTHONHASHSEED='2837498672'
...
Python 패키지 개발하기

tox 출력

py27 run-test: commands[0] | pytest
======================== test session starts ========================
platform linux2 -- Python 2.7.17, ...
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 패키지 개발하기

tox 출력

...
__________________________ summary __________________________
  py27: commands succeeded
  py35: commands succeeded
  py36: commands succeeded
  py37: commands succeeded
Python 패키지 개발하기

tox 출력

...
__________________________ summary __________________________
  py27: commands succeeded
  py35: commands succeeded
  py36: commands succeeded
ERROR:   py37: commands failed
Python 패키지 개발하기

Vamos praticar!

Python 패키지 개발하기

Preparing Video For Download...