pytest框架(二)

一、示例代码一

D:YOYO\
    __init__.py
    
    test_class.py
        #  content of  test_class.py  
        class TestClass:
            def test_one(self):
                x = "this"
                assert 'h' in x
        
            def test_two(self):
                x = "hello"
                assert hasattr(x, 'check')
                
            def test_three(self):
                a = "hello"
                b = "hello world"
                assert a in b
            
    test_sample.py
        #  content of  test_sample.py
        def func(x):
            return x +1
    
        def test_answer():
            assert func(3)==5

二、代码示例二

E:\pyYouYou\pytest_demo>pytest -k "test_ and not two"
============================= test session starts =============================
platform win32 -- Python 3.6.1, pytest-3.7.2, py-1.5.4, pluggy-0.7.1
rootdir: E:\pyYouYou\pytest_demo, inifile:
collected 4 items / 1 deselected

test_class.py ..                                                         [ 66%]
test_sample.py F                                                         [100%]

================================== FAILURES ===================================
_________________________________ test_answer _________________________________

    def test_answer():
>       assert func(3) == 5
E       assert 4 == 5
E        +  where 4 = func(3)

test_sample.py:8: AssertionError
============== 1 failed, 2 passed, 1 deselected in 0.13 seconds ===============

三、代码示例三

E:\pyYouYou\pytest_demo>pytest test_sample.py::test_answer
============================= test session starts =============================
platform win32 -- Python 3.6.1, pytest-3.7.2, py-1.5.4, pluggy-0.7.1
rootdir: E:\pyYouYou\pytest_demo, inifile:
collected 1 item

test_sample.py F                                                         [100%]

================================== FAILURES ===================================
_________________________________ test_answer _________________________________

    def test_answer():
>       assert func(3) == 5
E       assert 4 == 5
E        +  where 4 = func(3)

test_sample.py:8: AssertionError
========================== 1 failed in 0.05 seconds ===========================
E:\pyYouYou\pytest_demo>pytest test_class.py::TestClass::test_three
============================= test session starts =============================
platform win32 -- Python 3.6.1, pytest-3.7.2, py-1.5.4, pluggy-0.7.1
rootdir: E:\pyYouYou\pytest_demo, inifile:
collected 1 item

test_class.py .                                                          [100%]

========================== 1 passed in 0.03 seconds ===========================

四、代码示例四

E:\pyYouYou\pytest_demo>pytest -x test_class.py
============================= test session starts =============================
platform win32 -- Python 3.6.1, pytest-3.7.2, py-1.5.4, pluggy-0.7.1
rootdir: E:\pyYouYou\pytest_demo, inifile:
collected 3 items

test_class.py .F

================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________

self = <pyYouYou.pytest_demo.test_class.TestClass object at 0x000000000389E7F0>

    def test_two(self):
        x = "hello"
>       assert hasattr(x, 'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class.py:10: AssertionError
===================== 1 failed, 1 passed in 0.06 seconds ======================

五、代码示例五

E:\pyYouYou\pytest_demo>pytest --maxfail=2
============================= test session starts =============================
platform win32 -- Python 3.6.1, pytest-3.7.2, py-1.5.4, pluggy-0.7.1
rootdir: E:\pyYouYou\pytest_demo, inifile:
collected 4 items

test_class.py .F.                                                        [ 75%]
test_sample.py F                                                         [100%]

================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________

self = <pyYouYou.pytest_demo.test_class.TestClass object at 0x00000000038AE780>

    def test_two(self):
        x = "hello"
>       assert hasattr(x, 'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class.py:10: AssertionError
_________________________________ test_answer _________________________________

    def test_answer():
>       assert func(3) == 5
E       assert 4 == 5
E        +  where 4 = func(3)

test_sample.py:8: AssertionError
===================== 2 failed, 2 passed in 0.09 seconds ======================

猜你喜欢

转载自www.cnblogs.com/loveapple/p/9507948.html
今日推荐