Python Unit Test Automation



Download 1,71 Mb.
Pdf ko'rish
bet27/73
Sana31.12.2021
Hajmi1,71 Mb.
#213720
1   ...   23   24   25   26   27   28   29   30   ...   73
Bog'liq
2 5188215245394412341

Listing 3-8.  test_module07.py
import unittest
class TestClass08(unittest.TestCase):
        def test_case01(self):
                self.assertTrue("PYTHON".isupper())
                print("\nIn test_case1()")
        def test_case02(self):
                self.assertTrue("Python".isupper())
                print("\nIn test_case2()")
        def test_case03(self):
                self.assertTrue(True)
                print("\nIn test_case3()")


Chapter 3 

 Unittest
44
You already know that -v stands for verbose mode. The following is the output in 
verbose mode:
test_case01 (test_module07.TestClass08) ...
In test_case1()
ok
test_case02 (test_module07.TestClass08) ... FAIL
test_case03 (test_module07.TestClass08) ...
In test_case3()
ok
======================================================================
FAIL: test_case02 (test_module07.TestClass08)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/pi/book/code/chapter03/test/test_module07.py", line 11, in 
test_case02
    self.assertTrue("Python".isupper())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 3 tests in 0.012s
FAILED (failures=1)
The option -q stands for quiet mode. Run the following command to demonstrate 
quiet mode:
python3 -m unittest -q test_module07
The output is as follows:
In test_case1()
In test_case3()
======================================================================
FAIL: test_case02 (test_module07.TestClass08)
----------------------------------------------------------------------
Traceback (most recent call last):
   File "/home/pi/book/code/chapter03/test/test_module07.py", line 11, in 
test_case02
    self.assertTrue("Python".isupper())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 3 tests in 0.005s
FAILED (failures=1)


Chapter 3 

 Unittest
45
The option -f stands for failsafe. It forcefully stops execution as soon as the first test 
case fails. Run the following command to initiate failsafe mode:
python3 -m unittest -q test_module07
The following is the output in failsafe mode:
In test_case1()
.F
======================================================================
FAIL: test_case02 (test_module07.TestClass08)
----------------------------------------------------------------------
Traceback (most recent call last):
   File "/home/pi/book/code/chapter03/test/test_module07.py", line 11, in 
test_case02
    self.assertTrue("Python".isupper())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 2 tests in 0.004s
FAILED (failures=1)
You can also use more than one option. For example, you can combine verbose with 
failsafe using the following command:
python3 -m unittest -fv test_module07
The output is as follows:
test_case01 (test_module07.TestClass08) ...
In test_case1()
ok
test_case02 (test_module07.TestClass08) ... FAIL
======================================================================
FAIL: test_case02 (test_module07.TestClass08)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/pi/book/code/chapter03/test/test_module07.py", line 11, in 
test_case02
    self.assertTrue("Python".isupper())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 2 tests in 0.005s
FAILED (failures=1)


Chapter 3 

 Unittest
46
As an exercise, try to use different combinations of command-line options.
Creating a Test Package
Up until now, you have created and executed test modules individually. However, you 
can use Python’s built-in packaging feature to create a package of tests. This is standard 
practice in complex projects with large codebases.
Figure 
3-1
 shows a snapshot of the current test directory where you are saving your 
test modules.
Now, let’s create a package of test modules. Create an __init__.py file in the test 
directory. Add the code in Listing 
3-9
 to the __init__.py file,

Download 1,71 Mb.

Do'stlaringiz bilan baham:
1   ...   23   24   25   26   27   28   29   30   ...   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