前言

Pytest是Python的一种单元测试框架,与unittest相比,使用起来更简洁、效率更高,也是目前大部分使用python编写测试用例的小伙伴们的第一选择了。

除了框架本身提供的功能外,Pytest还支持上百种第三方插件,良好的扩展性可以更好的满足大家在用例设计时的不同需求。本文将为大家详细介绍下面6项常用的插件。废话就不多说了我们直接开始吧。

1、失败重跑 pytest-rerunfailures

  安装:pip install pytest-rerunfailures

  使用:pytest test_class.py --reruns 5 --reruns-delay 1 -vs (失败后重新运行5次,每次间隔1秒)

     @pytest.mark.flaky(reruns = 5 ,reruns-delay = 1 ) 指定某个用例

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:chenshifeng
@file:test_calc2.py
@time:2020/09/16
"""import pytest@pytest.mark.parametrize('a,b,result', [(1, 1, 3),(2, 2, 4),(100, 100, 200),(0.1, 0.1, 0.2),(-1, -1, -2)
], ids=['int', 'int', 'bignum', 'float', 'fushu'])  # 参数化
def test_add(a, b, result):# cal = Calculator()assert result == a + b

命令行执行:

pytest test_calc2.py --reruns 5 --reruns-delay 1 -vs

结果如下:

============================================================================= test session starts =============================================================================
platform darwin -- Python 3.6.4, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
plugins: rerunfailures-9.1, dependency-0.5.1, ordering-0.6, assume-2.3.2
collected 5 items                                                                                                                                                             test_calc2.py::test_add[int0] RERUN
test_calc2.py::test_add[int0] RERUN
test_calc2.py::test_add[int0] RERUN
test_calc2.py::test_add[int0] RERUN
test_calc2.py::test_add[int0] RERUN
test_calc2.py::test_add[int0] FAILED
test_calc2.py::test_add[int1] PASSED
test_calc2.py::test_add[bignum] PASSED
test_calc2.py::test_add[float] PASSED
test_calc2.py::test_add[fushu] PASSED================================================================================== FAILURES ===================================================================================
_______________________________________________________________________________ test_add[int0] ________________________________________________________________________________a = 1, b = 1, result = 3@pytest.mark.parametrize('a,b,result', [(1, 1, 3),(2, 2, 4),(100, 100, 200),(0.1, 0.1, 0.2),(-1, -1, -2)], ids=['int', 'int', 'bignum', 'float', 'fushu'])  # 参数化def test_add(a, b, result):cal = Calculator()
>       assert result == cal.add(a, b)
E       assert 3 == 2
E         +3
E         -2test_calc2.py:26: AssertionError
=========================================================================== short test summary info ===========================================================================
FAILED test_calc2.py::test_add[int0] - assert 3 == 2
==================================================================== 1 failed, 4 passed, 5 rerun in 5.11s =====================================================================

通过装饰器设置重跑次数与延时时间

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:chenshifeng
@file:test_calc2.py
@time:2020/09/16
"""import pytest@pytest.mark.parametrize('a,b,result', [(1, 1, 3),(2, 2, 4),(100, 100, 200),(0.1, 0.1, 0.2),(-1, -1, -2)
], ids=['int', 'int', 'bignum', 'float', 'fushu'])  # 参数化
# 通过装饰器设置重跑次数
@pytest.mark.flaky(reruns=6, reruns_delay=2)
def test_add(a, b, result):# cal = Calculator()assert result == a + b

结果:

Testing started at 10:10 下午 ...
/usr/local/bin/python3.6 "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --target test_calc2.py::test_add
Launching pytest with arguments test_calc2.py::test_add in /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest/testcode============================= test session starts ==============================
platform darwin -- Python 3.6.4, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
plugins: rerunfailures-9.1, dependency-0.5.1, ordering-0.6, assume-2.3.2
collecting ... collected 5 itemstest_calc2.py::test_add[int0] RERUN                                      [ 20%]
test_calc2.py::test_add[int0] RERUN                                      [ 20%]
test_calc2.py::test_add[int0] RERUN                                      [ 20%]
test_calc2.py::test_add[int0] RERUN                                      [ 20%]
test_calc2.py::test_add[int0] RERUN                                      [ 20%]
test_calc2.py::test_add[int0] RERUN                                      [ 20%]
test_calc2.py::test_add[int0] FAILED                                     [ 20%]
testcode/test_calc2.py:11 (test_add[int0])
3 != 2Expected :2
Actual   :3
<Click to see difference>a = 1, b = 1, result = 3@pytest.mark.parametrize('a,b,result', [(1, 1, 3),(2, 2, 4),(100, 100, 200),(0.1, 0.1, 0.2),(-1, -1, -2)], ids=['int', 'int', 'bignum', 'float', 'fushu'])  # 参数化# 通过装饰器设置重跑次数@pytest.mark.flaky(reruns=6, reruns_delay=2)def test_add(a, b, result):# cal = Calculator()
>       assert result == a + b
E       assert 3 == 2test_calc2.py:23: AssertionError
PASSED                                     [ 40%]PASSED                                   [ 60%]PASSED                                    [ 80%]PASSED                                    [100%]
Assertion failedAssertion failedAssertion failedAssertion failedtest_calc2.py::test_add[int1]
test_calc2.py::test_add[bignum]
test_calc2.py::test_add[float]
test_calc2.py::test_add[fushu] =================================== FAILURES ===================================
________________________________ test_add[int0] ________________________________a = 1, b = 1, result = 3@pytest.mark.parametrize('a,b,result', [(1, 1, 3),(2, 2, 4),(100, 100, 200),(0.1, 0.1, 0.2),(-1, -1, -2)], ids=['int', 'int', 'bignum', 'float', 'fushu'])  # 参数化# 通过装饰器设置重跑次数@pytest.mark.flaky(reruns=6, reruns_delay=2)def test_add(a, b, result):# cal = Calculator()
>       assert result == a + b
E       assert 3 == 2test_calc2.py:23: AssertionError
=========================== short test summary info ============================
FAILED test_calc2.py::test_add[int0] - assert 3 == 2
==================== 1 failed, 4 passed, 6 rerun in 12.13s =====================Process finished with exit code 1Assertion failedAssertion failedAssertion failedAssertion failed

2、多重校验 pytest-assume

  正常情况下一条用例如果有多条断言,一条断言失败了,其他断言就不会执行了,而使用pytest-assume可以继续执行下面的断言

    安装 : pip install pytest-assume

    执行 : pytest.assume(1==3)

for example:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:chenshifeng
@file:test_calc2.py
@time:2020/09/16
"""import pytestdef test_assume():print('登录操作')pytest.assume(1 == 2)print('搜索操作')pytest.assume(2 == 2)print('加购操作')pytest.assume(3 == 2)

运行结果:

Testing started at 10:23 下午 ...
/usr/local/bin/python3.6 "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --target test_calc2.py::test_assume
Launching pytest with arguments test_calc2.py::test_assume in /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest/testcode============================= test session starts ==============================
platform darwin -- Python 3.6.4, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
plugins: rerunfailures-9.1, dependency-0.5.1, ordering-0.6, assume-2.3.2
collecting ... collected 1 itemtest_calc2.py::test_assume FAILED                                        [100%]登录操作
搜索操作
加购操作testcode/test_calc2.py:11 (test_assume)
tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = Nonedef reraise(tp, value, tb=None):try:if value is None:value = tp()if value.__traceback__ is not tb:
>               raise value.with_traceback(tb)
E               pytest_assume.plugin.FailedAssumption:
E               2 Failed Assumptions:
E
E               test_calc2.py:14: AssumptionFailure
E               >>    pytest.assume(1 == 2)
E               AssertionError: assert False
E
E               test_calc2.py:18: AssumptionFailure
E               >>    pytest.assume(3 == 2)
E               AssertionError: assert False/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/six.py:702: FailedAssumptionAssertion failedAssertion failed=================================== FAILURES ===================================
_________________________________ test_assume __________________________________tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = Nonedef reraise(tp, value, tb=None):try:if value is None:value = tp()if value.__traceback__ is not tb:
>               raise value.with_traceback(tb)
E               pytest_assume.plugin.FailedAssumption:
E               2 Failed Assumptions:
E
E               test_calc2.py:14: AssumptionFailure
E               >>    pytest.assume(1 == 2)
E               AssertionError: assert False
E
E               test_calc2.py:18: AssumptionFailure
E               >>    pytest.assume(3 == 2)
E               AssertionError: assert False/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/six.py:702: FailedAssumption
----------------------------- Captured stdout call -----------------------------
登录操作
搜索操作
加购操作
=========================== short test summary info ============================
FAILED test_calc2.py::test_assume - pytest_assume.plugin.FailedAssumption:
============================== 1 failed in 0.09s ===============================Process finished with exit code 1Assertion failedAssertion failedAssertion failedAssertion failed

3、设定执行顺序 pytest-ordering

  正常情况下,用例默认执行顺序是自上而下的,对于一些有上下文依赖关系的用例,可是通过 pytest-ordering 来设置执行顺序,当然,通过setup、teardown和fixture来解决也是可以的

  安装插件 : pip install pytest-ordering

  使用方法 : @pytest.mark.run(order=2)

  需要注意的是,当有多个装饰器的时候,可能会发生冲突(比如参数化)

For example, this:

import pytest@pytest.mark.run(order=2)
def test_foo():assert True@pytest.mark.run(order=1)
def test_bar():assert True

Yields this output:

============================= test session starts ==============================
platform darwin -- Python 3.6.4, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
plugins: rerunfailures-9.1, dependency-0.5.1, ordering-0.6, assume-2.3.2
collecting ... collected 2 itemstest_ordering.py::test_bar
test_ordering.py::test_foo ============================== 2 passed in 0.02s ===============================

4、用例依赖(pytest-dependency)

使用该插件可以标记一个testcase作为其他testcase的依赖,当依赖项执行失败时,那些依赖它的test将会被跳过。

安装 : pip install pytest-dependency

使用方法: 用 @pytest.mark.dependency()对所依赖的方法进行标记,使用@pytest.mark.dependency(depends=["test_name"])引用依赖,test_name可以是多个。

上用例:

import pytest@pytest.mark.dependency()
def test_01():assert False@pytest.mark.dependency(depends=["test_01"])
def test_02():print("执行测试2")

output:

=========================== short test summary info ============================
FAILED test_ordering.py::test_01 - assert False
========================= 1 failed, 1 skipped in 0.06s =========================Process finished with exit code 1

5.分布式测试(pytest-xdist)

  • 平常我们功能测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟,如果单个测试人员执行需要1000分钟才能跑完
  • 当项目非常紧急时,会需要协调多个测试资源来把任务分成两部分,于是执行时间缩短一半,如果有10个小伙伴,那么执行时间就会变成十分之一,大大节省了测试时间
  • 为了节省项目测试时间,10个测试同时并行测试,这就是一种分布式场景

 分布式执行用例的原则:

  • 用例之间是独立的,没有依赖关系,完全可以独立运行用例执行没有顺序要求,随机顺序都能正常执行每个用例都能重复运行,运行结果不会影响其他用例

  插件安装:
      pip3 install pytest-xdist -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

  使用方法:

      pytest -n 2 (2代表2个CPU)

      pytest -n auto

  • nauto:可以自动检测到系统的CPU核数;从测试结果来看,检测到的是逻辑处理器的数量,即假12核使用auto等于利用了所有CPU来跑用例,此时CPU占用率会特别高

6.生成报告(pytest-html)

pytest-html是一个插件,pytest用于生成测试结果的HTML报告。兼容Python 2.7,3.6

安装插件: pip install pytest-html

使用方法: pytest --html=report.html

7、总结

本文为大家介绍了一些常用的pytest框架的插件,可以帮助我们解决一些实际使用过程中遇到的问题。目前,pytest支持的插件有很多个,除了本文介绍的6个常用插件外,还有很多支持其它需求的插件,大家可以根据自己的需要尝试查找使用相关的插件,以便能够更好的设计出符合业务场景的测试用例。

喜欢本文可以点赞加关注哟。七叔每天都会分享不同的东西哟,关注七叔和七叔一起学习。

Python自动化——pytest常用插件详解相关推荐

  1. python自动化xmind to testcase详解

    python自动化xmind to testcase详解 前文: 看过一些文章写得xmind2testlink,比较简单化对于初学者来说还是比较花时间去理解其中的一些步骤,所以整理下了前辈们的操作再加 ...

  2. Rollup常用插件详解

    文章目录 系列文章 @rollup/plugin-node-resolve Options extensions @rollup/plugin-commonjs @rollup/plugin-babe ...

  3. maven常用插件详解

    1. 简介 我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven- compiler-plugin完成的.进一步说, ...

  4. python流程控制-Python流程控制常用工具详解

    在我们的工作中,我们使用ddagent ver.5作为收集工具,收集和报告托管服务器的性能指标,并对ddagent进行一定程度的定制.经过多次功能迭代,发现一批在线运行时间长的托管服务器占用了太多内存 ...

  5. python的pytest模块:pytest命令行详解

    一.官方文档 How to invoke pytest - pytest documentationhttps://docs.pytest.org/en/latest/how-to/usage.htm ...

  6. python对输入的字符串进行解析_python数据类型_字符串常用操作(详解)

    这次主要介绍字符串常用操作方法及例子 1.python字符串 在python中声明一个字符串,通常有三种方法:在它的两边加上单引号.双引号或者三引号,如下: name = 'hello' name1 ...

  7. python的shutil模块是内置的_Python之shutil模块11个常用函数详解,python内置函数是什么...

    Python之shutil模块11个常用函数详解,python内置函数是什么 shutil 是 Python 中的高级文件操作模块,与os模块形成互补的关系,os主要提供了文件或文件夹的新建.删除.查 ...

  8. 最全整理!Python 操作 Excel 库 xlrd与xlwt 常用操作详解!

    来源/早起Python 在之前的Python办公自动化系列文章中,我们已经相信介绍了openyxl.xlsxwriter等Python操作Excel库. 相信大家对于几个库的差异与使用场景有了一定的认 ...

  9. python加密字符串小写字母循环后错两位_python数据类型_字符串常用操作(详解)

    这次主要介绍字符串常用操作方法及例子 1.python字符串 在python中声明一个字符串,通常有三种方法:在它的两边加上单引号.双引号或者三引号,如下: name = 'hello' name1 ...

最新文章

  1. 独家 | 微软与哈佛大学定量社会科学研究所合作开发开放数据差异隐私平台,开启研究新征程(附链接)...
  2. WINCE5.0下SQL server compact版本更新
  3. QT如何让窗口放置在屏幕正中间
  4. Python必备收藏!Pycharm 常用快捷键方式!让鼠标离手操作
  5. unity json mysql_unity——json总结
  6. OpenGL:着色器shader
  7. 大数据workshop:《在线用户行为分析:基于流式计算的数据处理及应用》之《实时数据分析:海量日志数据多维透视》篇...
  8. 【clickhouse】clickhouse 表引擎 之 VersionedCollapsingMergeTree
  9. Intelij IDEA解决Dependency无法更新问题
  10. scala访问MySQL数据库
  11. win7系统蓝屏修复工具如何使用
  12. python爬虫淘宝评论_Python爬虫,抓取淘宝商品评论内容
  13. 【源码】高精度31波段音频均衡器
  14. 2009年中国直销业9大主旋律
  15. apiclod 上传图片_apicloud图片上传
  16. 【无标题】二进制STL文件解析 计算面积和体积与根据高度计算部分体积
  17. 软考报名时间 流程 条件
  18. 星际争霸2-数据编辑器-菜鸟入门
  19. 【数据库系统概念第七版(Database System Concepts 7th)配套SQL文件如何获取】
  20. VS2017编译在XP环境下运行的程序

热门文章

  1. 蓝牙 - 如何实现安全性
  2. ip4标准(ip4网段)
  3. 《智能时代下的创新创业实践》 习题 + 期末考试+答案 + 2021年【完整/完结】
  4. whistle苹果手机安装证书步骤:
  5. 苹果笔记本android studio安装教程,MAC下如何安装AndroidStudio
  6. java排队买票_春运还需多少通宵排队买票猝死的悲剧
  7. C# Fleck实现websocket
  8. 手机那点事儿——华为与苹果的差距
  9. 【CMake】CMakeLists.txt的超傻瓜手把手教程(附实例源码)
  10. 基于USB的运动控制平台及采集卡的设计