การทดสอบแพ็กเกจกับหลายสภาพแวดล้อม

การพัฒนา Python Packages

James Fulton

Climate informatics researcher

การทดสอบ Python หลายเวอร์ชัน

ไฟล์ setup.py นี้รองรับ Python ตั้งแต่เวอร์ชัน 2.7 ขึ้นไป

from setuptools import setup, find_packages

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

สิ่งที่ต้องทำเพื่อทดสอบ Python หลายเวอร์ชัน:

  • ติดตั้ง Python ทุกเวอร์ชันที่ต้องการ
  • ติดตั้งแพ็กเกจและ dependencies ลงใน Python แต่ละเวอร์ชัน
  • รัน pytest
การพัฒนา Python Packages

การทดสอบ Python หลายเวอร์ชัน

ไฟล์ setup.py นี้รองรับ Python ตั้งแต่เวอร์ชัน 2.7 ขึ้นไป

from setuptools import setup, find_packages

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

สิ่งที่ต้องทำเพื่อทดสอบ Python หลายเวอร์ชัน:

  • ติดตั้ง Python ทุกเวอร์ชันที่ต้องการ
  • รัน tox
การพัฒนา Python Packages

tox คืออะไร?

  • ออกแบบมาเพื่อรันการทดสอบกับ Python หลายเวอร์ชัน
การพัฒนา Python Packages

ตั้งค่า tox

ไฟล์คอนฟิก - tox.ini

mysklearn/
|-- mysklearn
|   |-- ...
|-- tests
|   |-- ...
|-- setup.py
|-- LICENSE
|-- MANIFEST.in
|-- tox.ini   <--- configuration file
การพัฒนา Python Packages

ตั้งค่า tox

ไฟล์คอนฟิก - tox.ini

[tox]

envlist = py27, py35, py36, py37
[testenv]
deps = pytest
commands = pytest
echo "run more commands" ...
  • หัวข้อจะอยู่ในวงเล็บเหลี่ยม [...]
  • หากต้องการทดสอบ Python เวอร์ชัน X.Y ให้เพิ่ม pyXY ลงใน envlist
  • Python แต่ละเวอร์ชันที่ต้องการทดสอบต้องติดตั้งไว้ก่อน
  • พารามิเตอร์ commands ระบุคำสั่ง terminal ที่ tox จะรัน
  • รายการใน commands เป็นคำสั่ง terminal ใดก็ได้ เช่น ls, cd, echo เป็นต้น
การพัฒนา Python Packages

การรัน tox

tox
mysklearn/    <-- navigate to here
|-- mysklearn
|   |-- ...
|-- tests
|   |-- ...
|-- setup.py
|-- LICENSE
|-- MANIFEST.in
|-- tox.ini
การพัฒนา Python Packages

ผลลัพธ์ของ 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 Packages

ผลลัพธ์ของ 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 Packages

ผลลัพธ์ของ tox

...
__________________________ summary __________________________
  py27: commands succeeded
  py35: commands succeeded
  py36: commands succeeded
  py37: commands succeeded
การพัฒนา Python Packages

ผลลัพธ์ของ tox

...
__________________________ summary __________________________
  py27: commands succeeded
  py35: commands succeeded
  py36: commands succeeded
ERROR:   py37: commands failed
การพัฒนา Python Packages

มาฝึกกันเถอะ!

การพัฒนา Python Packages

Preparing Video For Download...