Python'da Yazılım Mühendisliği İlkeleri
Adam Spannbauer
Machine Learning Engineer at Eastman
doctestpytest
def square(x): """x sayısını kare al :param x: kare alınacak sayı :return: x'in karesi >>> square(3) 9 """ return x ** 3import doctest doctest.testmod()
Başarısız örnek:
square(3)
Beklenen:
9
Alınan:
27


workdir/tests/test_document.py içinde çalışılıyor
from text_analyzer import Document # Document nesnesinde tokens özelliğini test et def test_document_tokens(): doc = Document('a e i o u') assert doc.tokens == ['a', 'e', 'i', 'o', 'u']# Boş belge kenar durumunu test et def test_document_empty(): doc = Document('') assert doc.tokens == [] assert doc.word_counts == Counter()
# 2 aynı Document nesnesi oluştur doc_a = Document('a e i o u') doc_b = Document('a e i o u') # Nesnelerin eşit olup olmadığını kontrol et print(doc_a == doc_b)# Özelliklerin eşit olup olmadığını kontrol et print(doc_a.tokens == doc_b.tokens) print(doc_a.word_counts == doc_b.word_counts)
FalseTrue True
terminal ile çalışılıyor
datacamp@server:~/work_dir $ pytest
2 öğe toplandı
tests/test_document.py .. [100%]
========== 2 test geçti 0.61 saniyede ==========
terminal ile çalışılıyor
datacamp@server:~/work_dir $ pytest tests/test_document.py
2 öğe toplandı
tests/test_document.py .. [100%]
========== 2 test geçti 0.61 saniyede ==========
terminal ile çalışılıyor
datacamp@server:~/work_dir $ pytest
2 öğe toplandı
tests/test_document.py F.
============== HATALAR ==============
________ 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 Sol daha fazla öğe içeriyor, ilk ekstra öğe: 'u'
E Tam farkı görmek için -v kullanın
tests/test_document.py:7: AssertionError
====== 1 test başarısız oldu 0.57 saniyede ======
Python'da Yazılım Mühendisliği İlkeleri