PyTest ट्यूटोरियल: क्या है, कैसे स्थापित करें, फ्रेमवर्क, अभिकथन

पायटेस्ट क्या है?

पायटेस्ट एक परीक्षण ढांचा है जो उपयोगकर्ताओं को परीक्षण कोड लिखने की अनुमति देता है Python प्रोग्रामिंग भाषा। यह आपको डेटाबेस, API या UI के लिए सरल और स्केलेबल टेस्ट केस लिखने में मदद करता है। PyTest का उपयोग मुख्य रूप से API के लिए परीक्षण लिखने के लिए किया जाता है। यह सरल यूनिट टेस्ट से लेकर जटिल कार्यात्मक परीक्षण तक के परीक्षण लिखने में मदद करता है।

PyTest का उपयोग क्यों करें?

pytest के कुछ लाभ इस प्रकार हैं

  • इसकी सरल और आसान वाक्यविन्यास के कारण इसे शुरू करना बहुत आसान है।
  • समानांतर परीक्षण चला सकते हैं.
  • एक विशिष्ट परीक्षण या परीक्षणों का एक उपसमूह चलाया जा सकता है
  • स्वचालित रूप से परीक्षणों का पता लगाना
  • परीक्षण छोड़ें
  • खुला स्त्रोत

PyTest कैसे स्थापित करें

PyTest को स्थापित करने की प्रक्रिया निम्नलिखित है:

चरण 1) आप pytest को इस प्रकार स्थापित कर सकते हैं

pip install pytest==2.9.1

एक बार स्थापना पूरी हो जाने पर आप इसकी पुष्टि कर सकते हैं

py.test -h

इससे सहायता प्रदर्शित होगी

PyTest स्थापित करें

पहला बेसिक PyTest

अब, हम एक बुनियादी PyTest उदाहरण के साथ Pytest का उपयोग करना सीखेंगे।

एक फ़ोल्डर study_pytest बनाएँ। हम इस फ़ोल्डर के अंदर अपनी परीक्षण फ़ाइलें बनाने जा रहे हैं।

कृपया अपने कमांड लाइन में उस फ़ोल्डर पर जाएँ।

फ़ोल्डर के अंदर test_sample1.py नाम की एक फ़ाइल बनाएँ

पहला बेसिक PyTest

इसमें नीचे दिया गया कोड जोड़ें और सेव करें

import pytest
def test_file1_method1():
	x=5
	y=6
	assert x+1 == y,"test failed"
	assert x == y,"test failed"
def test_file1_method2():
	x=5
	y=6
	assert x+1 == y,"test failed" 

कमांड का उपयोग करके परीक्षण चलाएँ

py.test

आपको आउटपुट इस प्रकार मिलेगा

test_sample1.py F.
============================================== FAILURES ========================================
____________________________________________ test_sample1 ______________________________________
    def test_file1_method1():
    	x=5
    	y=6
       	assert x+1 == y,"test failed"
>      	assert x == y,"test failed"
E       AssertionError: test failed
E       assert 5 == 6
test_sample1.py:6: AssertionError

पहला बेसिक PyTest

यहाँ test_sample1.py में F.

एफ कहता है असफलता

बिंदु (.) सफलता कहता है।

विफलता अनुभाग में, आप विफल विधि(ओं) और विफलता की रेखा देख सकते हैं। यहाँ x==y का अर्थ 5==6 है जो कि गलत है।

इस PyTest ट्यूटोरियल में आगे, हम PyTest में assertion के बारे में सीखेंगे।

PyTest में अभिकथन

पायटेस्ट दावे वे जाँच हैं जो सत्य या असत्य स्थिति लौटाते हैं। Python Pytest में, यदि किसी परीक्षण विधि में कोई दावा विफल हो जाता है, तो उस विधि का निष्पादन वहीं रोक दिया जाता है। उस परीक्षण विधि में शेष कोड निष्पादित नहीं होता है, और Pytest दावे अगली परीक्षण विधि के साथ जारी रहेंगे।

पायटेस्ट एसर्ट उदाहरण:

assert "hello" == "Hai" is an assertion failure.
assert 4==4 is a successful assertion
assert True is a successful assertion
assert False is an assertion failure.

विचार करना

assert x == y,"test failed because x=" + str(x) + " y=" + str(y)

इस कोड को दावे के स्थान पर test_file1_method1() में रखें

assert x == y,"test failed"

परीक्षण चलाने पर विफलता मिलेगी अभिकथनत्रुटि: परीक्षण विफल x=5 y=6

PyTest परीक्षण फ़ाइलों और परीक्षण विधियों की पहचान कैसे करता है

डिफ़ॉल्ट रूप से pytest केवल से शुरू होने वाले फ़ाइल नामों की पहचान करता है परीक्षा_ या के साथ समाप्त हो रहा है _परीक्षा परीक्षण फ़ाइलों के रूप में। हालाँकि हम अन्य फ़ाइल नामों का स्पष्ट रूप से उल्लेख कर सकते हैं (बाद में समझाया जाएगा)। Pytest के लिए परीक्षण विधि नामों की शुरुआत से शुरू होना आवश्यक है "परीक्षाअन्य सभी विधि नामों को नजरअंदाज कर दिया जाएगा, भले ही हम स्पष्ट रूप से उन विधियों को चलाने के लिए कहें।

मान्य और अमान्य pytest फ़ाइल नामों के कुछ उदाहरण देखें

test_login.py - valid
login_test.py - valid
testlogin.py -invalid
logintest.py -invalid

नोट: हां, हम स्पष्ट रूप से pytest से testlogin.py और logintest.py चुनने के लिए कह सकते हैं

मान्य और अमान्य pytest परीक्षण विधियों के कुछ उदाहरण देखें

def test_file1_method1(): - valid
def testfile1_method1(): - valid
def file1_method1(): - invalid	

नोट: भले ही हम स्पष्ट रूप से file1_method1() का उल्लेख करें, pytest इस विधि को नहीं चलाएगा।

किसी विशिष्ट फ़ाइल और एकाधिक फ़ाइलों से एकाधिक परीक्षण चलाएँ

वर्तमान में, study_pytest फ़ोल्डर के अंदर, हमारे पास test_sample1.py फ़ाइल है। मान लीजिए हमारे पास कई फ़ाइलें हैं, जैसे test_sample2.py, test_sample3.py। फ़ोल्डर और सबफ़ोल्डर में सभी फ़ाइलों से सभी परीक्षण चलाने के लिए हमें बस pytest कमांड चलाने की ज़रूरत है।

py.test

यह उस फ़ोल्डर और उसके अंतर्गत आने वाले सबफ़ोल्डर्स में test_ से शुरू होने वाले और _test पर समाप्त होने वाले सभी फ़ाइलनामों को चलाएगा।

केवल एक विशिष्ट फ़ाइल से परीक्षण चलाने के लिए, हम py.test का उपयोग कर सकते हैं

py.test test_sample1.py

PyTest के साथ संपूर्ण परीक्षण का एक उपसमूह चलाएं

कभी-कभी हम पूरा टेस्ट सूट नहीं चलाना चाहते। Pytest हमें विशिष्ट परीक्षण चलाने की अनुमति देता है। हम इसे 2 तरीकों से कर सकते हैं

  • सबस्ट्रिंग मिलान द्वारा परीक्षण नामों का समूहीकरण
  • मार्करों द्वारा परीक्षणों का समूहीकरण

हमारे पास पहले से ही test_sample1.py है। एक फ़ाइल test_sample2.py बनाएँ और उसमें नीचे दिया गया कोड जोड़ें

def test_file2_method1():
	x=5
	y=6
	assert x+1 == y,"test failed"
	assert x == y,"test failed because x=" + str(x) + " y=" + str(y)
def test_file2_method2():
	x=5
	y=6
	assert x+1 == y,"test failed"

अतः वर्तमान में हमारे पास

• test_sample1.py
• test_file1_method1()
• test_file1_method2()
• test_sample2.py
• test_file2_method1()
• test_file2_method2()

विकल्प 1) सबस्ट्रिंग मिलान द्वारा परीक्षण चलाएँ

यहाँ नाम में method1 वाले सभी परीक्षण चलाने के लिए हमें चलाना होगा

py.test -k method1 -v
-k <expression> is used to represent the substring to match
-v increases the verbosity

इसलिए py.test -k method1 -v चलाने से आपको निम्नलिखित परिणाम प्राप्त होंगे

test_sample2.py::test_file2_method1 FAILED
test_sample1.py::test_file1_method1 FAILED

============================================== FAILURES ==============================================
_________________________________________ test_file2_method1 _________________________________________
    def test_file2_method1():
    	x=5
    	y=6
       	assert x+1 == y,"test failed"
>      	assert x == y,"test failed because x=" + str(x) + " y=" + str(y)
E       AssertionError: test failed because x=5 y=6
E       assert 5 == 6
test_sample2.py:5: AssertionError

_________________________________________ test_file1_method1 _________________________________________
    @pytest.mark.only
    def test_file1_method1():
    	x=5
    	y=6
       	assert x+1 == y,"test failed"
>      	assert x == y,"test failed because x=" + str(x) + " y=" + str(y)
E       AssertionError: test failed because x=5 y=6
E       assert 5 == 6
test_sample1.py:8: AssertionError

================================= 2 tests deselected by '-kmethod1' ==================================
=============================== 2 failed, 2 deselected in 0.02 seconds ===============================

यहां आप अंत की ओर देख सकते हैं '-kmethod2' द्वारा 1 परीक्षण अचयनित किये गये जो test_file1_method2 और test_file2_method2 हैं

विभिन्न संयोजनों के साथ दौड़ने का प्रयास करें जैसे:-

py.test -k method -v - will run all the four methods
py.test -k methods -v – will not run any test as there is no test name matches the substring 'methods'

विकल्प 2) मार्करों द्वारा परीक्षण चलाएँ

Pytest हमें pytest मार्कर, @pytest.mark का उपयोग करके परीक्षण विधियों के लिए विभिन्न विशेषताएँ सेट करने की अनुमति देता है। परीक्षण फ़ाइल में मार्कर का उपयोग करने के लिए, हमें परीक्षण फ़ाइलों पर pytest आयात करना होगा।

यहाँ हम परीक्षण विधियों पर अलग-अलग मार्कर नाम लागू करेंगे और मार्कर नामों के आधार पर विशिष्ट परीक्षण चलाएँगे। हम प्रत्येक परीक्षण नाम पर मार्कर को परिभाषित कर सकते हैं

@pytest.mark.<name>.			

हम परीक्षण विधियों पर मार्कर set1 और set2 परिभाषित कर रहे हैं, और हम मार्कर नामों का उपयोग करके परीक्षण चलाएँगे। निम्न कोड के साथ परीक्षण फ़ाइलों को अपडेट करें

test_sample1.py

import pytest
@pytest.mark.set1
def test_file1_method1():
	x=5
	y=6
	assert x+1 == y,"test failed"
	assert x == y,"test failed because x=" + str(x) + " y=" + str(y)

@pytest.mark.set2
def test_file1_method2():
	x=5
	y=6
	assert x+1 == y,"test failed"

test_sample2.py

import pytest
@pytest.mark.set1
def test_file2_method1():
	x=5
	y=6
	assert x+1 == y,"test failed"
	assert x == y,"test failed because x=" + str(x) + " y=" + str(y)

@pytest.mark.set1
def test_file2_method2():
	x=5
	y=6
	assert x+1 == y,"test failed"

हम चिह्नित परीक्षण चला सकते हैं

py.test -m <name>
-m <name> mentions the marker name

py.test -m set1 चलाएँ। यह test_file1_method1, test_file2_method1, test_file2_method2 विधियाँ चलाएगा।

py.test -m set2 चलाने से test_file1_method2 चलेगा.

Pytest के साथ समानांतर में परीक्षण चलाएं

आम तौर पर, एक टेस्ट सूट में कई टेस्ट फ़ाइलें और सैकड़ों टेस्ट विधियाँ होंगी जिन्हें निष्पादित करने में काफी समय लगेगा। Pytest हमें समानांतर में परीक्षण चलाने की अनुमति देता है।

इसके लिए हमें सबसे पहले pytest-xdist चलाकर इंस्टॉल करना होगा

pip install pytest-xdist

Pytest के साथ समानांतर में परीक्षण चलाएं

अब आप निम्न प्रकार से परीक्षण चला सकते हैं:

py.test -n 4

-एन कई वर्कर्स का उपयोग करके परीक्षण चलाता है। उपरोक्त कमांड में, परीक्षण चलाने के लिए 4 वर्कर्स होंगे।

पायटेस्ट फिक्स्चर

फिक्सचर का उपयोग तब किया जाता है जब हम हर टेस्ट विधि से पहले कुछ कोड चलाना चाहते हैं। इसलिए हर टेस्ट में एक ही कोड को दोहराने के बजाय हम फिक्सचर को परिभाषित करते हैं। आम तौर पर, फिक्सचर का उपयोग डेटाबेस कनेक्शन को आरंभ करने, बेस पास करने आदि के लिए किया जाता है।

एक विधि को Pytest फिक्सचर के रूप में चिह्नित करके चिह्नित किया जाता है

@pytest.fixture

एक परीक्षण विधि फिक्स्चर को इनपुट पैरामीटर के रूप में उल्लेख करके पायटेस्ट फिक्स्चर का उपयोग कर सकती है।

निम्नलिखित कोड के साथ एक नई फ़ाइल test_basic_fixture.py बनाएँ

import pytest
@pytest.fixture
def supply_AA_BB_CC():
	aa=25
	bb =35
	cc=45
	return [aa,bb,cc]

def test_comparewithAA(supply_AA_BB_CC):
	zz=35
	assert supply_AA_BB_CC[0]==zz,"aa and zz comparison failed"

def test_comparewithBB(supply_AA_BB_CC):
	zz=35
	assert supply_AA_BB_CC[1]==zz,"bb and zz comparison failed"

def test_comparewithCC(supply_AA_BB_CC):
	zz=35
	assert supply_AA_BB_CC[2]==zz,"cc and zz comparison failed"

यहाँ

  • हमारे पास supply_AA_BB_CC नाम का एक फिक्सचर है। यह विधि 3 मानों की सूची लौटाएगी।
  • हमारे पास प्रत्येक मान की तुलना करने के लिए 3 परीक्षण विधियाँ हैं।

प्रत्येक परीक्षण फ़ंक्शन में एक इनपुट तर्क होता है जिसका नाम उपलब्ध फ़िक्सचर से मेल खाता है। फिर Pytest संबंधित फ़िक्सचर विधि को आमंत्रित करता है और लौटाए गए मान इनपुट तर्क में संग्रहीत किए जाएँगे, यहाँ सूची [25,35,45] है। अब तुलना के लिए परीक्षण विधियों में सूची आइटम का उपयोग किया जा रहा है।

अब परीक्षण चलाएँ और परिणाम देखें

 py.test test_basic_fixture
test_basic_fixture.py::test_comparewithAA FAILED                                                                                                                                                                                       
test_basic_fixture.py::test_comparewithBB PASSED                                                                                                                                                                                       
test_basic_fixture.py::test_comparewithCC FAILED
                                                                                                                                                                                       
============================================== FAILURES ==============================================
_________________________________________ test_comparewithAA _________________________________________
supply_AA_BB_CC = [25, 35, 45]
    def test_comparewithAA(supply_AA_BB_CC):
    	zz=35
>   	assert supply_AA_BB_CC[0]==zz,"aa and zz comparison failed"
E    AssertionError: aa and zz comparison failed
E    assert 25 == 35
test_basic_fixture.py:10: AssertionError

_________________________________________ test_comparewithCC _________________________________________
supply_AA_BB_CC = [25, 35, 45]
    def test_comparewithCC(supply_AA_BB_CC):
    	zz=35
>   	assert supply_AA_BB_CC[2]==zz,"cc and zz comparison failed"
E    AssertionError: cc and zz comparison failed
E    assert 45 == 35
test_basic_fixture.py:16: AssertionError
================================= 2 failed, 1 passed in 0.05 seconds =================================

परीक्षण test_comparewithBB पारित हो गया है क्योंकि zz=BB=35 है, और शेष 2 परीक्षण विफल हो गए हैं।

फ़िक्स्चर विधि का दायरा केवल उस परीक्षण फ़ाइल के भीतर ही होता है जिसे परिभाषित किया गया है। यदि हम किसी अन्य परीक्षण फ़ाइल में फ़िक्स्चर तक पहुँचने का प्रयास करते हैं, तो हमें फ़िक्स्चर कहते हुए एक त्रुटि मिलेगी 'supply_AA_BB_CC' नहीं मिला अन्य फ़ाइलों में परीक्षण विधियों के लिए.

एकाधिक परीक्षण फ़ाइलों के विरुद्ध एक ही फिक्सचर का उपयोग करने के लिए, हम conftest.py नामक फ़ाइल में फिक्सचर विधियाँ बनाएंगे।

आइए इसे नीचे दिए गए PyTest उदाहरण से देखें। निम्नलिखित कोड के साथ 3 फ़ाइलें conftest.py, test_basic_fixture.py, test_basic_fixture2.py बनाएँ

conftest.py

import pytest
@pytest.fixture
def supply_AA_BB_CC():
	aa=25
	bb =35
	cc=45
	return [aa,bb,cc]

test_basic_fixture.py

import pytest
def test_comparewithAA(supply_AA_BB_CC):
	zz=35
	assert supply_AA_BB_CC[0]==zz,"aa and zz comparison failed"

def test_comparewithBB(supply_AA_BB_CC):
	zz=35
	assert supply_AA_BB_CC[1]==zz,"bb and zz comparison failed"

def test_comparewithCC(supply_AA_BB_CC):
	zz=35
	assert supply_AA_BB_CC[2]==zz,"cc and zz comparison failed"

test_basic_fixture2.py

import pytest
def test_comparewithAA_file2(supply_AA_BB_CC):
	zz=25
	assert supply_AA_BB_CC[0]==zz,"aa and zz comparison failed"

def test_comparewithBB_file2(supply_AA_BB_CC):
	zz=25
	assert supply_AA_BB_CC[1]==zz,"bb and zz comparison failed"

def test_comparewithCC_file2(supply_AA_BB_CC):
	zz=25
	assert supply_AA_BB_CC[2]==zz,"cc and zz comparison failed"

pytest सबसे पहले परीक्षण फ़ाइल में फ़िक्स्चर की तलाश करेगा और यदि नहीं मिलता है तो वह conftest.py में देखेगा

नीचे दिए गए परिणाम प्राप्त करने के लिए py.test -k test_comparewith -v द्वारा परीक्षण चलाएँ

test_basic_fixture.py::test_comparewithAA FAILED  
test_basic_fixture.py::test_comparewithBB PASSED 
test_basic_fixture.py::test_comparewithCC FAILED 
test_basic_fixture2.py::test_comparewithAA_file2 PASSED 
test_basic_fixture2.py::test_comparewithBB_file2 FAILED 
test_basic_fixture2.py::test_comparewithCC_file2 FAILED

पायटेस्ट पैरामीटराइज्ड टेस्ट

परीक्षण को पैरामीटराइज़ करने का उद्देश्य तर्कों के कई सेटों के विरुद्ध परीक्षण चलाना है। हम इसे @pytest.mark.parametrize द्वारा कर सकते हैं।

हम इसे नीचे दिए गए PyTest उदाहरण में देखेंगे। यहाँ हम एक टेस्ट विधि में 3 तर्क पास करेंगे। यह टेस्ट विधि पहले 2 तर्कों को जोड़ेगी और तीसरे तर्क से इसकी तुलना करेगी।

नीचे दिए गए कोड के साथ परीक्षण फ़ाइल test_addition.py बनाएँ

import pytest
@pytest.mark.parametrize("input1, input2, output",[(5,5,10),(3,5,12)])
def test_add(input1, input2, output):
	assert input1+input2 == output,"failed"

यहाँ टेस्ट विधि 3 तर्क स्वीकार करती है- input1, input2, output. यह input1 और input2 को जोड़ता है और आउटपुट के साथ तुलना करता है।

आइए py.test -k test_add -v द्वारा परीक्षण चलाएं और परिणाम देखें

test_addition.py::test_add[5-5-10] PASSED                                                                                                                                                                                              
test_addition.py::test_add[3-5-12] FAILED                                                                                                                                                                                              
============================================== FAILURES ==============================================
__________________________________________ test_add[3-5-12] __________________________________________
input1 = 3, input2 = 5, output = 12
    @pytest.mark.parametrize("input1, input2, output",[(5,5,10),(3,5,12)])
    def test_add(input1, input2, output):
>   	assert input1+input2 == output,"failed"
E    AssertionError: failed
E    assert (3 + 5) == 12
test_addition.py:5: AssertionError

आप देख सकते हैं कि परीक्षण 2 बार चलाए गए – एक 5+5 ==10 की जाँच कर रहा था और दूसरा 3+5 ==12 की जाँच कर रहा था

test_addition.py::test_add[5-5-10] पास

test_addition.py::test_add[3-5-12] विफल

Pytest Xfail / टेस्ट छोड़ें

कुछ स्थितियाँ ऐसी होंगी जहाँ हम परीक्षण नहीं करना चाहेंगे, या परीक्षण का मामला किसी विशेष समय के लिए प्रासंगिक नहीं है। ऐसी स्थितियों में, हमारे पास परीक्षण में असफल होने या परीक्षण छोड़ने का विकल्प होता है

xfailed परीक्षण निष्पादित किया जाएगा, लेकिन इसे आंशिक रूप से विफल या उत्तीर्ण परीक्षणों के रूप में नहीं गिना जाएगा। यदि वह परीक्षण विफल हो जाता है तो कोई ट्रेसबैक प्रदर्शित नहीं किया जाएगा। हम xfail परीक्षणों का उपयोग कर सकते हैं

@pytest.mark.xfail.

किसी परीक्षण को छोड़ने का मतलब है कि परीक्षण निष्पादित नहीं किया जाएगा। हम परीक्षण को छोड़ सकते हैं

@pytest.mark.skip.

नीचे दिए गए कोड से test_addition.py को संपादित करें

import pytest
@pytest.mark.skip
def test_add_1():
	assert 100+200 == 400,"failed"

@pytest.mark.skip
def test_add_2():
	assert 100+200 == 300,"failed"

@pytest.mark.xfail
def test_add_3():
	assert 15+13 == 28,"failed"

@pytest.mark.xfail
def test_add_4():
	assert 15+13 == 100,"failed"

def test_add_5():
	assert 3+2 == 5,"failed"

def test_add_6():
	assert 3+2 == 6,"failed"

यहाँ

  • test_add_1 और test_add_2 को छोड़ दिया जाएगा और उनका निष्पादन नहीं किया जाएगा।
  • test_add_3 और test_add_4 xfailed हैं। ये परीक्षण निष्पादित किए जाएँगे और xfailed(परीक्षण विफलता पर) या xpassed(परीक्षण पास होने पर) परीक्षणों का हिस्सा होंगे। विफलताओं के लिए कोई ट्रेसबैक नहीं होगा।
  • test_add_5 और test_add_6 निष्पादित किए जाएंगे और test_add_6 के पास होने तक test_add_5 ट्रेसबैक के साथ विफलता की रिपोर्ट करेगा

py.test test_addition.py -v द्वारा परीक्षण निष्पादित करें और परिणाम देखें

test_addition.py::test_add_1 SKIPPED
test_addition.py::test_add_2 SKIPPED
test_addition.py::test_add_3 XPASS
test_addition.py::test_add_4 xfail
test_addition.py::test_add_5 PASSED
test_addition.py::test_add_6 FAILED

============================================== FAILURES ==============================================
_____________________________________________ test_add_6 _____________________________________________
    def test_add_6():
>   	assert 3+2 == 6,"failed"
E    AssertionError: failed
E    assert (3 + 2) == 6
test_addition.py:24: AssertionError

================ 1 failed, 1 passed, 2 skipped, 1 xfailed, 1 xpassed in 0.07 seconds =================

परिणाम XML

हम XML प्रारूप में परीक्षण परिणाम बना सकते हैं जिसे हम आगे की प्रक्रिया के लिए निरंतर एकीकरण सर्वर को खिला सकते हैं। यह इस प्रकार किया जा सकता है

py.test test_sample1.py -v –junitxml="result.xml"

result.xml परीक्षण निष्पादन परिणाम रिकॉर्ड करेगा। नीचे एक नमूना result.xml पाएँ

<?xml version="1.0" encoding="UTF-8"?>
<testsuite errors="0" failures="1" name="pytest" skips="0" tests="2" time="0.046">
   <testcase classname="test_sample1" file="test_sample1.py" line="3" name="test_file1_method1" time="0.001384973526">
     <failure message="AssertionError:test failed because x=5 y=6 assert 5 ==6">
    @pytest.mark.set1
    def test_file1_method1():
    	x=5
    	y=6
       	assert x+1 == y,"test failed"
>      	assert x == y,"test failed because x=" + str(x) + " y=" + str(y)
E       AssertionError: test failed because x=5 y=6
E       assert 5 == 6
         test_sample1.py:9: AssertionError
    </failure>
   </testcase>
   <testcase classname="test_sample1" file="test_sample1.py" line="10" name="test_file1_method2" time="0.000830173492432" />
</testsuite>

से हम कुल दो परीक्षण देख सकते हैं जिनमें से एक विफल रहा। नीचे आप प्रत्येक निष्पादित परीक्षण के बारे में विवरण देख सकते हैं टैग।

Pytest फ्रेमवर्क एक API का परीक्षण

अब हम API का परीक्षण करने के लिए एक छोटा सा pytest फ्रेमवर्क बनाएंगे। यहाँ इस्तेमाल किया गया API एक निःशुल्क API है। https://reqres.in/यह वेबसाइट केवल परीक्षण योग्य API प्रदान करने के लिए है। यह वेबसाइट हमारा डेटा संग्रहीत नहीं करती है।

यहाँ हम कुछ परीक्षण लिखेंगे

  • कुछ उपयोगकर्ताओं की सूची
  • उपयोगकर्ताओं के साथ लॉगिन करें

नीचे दिए गए कोड के साथ नीचे दी गई फ़ाइलें बनाएं

conftest.py - एक फिक्सचर है जो सभी परीक्षण विधियों के लिए आधार यूआरएल प्रदान करेगा

import pytest
@pytest.fixture
def supply_url():
	return "https://reqres.in/api"

test_list_user.py – इसमें वैध और अवैध उपयोगकर्ताओं को सूचीबद्ध करने के लिए परीक्षण विधियाँ शामिल हैं

  • test_list_valid_user वैध उपयोगकर्ता के लिए परीक्षण करता है और प्रतिक्रिया को सत्यापित करता है
  • test_list_invaliduser अमान्य उपयोगकर्ता के लिए परीक्षण करता है और प्रतिक्रिया को सत्यापित करता है
import pytest
import requests
import json
@pytest.mark.parametrize("userid, firstname",[(1,"George"),(2,"Janet")])
def test_list_valid_user(supply_url,userid,firstname):
	url = supply_url + "/users/" + str(userid)
	resp = requests.get(url)
	j = json.loads(resp.text)
	assert resp.status_code == 200, resp.text
	assert j['data']['id'] == userid, resp.text
	assert j['data']['first_name'] == firstname, resp.text

def test_list_invaliduser(supply_url):
	url = supply_url + "/users/50"
	resp = requests.get(url)
	assert resp.status_code == 404, resp.text

test_login_user.py - इसमें लॉगिन कार्यक्षमता के परीक्षण के लिए परीक्षण विधियाँ शामिल हैं।

  • test_login_valid ईमेल और पासवर्ड के साथ वैध लॉगिन प्रयास का परीक्षण करता है
  • test_login_no_password पासवर्ड पास किए बिना अमान्य लॉगिन प्रयास का परीक्षण करता है
  • test_login_no_email ईमेल भेजे बिना अमान्य लॉगिन प्रयास का परीक्षण करता है।
import pytest
import requests
import json
def test_login_valid(supply_url):
	url = supply_url + "/login/" 
	data = {'email':'test@test.com','password':'something'}
	resp = requests.post(url, data=data)
	j = json.loads(resp.text)
	assert resp.status_code == 200, resp.text
	assert j['token'] == "QpwL5tke4Pnpja7X", resp.text

def test_login_no_password(supply_url):
	url = supply_url + "/login/" 
	data = {'email':'test@test.com'}
	resp = requests.post(url, data=data)
	j = json.loads(resp.text)
	assert resp.status_code == 400, resp.text
	assert j['error'] == "Missing password", resp.text

def test_login_no_email(supply_url):
	url = supply_url + "/login/" 
	data = {}
	resp = requests.post(url, data=data)
	j = json.loads(resp.text)
	assert resp.status_code == 400, resp.text
	assert j['error'] == "Missing email or username", resp.text

py.test -v का उपयोग करके परीक्षण चलाएँ

परिणाम इस प्रकार देखें

test_list_user.py::test_list_valid_user[1-George] PASSED                                                                                                                                                                               
test_list_user.py::test_list_valid_user[2-Janet] PASSED                                                                                                                                                                                
test_list_user.py::test_list_invaliduser PASSED                                                                                                                                                                                        
test_login_user.py::test_login_valid PASSED                                                                                                                                                                                            
test_login_user.py::test_login_no_password PASSED                                                                                                                                                                                      
test_login_user.py::test_login_no_email PASSED

परीक्षणों को अपडेट करें और विभिन्न आउटपुट आज़माएँ

सारांश

इस PyTest ट्यूटोरियल में, हमने कवर किया

  • pytest का उपयोग करके इंस्टॉल करें पाइप स्थापित करें पायथेस्ट=2.9.1
  • सरल pytest प्रोग्राम और इसे py.test कमांड के साथ चलाएं।
  • अभिकथन कथन, assert x==y, या तो सत्य या असत्य लौटाएगा।
  • pytest परीक्षण फ़ाइलों और विधियों की पहचान कैसे करता है।
  • परीक्षण फ़ाइलें शुरू होती हैं परीक्षा_ या के साथ समाप्त हो रहा है _परीक्षा
  • से शुरू होने वाली परीक्षण विधियाँ परीक्षण
  • py.test कमांड उस फ़ोल्डर और सबफ़ोल्डर्स में सभी टेस्ट फ़ाइलें चलाएगा। किसी खास फ़ाइल को चलाने के लिए, हम py.test कमांड का इस्तेमाल कर सकते हैं
  • परीक्षण विधियों का एक उपसमूह चलाएँ
  • उपस्ट्रिंग matching.py.test -k द्वारा परीक्षण नामों का समूहन -v सभी परीक्षण चलाएगा इसके नाम में.
  • मार्कर द्वारा परीक्षण चलाएँ। @pytest.mark का उपयोग करके परीक्षणों को चिह्नित करें। और pytest -m का उपयोग करके परीक्षण चलाएं चिह्नित परीक्षण चलाने के लिए .
  • समानांतर परीक्षण चलाएँ
  • pip install pytest-xdist का उपयोग करके pytest-xdist स्थापित करें
  • py.test -n NUM का उपयोग करके परीक्षण चलाएँ जहाँ NUM श्रमिकों की संख्या है
  • @pytest.fixture के साथ विधि को चिह्नित करके प्रत्येक परीक्षण से पहले कोड चलाने के लिए फिक्सचर विधियाँ बनाना
  • फ़िक्स्चर विधि का दायरा उस फ़ाइल के भीतर होता है जिसमें उसे परिभाषित किया गया है।
  • Conftest.py फ़ाइल में परिभाषित करके एक फ़िक्स्चर विधि को एकाधिक परीक्षण फ़ाइलों में एक्सेस किया जा सकता है।
  • एक परीक्षण विधि Pytest फिक्सचर को इनपुट तर्क के रूप में उपयोग करके उस तक पहुंच सकती है।
  • इनपुट के एकाधिक सेट के विरुद्ध परीक्षण चलाने के लिए पैरामीटरीकरण करना।
    @pytest.mark.parametrize(“इनपुट1, इनपुट2, आउटपुट”,[(5,5,10),(3,5,12)])
    def test_add(इनपुट1, इनपुट2, आउटपुट):
    इनपुट1+इनपुट2 == आउटपुट,"विफल"
    इनपुट (5,5,10) और (3,5,12) के साथ परीक्षण चलाएगा
  • @pytets.mark.skip और @pytest.mark.xfail का उपयोग करके स्किप/xfail परीक्षण
  • py.test test_sample1.py -v –junitxml="result.xml" का उपयोग करके XML प्रारूप में परीक्षण परिणाम बनाएं जो निष्पादित परीक्षण विवरण को कवर करता है
  • API का परीक्षण करने के लिए एक नमूना pytest फ्रेमवर्क