Python Unit Test Automation



Download 1,71 Mb.
Pdf ko'rish
bet71/73
Sana31.12.2021
Hajmi1,71 Mb.
#213720
1   ...   65   66   67   68   69   70   71   72   73
Bog'liq
2 5188215245394412341

Listing 6-5.  test_module01.py
from calculator import Calculator
import pytest
class TestClass01:
        def test_case01(self):
                calc = Calculator()
                result = calc.add(2, 2)
                assert 4 == result
        def test_case02(self):
                with pytest.raises(ValueError):
                        result = Calculator().add(2, 'two')
In the modified code shown in Listing 
6-5
, we’re trying to add an integer and a string
which should raise a ValueError exception.


Chapter 6 

 tips and triCks
106
If you run the modified test module, you get the following:
============================= test session starts =============================
platform linux -- Python 3.4.2, pytest-3.0.4, py-1.4.31, pluggy-0.4.0 -- /
usr/bin/python3
cachedir: .cache
rootdir: /home/pi/book/code/chapter06, inifile:
collected 2 items
test_module01.py::TestClass01::test_case01 PASSED
test_module01.py::TestClass01::test_case02 FAILED
================================= FAILURES ====================================
__________________________ TestClass01.test_case02 ____________________________
self =
    def test_case02(self):
            with pytest.raises(ValueError):
>                   result = Calculator().add(2, 'two')
test_module01.py:14:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = , x = 2, y = 'two'
    def add(self, x, y):
>           return x+y
E           TypeError: unsupported operand type(s) for +: 'int' and 'str'
calculator.py:4: TypeError
===================== 1 failed, 1 passed in 0.33 seconds ======================
As you can see in the output, the second test fails as it does not detect a ValueError 
exception. So, let’s add the provision to check if both the arguments are numeric, else 
raise a ValueError exception—see Listing 
6-6
.

Download 1,71 Mb.

Do'stlaringiz bilan baham:
1   ...   65   66   67   68   69   70   71   72   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