• pytest测试框架


    一、测试用例的识别和运行

    • test_*.py
    • *_test.py

    二、用例的识别

    • Test*类包含所有的test_*的方法(测试类不能带有__int__方法)
    • 不在class中所有的test_*方法

    三、pytest也可以执行unittest框架写的用例和方法

    四、pytest安装与依赖

    • pytest安装
    1.   pip install -U pytset  U表示升级
    • pytest常用插件
    1.   pytest-selenium 集成selenium
    2. pytest-allure-adaptor 生成漂亮的allure报告
    3. pip install pytest-sugar 优化运行效果
    4. pip install pytest-rerunfailures 重新运行错误用例
    5. pip install pytest-xdist  多cpu分布式执行
    6. pip install pytest-assume 断言
    7. pip install pytest-html 测试报告
    • pip list 查看
    • pytest-h 帮助

    五、执行测试用例

      

     1 import pytest
     2 
     3 def fun(x):
     4     return x+1
     5 
     6 def test_one():
     7     assert fun(3)==5
     8 
     9 class TestCase():
    10     def test_tow(self):
    11         assert fun(4)==5
    12 
    13     def test_three(self):
    14         assert  fun(6)==7
    • 终端执行
    1.   pytest -v -s test_cass.py       -v表示打印详细的信息   -s表示输出print打印信息
    2.  pytest -v -s test_cass.py::test_one  ::表示只执行某个测试函数
    3. pytest -v -s test_cass.py::TestCase::test_three   :: ::  表示执行某个类下面的某个测试函数

    4. pytest -v -s -x  test_cass.py   -x表示测试用例执行出现错误那么将不执行下面的函数
    5. pytest -v -s --maxfail=2  test_cass.py     --maxfail=2表示当faillde有两个时候停止程序,2可以自定义
    6. pytest -v -s -k TestCase test_cass.py   -k 表示只执行某个类的里面的测试用例 
    • pytest执行--失败重新运行
    1.   pytest -v -s --reruns=2 test_cass.py  --reruns=2表示执行用例出错就再执行两次,2可以自定义
    2. pytest -v -s --reruns=2 test_cass.py --reruns-delay=2  --reruns-delay=2表示用例执行失败等两秒再运行,2可以自定义
    • pytset执行--多重断言失败也能运行assume

      

     1 import pytest
     2 
     3 def fun(x):
     4     return x+1
     5 
     6 def test_one():
     7     print('这是第一条测试用例!')
     8     assert fun(3)==5
     9 
    10 class TestCase():
    11     def test_tow(self):
    12         print('这是第二条测试用例')
    13         assert fun(4)==5
    14 
    15     def test_three(self):
    16         assert  fun(3)==7
    17 
    18     def test_four(self):
    19         assert fun(8)==9
    20 
    21     def test_fall(self):
    22         pytest.assume(1==2)
    23         pytest.assume(fun(1) == 2)
    24         pytest.assume(fun(2)==3)
    25         pytest.assume(2==3)
    26         print('这是多断言测试用例')
    

    六、pytest的执行

    import pytest
    
    def fun(x):
        return x+1
    
    def test_one():
        print('这是第一条测试用例!')
        assert fun(3)==5
    
    class TestCase():
        def test_tow(self):
            print('这是第二条测试用例')
            assert fun(4)==5
    
        def test_three(self):
            assert  fun(3)==7
    
        def test_four(self):
            assert fun(8)==9
    
        def test_fall(self):
            pytest.assume(1==1)
            pytest.assume(fun(1) == 2)
            pytest.assume(fun(2)==3)
            pytest.assume(1==2)
            print('这是多断言测试用例')
    
    if __name__ == '__main__':
        pytest.main(['-v','-s','test_cass.py::test_one'])

    注意:pytest.main([])方法的[]里面的属性和终端里面的相同

    七、pytest框架结构

     1 import pytest
     2 
     3 #模块级别的
     4 def setup_module():
     5     print('模块级别的setup')
     6 
     7 #模块级别的
     8 def teardown_module():
     9     print('模块级别的teardown')
    10 
    11 #函数级别的
    12 def setup_function():
    13     print('函数级别的setup')
    14 
    15 #函数级别的
    16 def teardown_function():
    17     print('方法级别的teardown')
    18 
    19 class Test_case():
    20     #类级别的
    21     def setup_class(self):
    22         print('这是类级别的setup')
    23 
    24     #类级别的
    25     def teardown_class(self):
    26         print('这是类级别的teardown')
    27 
    28     #方法级别的
    29     def setup_method(self):
    30         print('这是方法级别的setup')
    31 
    32     #方法级别的
    33     def teardown_method(self):
    34         print('这是方法级别的teardown')
    35 
    36     def setup(self):
    37         print('普通级别的setup')
    38 
    39     def teardown(self):
    40         print('普通级别的teardown')
    41 
    42     def test_one(self):
    43         print('这是第一条用例!')
    44 
    45     def test_tow(self):
    46         print('这是第二条用例')
    47 
    48 if __name__ == '__main__':
    49     pytest.main()

    test_case01.py 模块级别的setup
    函数级别的setup
    .这是一个方法
    函数级别的teardown
    这是类级别的setup
    这是方法级别的setup
    普通级别的setup
    .这是第一条用例!
    普通级别的teardown
    这是方法级别的teardown
    这是方法级别的setup
    普通级别的setup
    .这是第二条用例
    普通级别的teardown
    这是方法级别的teardown
    这是类级别的teardown
    模块级别的teardown

    模块级别的优先级最高,模块开始和结束(不在类中)

    函数级别,只对函数用例生效(不在类中)

    类级别,在类中前后执行(在类中)

    方法级别,开始于方法始末(在类中)

    普通级别,运行在调用方法的前后(在类中)

    八、pytest fixture的使用

    import pytest
    
    @pytest.fixture(scope='session') 

    def login(): print('这是一个登陆方法')

    def test_case01(): print('测试用例1')

    def test_cese02(login): print('测试用例2')

    if __name__ == '__main__': pytest.main()
    使用@pytest.fixture()的时候可以将函数名称作为参数传入,该函数的返回值作为测试函数的传入参数
    @pytest.fixtuer()想在整改包下可以用要在他的同一级别目录下创建conftest.py文件
    可以传入有多个fixture标签的函数
    @pytest.fixture() 参数:
    socpe session(作用域最大)
    import pytest
    #session是最大级别的可以在整个session下使用
    @pytest.fixture(scope='session')
    def login():
        print('这是一个登陆方法')
    
    
    
    utouse(参数的范围是方法级别的) 默认是False 传入True
     1 import pytest
     2 
     3 @pytest.fixture(autouse=True)
     4 def test_one():
     5     print('这个一个公用方法')
     6 
     7 def test_case01():
     8     print('测试用例1')
     9 
    10 def test_cese02():
    11     print('测试用例2 ')
    12 
    13 if __name__ == '__main__':
    14     pytest.main()


    test_case02.py 这个一个公用方法
    .测试用例1
    这个一个公用方法
    .测试用例2
    [100%]

    ============================== 2 passed in 0.04s ==============================

    
    
    
    九、打印html测试报告
    • pytest test_case.py --html=./report.html

  • 相关阅读:
    springboot 踩坑之表单验证
    爬虫学习研究
    selenium+chrome知识
    每天学一点linux命令
    Javascript禁止子元素继承父元素的事件
    sass编译css(转自阮一峰)
    php配置虚拟主机的配置步骤(hosts、httpd.conf、vhosts.conf)1.配置本地的dns文件2.配置apache的主配置文件3.配置Apache的虚拟主机
    修改原代码定制bootstrap
    网页在线进行标准验证
    浏览器兼容性判定写法格式(ie)
  • 原文地址:https://www.cnblogs.com/zihkj/p/12854240.html
Copyright © 2020-2023  润新知