यूनिट टेस्टिंग

Python में Software Engineering Principles

Adam Spannbauer

Machine Learning Engineer at Eastman

टेस्टिंग क्यों?

  • कोड इच्छित तरह काम कर रहा है, यह पुष्टि करें
  • एक फंक्शन का बदलाव दूसरे को न तोड़े, यह सुनिश्चित करें
  • डिपेंडेंसी में बदलावों से सुरक्षा करें
Python में Software Engineering Principles

Python में टेस्टिंग

  • doctest
  • pytest

pytest लोगो

Python में Software Engineering Principles

doctest का उपयोग

def square(x):
    """Square the number x

    :param x: number to square
    :return: x squared

    >>> square(3)
    9
    """
    return x ** 3


import doctest doctest.testmod()
Failed example:
    square(3)
Expected:
    9
Got:
    27
Python में Software Engineering Principles

pytest संरचना

टेस्ट संरचना

Python में Software Engineering Principles

pytest संरचना

टेस्ट संरचना

Python में Software Engineering Principles

यूनिट टेस्ट लिखना

workdir/tests/test_document.py में कार्य करते हुए

from text_analyzer import Document


# Test tokens attribute on Document object
def test_document_tokens():
    doc = Document('a e i o u')

    assert doc.tokens == ['a', 'e', 'i', 'o', 'u']

# Test edge case of blank document def test_document_empty(): doc = Document('') assert doc.tokens == [] assert doc.word_counts == Counter()
Python में Software Engineering Principles

यूनिट टेस्ट लिखना

# Create 2 identical Document objects
doc_a = Document('a e i o u')
doc_b = Document('a e i o u')

# Check if objects are ==
print(doc_a == doc_b)

# Check if attributes are == print(doc_a.tokens == doc_b.tokens) print(doc_a.word_counts == doc_b.word_counts)
False

True True
Python में Software Engineering Principles

pytest चलाना

terminal के साथ कार्य करते हुए

datacamp@server:~/work_dir $ pytest
collected 2 items

tests/test_document.py ..                     [100%]

========== 2 passed in 0.61 seconds ==========
Python में Software Engineering Principles

pytest चलाना

terminal के साथ कार्य करते हुए

datacamp@server:~/work_dir $ pytest tests/test_document.py
collected 2 items

tests/test_document.py ..                     [100%]

========== 2 passed in 0.61 seconds ==========
Python में Software Engineering Principles

फेल होते टेस्ट

terminal के साथ कार्य करते हुए

datacamp@server:~/work_dir $ pytest
collected 2 items

tests/test_document.py F.

============== FAILURES ==============
________ test_document_tokens ________

def test_document_tokens(): doc = Document('a e i o u')

assert doc.tokens == ['a', 'e', 'i', 'o']
E AssertionError: assert ['a', 'e', 'i', 'o', 'u'] == ['a', 'e', 'i', 'o']
E Left contains more items, first extra item: 'u'
E Use -v to get the full diff

tests/test_document.py:7: AssertionError
====== 1 failed in 0.57 seconds ======
Python में Software Engineering Principles

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

Python में Software Engineering Principles

Preparing Video For Download...