इस ट्यूटोरियल में, हम इकाई परीक्षण के बारे में जानेंगे। unittest . का उपयोग करके अंतर्निहित मॉड्यूल। सॉफ्टवेयर विकास में परीक्षण एक प्रमुख भूमिका निभाता है। प्रोडक्शन में जाने से पहले आप मुद्दों को जानेंगे।
हम unittest . नामक बिल्ट-इन मॉड्यूल का उपयोग करके पायथन में परीक्षण की मूल बातें सीखेंगे . आइए ट्यूटोरियल में कूदें।
इकाई परीक्षण क्या है?
यदि आप लॉगिन सिस्टम को एक उदाहरण के रूप में लेते हैं। लॉगिन फॉर्म में प्रत्येक फ़ील्ड एक इकाई/घटक है। और उन इकाइयों/घटकों की कार्यक्षमता का परीक्षण करना इकाई परीक्षण . के रूप में जाना जाता है
उदाहरण
आइए देखते हैं एकतरफा ढांचे की बुनियादी संरचना।
# importing unittest module import unittest # unittest will test all the methods whose name starts with 'test' class SampleTest(unittest.TestCase): # return True or False def test(self): self.assertTrue(True) # running the test unittest.main()
आउटपुट
यदि आप उपरोक्त कार्यक्रम चलाते हैं, तो आपको निम्नलिखित परिणाम प्राप्त होंगे।
---------------------------------------------------------------------- Ran 1 test in 0.001s OK
2.स्ट्रिंग विधियों का परीक्षण
अब, हम नमूना परीक्षण मामलों के साथ विभिन्न स्ट्रिंग विधियों का परीक्षण करने जा रहे हैं। याद रखें कि विधि का नाम परीक्षण with से शुरू होना चाहिए
आइए प्रत्येक विधि के बारे में संक्षिप्त परिचय देखें जिसे हम लिखने जा रहे हैं।
-
test_string_equality
-
assertEqaul . का उपयोग करके यह विधि परीक्षण करती है कि दो तार बराबर हैं या नहीं unittest.TestCase. . की विधि
-
-
test_string_case
-
assertEqaul . का उपयोग करके यह विधि परीक्षण करती है कि दो स्ट्रिंग केस बराबर हैं या नहीं unittest.TestCase. . की विधि
-
-
test_is_string_upper
-
यह विधि परीक्षण करती है कि स्ट्रिंग अपर केस में है या नहीं assertTrue . का उपयोग नहीं कर रही है और गलत जोर दें unittest.TestCase . के तरीके ।
-
उदाहरण
# importing unittest module
import unittest
class TestingStringMethods(unittest.TestCase):
# string equal
def test_string_equality(self):
# if both arguments are equal then it's succes
self.assertEqual('ttp' * 5, 'ttpttpttpttpttp')
# comparing the two strings
def test_string_case(self):
# if both arguments are equal then it's succes
self.assertEqual('tutorialspoint'.upper(), 'TUTORIALSPOINT')
# checking whether a string is upper or not
def test_is_string_upper(self):
# used to check whether the statement is True or False
# the result of expression inside the **assertTrue** must be True to pass the test case
# the result of expression inside the **assertFalse** must be False to pass the test case
self.assertTrue('TUTORIALSPOINT'.isupper())
self.assertFalse('TUTORIALSpoint'.isupper())
# running the tests
unittest.main() चल रहा है आउटपुट
यदि आप उपरोक्त कोड को चलाते हैं, तो सभी टेस्ट केस पास होने पर आपको निम्न परिणाम मिलेंगे।
... ---------------------------------------------------------------------- Ran 3 tests in 0.001s OK
उदाहरण
विफल परीक्षण केस आउटपुट देखने के लिए निम्न प्रोग्राम चलाएँ।
# importing unittest module
import unittest
class TestingStringMethods(unittest.TestCase):
# string equal
def test_string_equality(self):
# if both arguments are equal then it's succes
self.assertEqual('ttp' * 5, 'ttpttpttpttpttp')
# comparing the two strings
def test_string_case(self):
# if both arguments are equal then it's succes
self.assertEqual('tutorialspoint'.upper(), 'TUTORIALSPOINT')
# checking whether a string is upper or not
def test_is_string_upper(self):
# used to check whether the statement is True or False
# the result of expression inside the **assertTrue** must be True to pass the test case
# the result of expression inside the **assertFalse** must be False to pass the test case
self.assertTrue('TUTORIALSPOINt'.isupper())
self.assertFalse('TUTORIALSpoint'.isupper())
# running the tests
unittest.main() चल रहा है आउटपुट
यदि आप उपरोक्त प्रोग्राम चलाते हैं, तो आपको निम्न परिणाम प्राप्त होंगे।
======================================================================
FAIL: test_is_string_upper (__main__.TestingStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
File "p:/Python Work/Stopwatch/practice.py", line 21, in test_is_string_upper
self.assertTrue('TUTORIALSPOINt'.isupper())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 3 tests in 0.016s
FAILED (failures=1) हम संदेश को विफल कर देंगे, यहां तक कि सभी परीक्षण मामलों में एक परीक्षण मामला भी विफल हो गया है।
निष्कर्ष
यदि आपको ट्यूटोरियल में कोई संदेह है, तो उनका टिप्पणी अनुभाग में उल्लेख करें।