अपने पैकेज का परीक्षण

Python पैकेज विकसित करना

James Fulton

Climate informatics researcher

टेस्टिंग: कला और अनुशासन

कल्पना कीजिए आप इस फंक्शन पर काम कर रहे हैं

def get_ends(x):
    """Get the first and last element in a list"""
    return x[0], x[-1]

आप इसे काम कर रहा है या नहीं, यह जाँच सकते हैं

# फंक्शन जाँचें
get_ends([1,1,5,39,0])
(1, 0)
Python पैकेज विकसित करना

टेस्टिंग: कला और अनुशासन

अच्छे पैकेज अपने टेस्ट कवरिज पर गर्व करते हैं

  • pandas पैकेज के 91% कोड का परीक्षण हुआ है Pandas कवरिज बैज बताता है कि 91% फंक्शंस टेस्ट किए गए हैं
Python पैकेज विकसित करना

टेस्ट कैसे लिखें

def get_ends(x):
    """Get the first and last element in a list"""
    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):
    """Get the first and last element in a list"""
    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):
    """Get the first and last element in a list"""
    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   <-- package
|-- tests       <-- tests directory
|-- 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 पैकेज विकसित करना

अभ्यास करते हैं!

Python पैकेज विकसित करना

Preparing Video For Download...