Python Unit Test Automation



Download 1,71 Mb.
Pdf ko'rish
bet45/73
Sana31.12.2021
Hajmi1,71 Mb.
#213720
1   ...   41   42   43   44   45   46   47   48   ...   73
Bog'liq
2 5188215245394412341

Listing 4-4.  test_module03.py
from mypackage.mymathlib import *
math_obj = 0
def setUpModule():
    """called once, before anything else in this module"""
    print("In setUpModule()...")
    global math_obj
    math_obj = mymathlib()
def tearDownModule():
    """called once, after everything else in this module"""
    print("In tearDownModule()...")
    global math_obj
    del math_obj
class TestClass02:
    @classmethod
    def setUpClass(cls):


Chapter 4 

 nose and nose2
71
        """called once, before any test in the class"""
        print("In setUpClass()...")
    def setUp(self):
        """called before every test method"""
        print("\nIn setUp()...")
    def test_case01(self):
        print("In test_case01()")
        assert math_obj.add(2, 5) == 7
    def test_case02(self):
        print("In test_case02()")
    def tearDown(self):
        """called after every test method"""
        print("In tearDown()...")
    @classmethod
    def tearDownClass(cls):
        """called once, after all tests, if setUpClass() successful"""
        print ("\nIn tearDownClass()...")
If you run the code in Listing 
4-4
 with the following command:
nosetests test_module03.py -v
The output will be as follows:
test.test_module03.TestClass02.test_case01 ... ok
test.test_module03.TestClass02.test_case02 ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.010s
OK
In order to get more details of test execution, you need to add the -s option to the 
command line, which allows any stdout output to be printed in the command line 
immediately.
Run the following command:
nosetests test_module03.py -vs


Chapter 4 

 nose and nose2
72
The output is as follows:
In setUpModule()...
Creating object : mymathlib
In setUpClass()...
test.test_module03.TestClass02.test_case01 ...
In setUp()...
In test_case01()
In tearDown()...
ok
test.test_module03.TestClass02.test_case02 ...
In setUp()...
In test_case02()
In tearDown()...
ok
In tearDownClass()...
In tearDownModule()...
Destroying object : mymathlib
----------------------------------------------------------------------
Ran 2 tests in 0.011s
OK
From now onward, we will add the -s option to the nosetests command while 
executing the tests.
Fixtures for Functions
Before you get started with the fixtures for functions, you must understand the difference 
between a function and a method in Python. A function is a named piece of code that 
performs an operation and a method is a function with an extra parameter that’s the 
object on which it runs. A function is not associated with a class. A method is always 
associated with a class.
Check the code in Listing 
4-5
 as an example.

Download 1,71 Mb.

Do'stlaringiz bilan baham:
1   ...   41   42   43   44   45   46   47   48   ...   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