• pytest一些常见的插件


    Pytest拥有丰富的插件架构,超过800个以上的外部插件和活跃的社区,在PyPI项目中以“ pytest- *”为标识。

    本篇将列举github标星超过两百的一些插件进行实战演示。

    插件库地址:http://plugincompat.herokuapp.com/


    1、pytest-html:用于生成HTML报告

    一次完整的测试,测试报告是必不可少的,但是pytest自身的测试结果过于简单,而pytest-html正好可以给你提供一份清晰报告。

    安装:
    pip install -U pytest-html
    用例:

    1. # test_sample.py
    2. import pytest
    3. # import time
    4. # 被测功能
    5. def add(x, y):
    6. # time.sleep(1)
    7. return x + y
    8. # 测试类
    9. class TestLearning:
    10. data = [
    11. [3, 4, 7],
    12. [-3, 4, 1],
    13. [3, -4, -1],
    14. [-3, -4, 7],
    15. ]
    16. @pytest.mark.parametrize("data", data)
    17. def test_add(self, data):
    18. assert add(data[0], data[1]) == data[2]

    运行:

    1. E:\workspace-py\Pytest>pytest test_sample.py --html=report/index.html
    2. ========================================================================== test session starts ==========================================================================
    3. platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
    4. rootdir: E:\workspace-py\Pytest
    5. plugins: allure-pytest-2.8.18, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
    6. collected 4 items
    7. test_sample.py ...F [100%]
    8. =============================================================================== FAILURES ================================================================================
    9. _____________________________________________________________________ TestLearning.test_add[data3] ______________________________________________________________________
    10. self = <test_sample.TestLearning object at 0x00000000036B6AC8>, data = [-3, -4, 7]
    11. @pytest.mark.parametrize("data", data)
    12. def test_add(self, data):
    13. > assert add(data[0], data[1]) == data[2]
    14. E assert -7 == 7
    15. E + where -7 = add(-3, -4)
    16. test_sample.py:20: AssertionError
    17. ------------------------------------------------- generated html file: file://E:\workspace-py\Pytest\report\index.html --------------------------------------------------
    18. ======================================================================== short test summary info ========================================================================
    19. FAILED test_sample.py::TestLearning::test_add[data3] - assert -7 == 7
    20. ====================================================================== 1 failed, 3 passed in 0.14s ======================================================================

    运行完,会生产一个html文件 和 css样式文件夹assets,用浏览器打开html即可查看清晰的测试结果。

     

    后面我将会更新更加清晰美观的测试报告插件: allure-python


    2、pytest-cov:用于生成覆盖率报告

    在做单元测试时,代码覆盖率常常被拿来作为衡量测试好坏的指标,甚至,用代码覆盖率来考核测试任务完成情况。

    安装:
    pip install -U pytest-cov
     运行:

    1. E:\workspace-py\Pytest>pytest --cov=.
    2. ========================================================================== test session starts ==========================================================================
    3. platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
    4. rootdir: E:\workspace-py\Pytest
    5. plugins: allure-pytest-2.8.18, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
    6. collected 4 items
    7. test_sample.py .... [100%]
    8. ----------- coverage: platform win32, python 3.7.3-final-0 -----------
    9. Name Stmts Miss Cover
    10. ------------------------------------
    11. conftest.py 5 3 40%
    12. test_sample.py 7 0 100%
    13. ------------------------------------
    14. TOTAL 12 3 75%
    15. =========================================================================== 4 passed in 0.06s ===========================================================================


    3、pytest-xdist:实现多线程、多平台执行

    通过将测试发送到多个CPU来加速运行,可以使用-n NUMCPUS指定具体CPU数量,或者使用-n auto自动识别CPU数量并全部使用。

    安装:
    pip install -U pytest-xdist
    用例:

    1. # test_sample.py
    2. import pytest
    3. import time
    4. # 被测功能
    5. def add(x, y):
    6. time.sleep(3)
    7. return x + y
    8. # 测试类
    9. class TestAdd:
    10. def test_first(self):
    11. assert add(3, 4) == 7
    12. def test_second(self):
    13. assert add(-3, 4) == 1
    14. def test_three(self):
    15. assert add(3, -4) == -1
    16. def test_four(self):
    17. assert add(-3, -4) == 7

     运行:

    1. E:\workspace-py\Pytest>pytest test_sample.py
    2. ========================================================================== test session starts ==========================================================================
    3. platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
    4. rootdir: E:\workspace-py\Pytest
    5. plugins: allure-pytest-2.8.18, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
    6. collected 4 items
    7. test_sample.py .... [100%]
    8. ========================================================================== 4 passed in 12.05s ===========================================================================
    9. E:\workspace-py\Pytest>pytest test_sample.py -n auto
    10. ========================================================================== test session starts ==========================================================================
    11. platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
    12. rootdir: E:\workspace-py\Pytest
    13. plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, forked-1.3.0, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
    14. gw0 [4] / gw1 [4] / gw2 [4] / gw3 [4]
    15. .... [100%]
    16. =========================================================================== 4 passed in 5.35s ===========================================================================
    17. E:\workspace-py\Pytest>pytest test_sample.py -n 2
    18. ========================================================================== test session starts ==========================================================================
    19. platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
    20. rootdir: E:\workspace-py\Pytest
    21. plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, forked-1.3.0, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
    22. gw0 [4] / gw1 [4]
    23. .... [100%]
    24. =========================================================================== 4 passed in 7.65s ===========================================================================

    上述分别进行了未开多并发、开启4个cpu、开启2个cpu,从运行耗时结果来看,很明显多并发可以大大缩减你的测试用例运行耗时。


    4、pytest-rerunfailures:实现重新运行失败用例

     我们在测试时可能会出现一些间接性故障,比如接口测试遇到网络波动,web测试遇到个别插件刷新不及时等,这时重新运行则可以帮忙我们消除这些故障。

     安装:
    pip install -U pytest-rerunfailures
    运行:

    1. E:\workspace-py\Pytest>pytest test_sample.py --reruns 3
    2. ========================================================================== test session starts ==========================================================================
    3. platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
    4. rootdir: E:\workspace-py\Pytest
    5. plugins: allure-pytest-2.8.18, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
    6. collected 4 items
    7. test_sample.py ...R [100%]R
    8. [100%]R [100%]F [100%]
    9. =============================================================================== FAILURES ================================================================================
    10. ___________________________________________________________________________ TestAdd.test_four ___________________________________________________________________________
    11. self = <test_sample.TestAdd object at 0x00000000045FBF98>
    12. def test_four(self):
    13. > assert add(-3, -4) == 7
    14. E assert -7 == 7
    15. E + where -7 = add(-3, -4)
    16. test_sample.py:22: AssertionError
    17. ======================================================================== short test summary info ========================================================================
    18. FAILED test_sample.py::TestAdd::test_four - assert -7 == 7
    19. ================================================================= 1 failed, 3 passed, 3 rerun in 0.20s ==================================================================

    如果你想设定重试间隔,可以使用 --rerun-delay 参数指定延迟时长(单位秒); 

    如果你想重新运行指定错误,可以使用 --only-rerun 参数指定正则表达式匹配,并且可以使用多次来匹配多个。

    pytest --reruns 5 --reruns-delay 1 --only-rerun AssertionError --only-rerun ValueError

    如果你只想标记单个测试失败时自动重新运行,可以添加 pytest.mark.flaky() 并指定重试次数以及延迟间隔。

    @pytest.mark.flaky(reruns=5, reruns_delay=2)
    def test_example():
        import random
        assert random.choice([True, False])


    5、pytest-randomly:实现随机排序测试

    测试中的随机性非常越大越容易发现测试本身中隐藏的缺陷,并为你的系统提供更多的覆盖范围。

    安装:
    pip install -U pytest-randomly
    运行:

    1. E:\workspace-py\Pytest>pytest test_sample.py
    2. ========================================================================== test session starts ==========================================================================
    3. platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
    4. Using --randomly-seed=3687888105
    5. rootdir: E:\workspace-py\Pytest
    6. plugins: allure-pytest-2.8.18, cov-2.10.1, html-3.0.0, randomly-3.5.0, rerunfailures-9.1.1, xdist-2.1.0
    7. collected 4 items
    8. test_sample.py F... [100%]
    9. =============================================================================== FAILURES ================================================================================
    10. ___________________________________________________________________________ TestAdd.test_four ___________________________________________________________________________
    11. self = <test_sample.TestAdd object at 0x000000000567AD68>
    12. def test_four(self):
    13. > assert add(-3, -4) == 7
    14. E assert -7 == 7
    15. E + where -7 = add(-3, -4)
    16. test_sample.py:22: AssertionError
    17. ======================================================================== short test summary info ========================================================================
    18. FAILED test_sample.py::TestAdd::test_four - assert -7 == 7
    19. ====================================================================== 1 failed, 3 passed in 0.13s ======================================================================
    20. E:\workspace-py\Pytest>pytest test_sample.py
    21. ========================================================================== test session starts ==========================================================================
    22. platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
    23. Using --randomly-seed=3064422675
    24. rootdir: E:\workspace-py\Pytest
    25. plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, forked-1.3.0, html-3.0.0, randomly-3.5.0, rerunfailures-9.1.1, xdist-2.1.0
    26. collected 4 items
    27. test_sample.py ...F [100%]
    28. =============================================================================== FAILURES ================================================================================
    29. ___________________________________________________________________________ TestAdd.test_four ___________________________________________________________________________
    30. self = <test_sample.TestAdd object at 0x00000000145EA940>
    31. def test_four(self):
    32. > assert add(-3, -4) == 7
    33. E assert -7 == 7
    34. E + where -7 = add(-3, -4)
    35. test_sample.py:22: AssertionError
    36. ======================================================================== short test summary info ========================================================================
    37. FAILED test_sample.py::TestAdd::test_four - assert -7 == 7
    38. ====================================================================== 1 failed, 3 passed in 0.12s ======================================================================

    这功能默认情况下处于启用状态,但可以通过标志禁用(假如你并不需要这个模块,建议就不要安装)。

    pytest -p no:randomly

    如果你想指定随机顺序,可以通过 --randomly-send 参数来指定,也可以使用 last 值来指定沿用上次的运行顺序。

    pytest --randomly-seed=4321
    
    pytest --randomly-seed=last
    

    6、其他活跃的插件

    还有一些其他功能性比较活跃的、一些专门为个别框架所定制的、以及为了兼容其他测试框架,这里暂不做演示,我就简单的做个列举:

    pytest-django:用于测试Django应用程序(Python Web框架)。

    pytest-flask:用于测试Flask应用程序(Python Web框架)。

    pytest-splinter:兼容Splinter Web自动化测试工具。

    pytest-selenium:兼容Selenium Web自动化测试工具。

    pytest-testinfra:测试由Salt,Ansible,Puppet, Chef等管理工具配置的服务器的实际状态。

    pytest-mock:提供一个mock固件,创建虚拟的对象来实现测试中个别依赖点。

    pytest-factoryboy:结合factoryboy工具用于生成各式各样的数据。

    pytest-qt:提供为PyQt5和PySide2应用程序编写测试。

    pytest-asyncio:用于使用pytest测试异步代码。

    pytest-bdd:实现了Gherkin语言的子集,以实现自动化项目需求测试并促进行为驱动的开发。

    pytest-watch:为pytest提供一套快捷CLI工具。

    pytest-testmon:可以自动选择并重新执行仅受最近更改影响的测试。

    pytest-assume:用于每个测试允许多次失败。

    pytest-ordering:用于测试用例的排序功能。

    pytest-sugar:可立即显示失败和错误并显示进度条。

    pytest-dev/pytest-repeat:可以重复(可指定次数)执行单个或多个测试。

  • 相关阅读:
    【在Vue脚手架项目中使用qs框架】
    构建系列之前端脚手架vite
    5.最长回文子串(马拉车怨种版)
    system V共享内存【Linux】
    SC0099 AT32F4xx 模拟EEPROM并通过I2C通信
    spring cloud alibaba - seata分布式事务记录
    Spark大数据应用实战
    第十一章 JavaScript操作DOM对象
    Word通过Adobe打印PDF时总是报错,打开记事本
    【CSP考点回顾】前缀和数组
  • 原文地址:https://blog.csdn.net/jj2772367224/article/details/132993885