Unit testing with unittest or pytest In Hindi

Unit testing with unittest or pytest In Hindi

पाइथन प्रोग्रामिंग लैंग्वेज में यूनिट सॉफ्टवेयर प्रोग्राम टेस्टिंग एक यूजर डिफाइन पाइथन प्रोगाम बेस्ड सॉफ्टवेयर सोर्स कोड टेस्टिंग टेक्निक या मेथड है, जिसमें किसी पाइथन प्रोग्राम सोर्स कोड के मल्टीप्ल इंडिविजुअल पोरशन एलिमेंट, जैसे प्रोग्राम डिफाइन फंक्शन, मेथड, या स्माल क्लास—को अलग-अलग इंडिविजुअल मल्टीप्ल लेवल पर टेस्ट या एनालाइज किया जाता है, जिससे कि मौजूदा पाइथन सॉफ्टवेयर सोर्स कोड में आसानी से यह चेक किया जा सके कि वे प्रॉपर आर्डर में काम कर रहे हैं, या नहीं है।

Unit testing with unittest or pytest In Hindi

In Python programming, there are two popular tools for unit testing.

  • unittest – यह पाइथन प्रोग्रामिंग लैंग्वेज का एक बिल्ट-इन सॉफ्टवेयर टेस्टिंग फ्रेमवर्क टूल या प्लेटफार्म है।
  • pytest – यह भी एक पॉपुलर थर्ड-पार्टी पाइथन सॉफ्टवेयर सोर्स कोड टेस्टिंग फ्रेमवर्क टूल है, जो अपने बेसिक सिंटैक्स और पावरफुल टेस्टिंग फीचर्स के लिए अधिक यूज़ किया जाता है।

What is unit testing in Python programming?

यहाँ माना की हमारे पास एक पाइथन बेस्ड यूजर डिफाइन फंक्शन है, जो मौजूदा प्रोग्राम में दो नंबर वैल्यू को ऐड करता है.

def total(p, q):

return p + q

यहाँ हम उम्मीद करते हैं.

total(4, 8) → 12

यहाँ यह एक पाइथन बेस्ड यूनिट टेस्ट को ऑटोमैटिकली चेक करता है।

Python Program Function

↓

Unit Test

↓

Supposed Result = Actual Program Result?

↓

SUCESS / FAIL

पाइथन प्रोग्राम में सोर्स कोड यूनिट टेस्टिंग इश्यूज या एरर को इमीडियेट फाइंड करने में हेल्प करता है, और प्रोग्राम सोर्स कोड को सिक्योर आर्डर में रिप्लेस या मॉडिफाई करना इजी करता है।

Why is unit testing important in Python?

Unit testing has the following advantages.

  • जल्दी सॉफ्टवेयर या प्रोग्राम एरर को फाइंड करना।
  • यह एप्लिकेशन प्रोग्राम सॉफ्टवेयर डिप्लॉय होने से पहले उसमे बग को फाइंड कर सकता है।
  • इजी प्रोगाम सॉफ्टवेयर डीबगिंग करना।
  • यदि कोई पाइथन प्रोग्राम सॉफ्टवेयर टेस्ट फेल हो जाता है, तो यह डेवलपर को यह फाइंड करने में हेल्प कर सकता है, जिससे कि प्रोग्राम या सॉफ्टवेयर के किस पार्ट सोर्स कोड लाइन में प्रॉब्लम है।
  • यह आपके प्रोग्राम सोर्स कोड क्वालिटी को बेहतर करता है.
  • पाइथन यूनिट टेस्टिंग सॉफ्टवेयर डेवलपर्स को स्माल, प्रॉपर आर्डर में ऑर्गनाइज़्ड फ़ंक्शन क्रिएट करने के लिए बढ़ावा देती है।
  • आसानी से पाइथन सॉफ्टवेयर सोर्स कोड में सिक्योर मॉडिफिकेशन अल्लॉव करता है
  • जब मौजूदा पाइथन प्रोग्राम सोर्स कोड में मॉडिफिकेशन किया जाता है, तो यह टेस्ट आपको फाइंड करने में हेल्प कर सकते हैं कि पहले से काम कर रही प्रोग्राम की फ़ंक्शनैलिटी में कोई दिक्कत तो नहीं आई है।
  • आसानी से सॉफ्टवेयर प्रोग्राम ऑटोमेशन में हेल्प करता है.
  • एक बार प्रोग्राम सॉफ्टवेयर टेस्ट लिखे जाने के बाद, उन्हें हर रिज़ल्ट को मैन्युअल रूप से चेक किए बिना बार-बार रन किया जा सकता है।

Testing with Unit Tests in Python.

यूनिटटेस्ट Python टेस्टर के साथ अपने आप आता है, इस वजह से इसे सामान्य रूप से आपको अलग से मैन्युअल रूप से इंस्टॉल करने की ज़रूरत नहीं होती है। आप पाइथन में इसे इस तरह से इंपोर्ट कर सकते है.

import unittest

यहाँ माना की हमारे पास एक calculation.py नाम की एक पाइथन प्रोग्राम सोर्स कोड फ़ाइल है.

def plus(p, q):

return p + q

def minus(p, q):

return p – q

यहाँ हम एक पाइथन प्रोग्राम टेस्ट फ़ाइल क्रिएट कर सकते हैं, जिसका नाम है.

test_calculation.py

Writing a UnitTest test in Python.

import unittest

from calculation import plus minus

class TestCalculation(unittest.TestCase):

def test_plus(self):

self.assertEqual(plus(3, 5), 8)

def test_minus(self):

self.assertEqual(minus(9, 4), 5)

if __name__ == “__main__”:

unittest.main()

Here in this example.

  • TestCalculation यहाँ यह एक यूजर डिफाइन टेस्ट क्लास है।
  • test_plus() plus() यह एक प्लस फ़ंक्शन वैल्यू को टेस्ट करता है।
  • test_minus() minus() यह एक माइनस फ़ंक्शन वैल्यू को टेस्ट करता है।
  • assertEqual() यह फंक्शन चेक करता है कि आपस में दो वैल्यू बराबर हैं, या नहीं है।
  • unittest.main() यह फंक्शन एक टेस्ट को रन करता है।

Running unittest in Python.

अपने कंप्यूटर में टर्मिनल से, चलाएँ।

python -m unittest test_calculation.py

यदि यहाँ इस प्रोग्राम में सब कुछ प्रॉपर डिफाइन है, तो आपको आपके प्रोग्राम का एक करेक्ट आउटपुट डिस्प्ले होगा, जो आपको इंडीकेट करता कि टेस्ट प्रॉपर पास हो गए हैं।

For example…

..

———————————————————————

0.001s में 2 टेस्ट चलाए

OK

हर . यह आपको एक सफल टेस्ट डिस्प्ले करता है।

Common unittest Assertions in python

Assertion functionUnittest assertion’s purpose with python
Assertequal(p, q) functionHere we use this function to checks whether p == q equal or not condition
Assertnotequal(p, q) functionHere we use this function to checks whether p != q is not equal to condition
Asserttrue(p) functionHere we use this function to checks whether p is true value statement
Assertfalse(p) functionHere we use this function to checks whether p is false value statement
Assertisnone(p) functionHere we use this function to checks whether p is none or not value
Assertin(p, q) functionHere we use this function to checks whether p is contained in q value statement
Assertraises()functionHere we use this function to checks whether code raises an expected exception or not

Example of unittest in Python.

import unittest

class TestExample(unittest.TestCase):

def test_values(self):

self.assertEqual(3, 3)

self.assertNotEqual(4, 7)

self.assertTrue(9 > 4)

self.assertIn(“Javascript”, [“Javascript”, “Matlab”])

if __name__ == “__main__”:

unittest.main()

Exception Testing in Python.

पाइथन प्रोग्राम में यूनिट टेस्ट में यह भी चेक कर सकते हैं कि कोई यूजर डिफाइन फ़ंक्शन प्रॉपर आर्डर से एरर को डिस्प्ले करता है, या नहीं । यहाँ माना की,

def division(p, q):

if q == 0:

raise ValueError(“Cannot division by zero value”)

return p / q

The test here could be.

import unittest

class TestDivision(unittest.TestCase):

def test_division_by_zero(self):

with self.assertRaises(ValueError):

division(30, 0)

if __name__ == “__main__”:

unittest.main()

यह मौजूदा प्रोग्राम में वेरिफ़ाई करता है कि ज़ीरो से डिवाइड करने पर किसी प्रकार की कोई एक्सपेक्टेड एक्सेप्शन रेज होती है।

The setUp() and tearDown() functions in Python testing.

पाइथन प्रोग्राम सॉफ्टवेयर टेस्टिंग में unittest एक ऐसे मेथड कांसेप्ट को प्रोवाइड करता है, जिसका यूज़ टेस्ट रिसोर्स तैयार करने में और उन्हें क्लीनअप करने में किया जा सकता है।

setUp()

यह हर पाइथन प्रोग्राम में टेस्ट से पहले रन होता है।

tearDown()

यह हर पाइथन प्रोग्राम में टेस्ट के बाद रन होता है।

Example of the setUp() and tearDown() functions.

import unittest

class TestCalculation(unittest.TestCase):

def setUp(self):

self.p = 5

self.q = 3

def test_plus(self):

self.assertEqual(self.p + self.q, 8)

def test_minus(self):

self.assertEqual(self.p – self.q, 2)

def tearDown(self):

pass

if __name__ == “__main__”:

unittest.main()

पाइथन में इसे तब यूज़ किया जाता है, जब पाइथन यूजर को टेस्ट के लिए कॉमन सेटअप या क्लीनअप की आवश्यकता होती है।

Testing with pytest in Python programming.

पाइथन प्रोग्रामिंग लैंग्वेज में pytest एक पॉपुलर थर्ड-पार्टी पाइथन टेस्टिंग टूल या लाइब्रेरी फ्रेमवर्क है। यूनिटटेस्ट पाइथन टेस्टिंग के अपोजिट, यह डिफ़ॉल्ट रूप से पाइथन में बिल्ट-इन इन्क्लुड नहीं है।

Install pytest in Python like this.

pip install pytest

or

python -m pip install pytest

Write a test using pytest tests in Python programming.

पाइथन प्रोग्रामिंग में उसी plus() फ़ंक्शन का यूज़ करके इसे अप्लाई करते है.

def plus(p, q):

return p + q

यह आपके टेस्ट में बहुत इजी हो सकता है.

from calculation import plus

def test_plus():

assert plus(3, 7 ) == 10

यहाँ आप ध्यान दें कि हमें टेस्ट क्लास क्रिएट करने या self.assertEqual() का यूज़ करने की ज़रूरत नहीं है।

आप पाइथन में assert स्टेटमेंट का यूज़ करके इसे डायरेक्ट अप्लाई कर सकते है।

Running pytest in Python.

यदि आपके यहाँ टेस्ट फ़ाइल का नाम calculation है.

test_calculation.py

run:

pytest

or:

python -m pytest

यह आपको एक सक्सेस्फुल पाइथन प्रोग्राम रन टेस्ट को डिस्प्ले करता है, जिससे कि टेस्ट पास हो गया है।

Performing multiple case tests with Python pytest.

पाइथन प्रोग्रामिंग लैंग्वेज में pytest का काम एक फ़ीचर पैरामीटराइज़ेशन को चेक या टेस्ट करना है।

For example.

import pytest

@pytest.mark.parametrize(

“p, q, assume”,

[

(7, 2, 4),

(3, 9, 8),

(-2, 7, 2 ),

(0, 0, 0)

]

)

def test_plus(p, q, assume):

assert p + q == assume

यहाँ इस प्रोग्राम में एक ही टेस्ट फ़ंक्शन कई अलग-अलग इनपुट के साथ रन किया जाता है।

Testing exceptions with Python pytest.

pytest, pytest.raises() एक फंक्शन प्रोवाइड करता है।

import pytest

def division(p, q):

if q == 0:

raise ValueError(“Cannot division by zero value”)

return p / q

def test_division_by_zero():

with pytest.raises(ValueError):

division(20, 0)

यह आपके प्रोग्राम में चेक करता है कि एक्सपेक्टेड एक्सेप्शन जनरेट होता है, या नहीं है।

Fixture feature in pytest.

एक फिक्सचर टेस्ट के लिए दोबारा यूज़ होने वाला सेटअप डेटा या रिसोर्स प्रोवाइड करता है।

Example of a Fixture feature.

import pytest

@pytest.fixture

def integers():

return [9, 7, 8, 4, 3 , 2]

def test_integers(integers):

assert len(integers) == 5

assert 4 in integers

यहां, इस एक्साम्प्ल में integers एक फिक्सचर टेस्ट को डेटा प्रोवाइड करता है। पाइथन में फिक्स्चर स्पेशली रूप से यूज़फुल हैं. जैसे,

  • डेटाबेस सेटअप करना
  • टेम्पररी फ़ाइलें बनाना
  • टेस्ट डेटा को चेक करना
  • दोबारा इस्तेमाल होने वाले ऑब्जेक्ट बनाना
  • रिसोर्स तैयार करना और क्लियर करना

Main difference between unittest vs pytest in python

Each featureUnittest testing frameworkPytest testing framework
Included with pythonUnittest included built-in in python library, you can just use it when you needPytest is third party module, we need to add it manually in python
Installation requiredIt’s a built-in module you no need to install separate unittest in python libraryPytest is third party library test module, you need to install with pip python command
Test syntax or methodPython unittest test framework is more structured syntax to followPytest python framework is generally simpler syntax
Test classes availablePython unittest test framework commonly used for testing purposePytest python framework often unnecessary
Assertions methodPython unittest test framework allow self.assertequal() etc.Pytest python framework support python assert
Fixtures conceptSetup() / teardown() and other mechanismsPytest python framework provides powerful fixture system
Parameterized tests allowPython unittest test supported, but more verbose usePytest python framework very convenient during use
Plugins supportPython unittest test plugins support available, you can use them when needPytest python framework large plugin ecosystem supported
Best use forPython unittest test standard-library testing and structured suites for use casePytest python framework concise, flexible, feature-rich testing

Structure of a project with examples in Python.

पाइथन प्रोग्राम में एक सिंपल प्रोजेक्ट कुछ ऐसा डिस्प्ले हो सकता है.

testproject/

│

├── calculation.py

│

└── tests/

└── test_calculation.py

calculation.py

def plus(p, q):

return p + q

def plural(p, q):

return p * q

test_calculation.py

Using pytest in Python.

from calculation import plus, multiply

def test_plus():

assert plus(4, 7) == 11

def test_multiply():

assert multiply(3, 5) == 15

run.

pytest

पाइथन प्रोग्राम में यह स्ट्रक्चर एप्लिकेशन कोड को होल्ड करता है, और टेस्ट अलग-अलग ऑर्गनाइज़ किए जाते हैं।

Unit testing vs. manual testing in Python.

Manual testing

एक प्रोग्रामर प्रोग्राम एप्लिकेशन को मैन्युअली रन करता है, और प्रोग्राम आउटपुट को चेक करता है।

First, run the program.

↓

Enter user input.

↓

Check the output result.

↓

Decide whether the result is correct.

Unit testing in Python.

यह पाइथन प्रोग्राम सोर्स कोड में टेस्ट ऑटोमैटिकली एक्सपेक्टेड बिहेवियर को चेक करते हैं।

Run a Python test

↓

Test user input

↓

Compare expected/actual results

↓

Pass or fail

पाइथन प्रोग्राम में ऑटोमेटेड टेस्ट स्पेशली रूप से तब यूज़ किए जाते हैं, जब कोई एप्लिकेशन अपने आप में लार्ज स्केल हो जाता है।

Best practices for unit testing in Python programming.

पाइथन प्रोग्रामिंग लैंग्वेज में एक बार में एक ही प्रोग्राम बिहेवियर को टेस्ट करें।

और आपके द्वारा हर पाइथन प्रोग्राम टेस्ट का एक क्लियर डिटेल पर्पस होना चाहिए।

Use descriptive test names when testing.

For example.

def test_division_by_zero_raises_error():

…

इससे एक बेहतर चॉइस है.

def testone():

…

Create a normal and edge case test in Python.

For example, testone:

पॉजिटिव नंबर

नेगेटिव नंबर

ज़ीरो

खाली इनपुट

बहुत बड़ी वैल्यू

इनवैलिड इनपुट

How to keep tests independent in Python.

यहाँ इसमें एक टेस्ट दूसरे टेस्ट के पहले रन होने पर डिपेंड नहीं होना चाहिए।

Create tests in Python and run them repeatedly.

जब भी किसी टेस्ट पाइथन प्रोग्राम सोर्स कोड में कोई बड़ा अपडेट या मोडफिकेशन हो, तो आपको उसका टेस्ट रन करना चाहिए।

Which of the two, unittest or pytest, should a Python user learn for testing their software source code?

पाइथन सॉफ्टवेयर सोर्स कोड टेस्टिंग में unittest और pytest दोनों यूज़फुल कांसेप्ट हैं।

You can choose unittest if.

  • यदि पाइथन यूजर पाइथन के बिल्ट-इन टेस्टिंग फ्रेमवर्क को यूज़ करना चाहते हैं।
  • यदि पाइथन यूजर स्टैंडर्ड पाइथन लाइब्रेरीज़ को रीड कर रहे हैं।
  • यदि पाइथन यूजर क्लास-बेस्ड टेस्टिंग स्ट्रक्चर को लाइक करते हैं।

You can choose pytest if.

  • यदि पाइथन यूजर को स्माल सोर्स टेस्ट कोड चाहिए।
  • यदि पाइथन यूजर को पावरफुल फिक्स्चर फीचर चाहिए।
  • यदि पाइथन यूजर को आसान पैरामीटर वाले टेस्ट रन चाहिए।
  • यदि पाइथन यूजर मॉडर्न पाइथन सॉफ्टवेयर सोर्स कोड प्रोजेक्ट पर काम कर रहे हैं।

बिगिनर पाइथन यूजर के लिए, pytest टेस्ट लिखना ज्यादा इजी होता है, जबकि unittest को डिटेल में सीखना अधिक फायदेमंद होता है, क्योंकि यह पाइथन की एक स्टैंडर्ड लाइब्रेरी का एलिमेंट या पार्ट है।

Conclustion of Unit Testing with unittest or pytest in Python.

पाइथन प्रोग्रामिंग लैंग्वेज में यूनिट टेस्टिंग यूजर को यह वेरिफाई करने में हेल्प करता है, कि पाइथन प्रोग्राम के अलग-अलग एलिमेंट पोरशन प्रॉपर आर्डर में काम कर रहे हैं।

There are two essential testing methods in Python programs.

unittest method

↓

built in Python

structured and class-based

and

pytest

↓

third-party Python-supported framework

A simple and flexible testing method in Python.

पाइथन प्रोग्रामिंग लैंग्वेज में एक सिंपल unittest टेस्ट कुछ इस प्रकार से डिस्प्ले होता  है.

import unittest

class TestSolution(unittest.TestCase):

def test_plus(self):

self.assertEqual(1 + 7, 8)

if __name__ == “__main__”:

unittest.main()

A simple pytest test in Python looks like this.

def test_plus():

assert 1 + 7 == 8

Python Testing in a Nutshell.

पाइथन प्रोग्रामिंग लैंग्वेज में यूनिट टेस्टिंग प्रोग्राम के स्माल पोरशन को ऑटोमैटिकली चेक या एनालाइज करता है, जिससे कि मौजूदा प्रोग्राम में किसी भी प्रकार के मिस्टेक्स को इमीडियेट फाइंड किया जा सके और प्रोग्राम सोर्स कोड को ज़्यादा ट्रस्ट अथॉरिटी के साथ मॉडिफाई किया जा सके।

Leave a Reply