pytest框架

Posted loveapple

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:pyYouYoupytest_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:pyYouYoupytest_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:pyYouYoupytest_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:pyYouYoupytest_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:pyYouYoupytest_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:pyYouYoupytest_demo, inifile:
collected 1 item

test_class.py .                                                          [100%]

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

 

四、代码示例四

E:pyYouYoupytest_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:pyYouYoupytest_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:pyYouYoupytest_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:pyYouYoupytest_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 ======================

 

以上是关于pytest框架的主要内容,如果未能解决你的问题,请参考以下文章

代码质量保证-单元测试框架pytest

pytest接口自动化测试框架 | 用python代码测试接口

python pytest测试框架介绍三

pytest框架

手把手教你pytest测试框架—— 初识框架

python - pytest