Python Unit Test Automation



Download 1,71 Mb.
Pdf ko'rish
bet24/73
Sana31.12.2021
Hajmi1,71 Mb.
#213720
1   ...   20   21   22   23   24   25   26   27   ...   73
Bog'liq
2 5188215245394412341

Listing 3-5.  test_module05.py
import unittest
def setUpModule():
        """called once, before anything else in this module"""
        print("In setUpModule()...")
def tearDownModule():
        """called once, after everything else in this module"""
        print("In tearDownModule()...")
class TestClass06(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
                """called once, before any test"""
                print("In setUpClass()...")
        @classmethod
        def tearDownClass(cls):
                """called once, after all tests, if setUpClass successful"""
                print("In tearDownClass()...")
        def setUp(self):
                """called multiple times, before every test method"""
                print("\nIn setUp()...")
        def tearDown(self):
                """called multiple times, after every test method"""
                print("In tearDown()...")
        def test_case01(self):
                self.assertTrue("PYTHON".isupper())
                print("In test_case01()")
        def test_case02(self):
                self.assertFalse("python".isupper())
                print("In test_case02()")
if __name__ == '__main__':
        unittest.main()
In the code in Listing 
3-5
, the setUpModule() and tearDownModule()methods are the 
module-level fixtures. setUpModule() is executed before any method in the test module. 
tearDownModule() is executed after all methods in the test module. setUpClass() and 
tearDownClass() are class-level fixtures. setUpClass() is executed before any method in 
the test class. tearDownClass() is executed after all methods in the test class.


Chapter 3 

 Unittest
39
These methods are used with the @classmethod decorator, as shown in the code in 
Listing 
3-5
. The @classmethod decorator must have a reference to a class object as the first 
parameter. setUp() and tearDown() are method-level fixtures. setUp() and tearDown() 
methods are executed before and after every test method in the test class. Run the code in 
Listing 
3-5
 as follows:
python3 test_module05.py -v
The following is the output of the code:
In setUpModule()...
In setUpClass()...
test_case01 (__main__.TestClass06) ...
In setUp()...
In test_case01()
In tearDown()...
ok
test_case02 (__main__.TestClass06) ...
In setUp()...
In test_case02()
In tearDown()...
ok
In tearDownClass()...
In tearDownModule()...
----------------------------------------------------------------------
Ran 2 tests in 0.004s
OK
The test fixtures and their implementation is the key feature in any test automation 
library. This is a major advantage over the static testing offered by doctest.
Running Without unittest.main()
Up until now, you have run the test modules with unittest.main(). Now you will see 
how to run the test module without unittest.main(). Consider the code in Listing 
3-6

for example.

Download 1,71 Mb.

Do'stlaringiz bilan baham:
1   ...   20   21   22   23   24   25   26   27   ...   73




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish