Python में Testing का परिचय
Alexander Levin
Data Scientist
"पिकनिक" वाली उपमा याद करें:
टेस्ट के अंत में एन्वायरनमेंट साफ करना ज़रूरी है। अगर teardown नहीं करेंगे, तो बड़े मुद्दे हो सकते हैं:
कब इस्तेमाल करें:
autouse का उपयोगकब ज़रूरी नहीं:
yield - एक Python कीवर्ड है, जो जेनरेटर बनाने देता है# Example of generator function
def lazy_increment(n):
for i in range(n):
yield i
f = lazy_increment(5)
next(f) # 0
next(f) # 1
next(f) # 2
कैसे इस्तेमाल करें:
return की जगह yield रखेंyield के बाद teardown कोड रखेंyield से पहले हो@pytest.fixture
def init_list():
return []
@pytest.fixture(autouse=True)
def add_numbers_to_list(init_list):
# Fixture Setup
init_list.extend([i for i in range(10)])
# Fixture output
yield init_list
# Teardown statement
init_list.clear()
def test_9(init_list):
assert 9 in init_list
return के बजाय yield कीवर्डyield के बाद teardown कोडPython में Testing का परिचय