pytest合集(5)— Function函数
1、pytest.approx
断言两个数字(或两组数字)在某个容差范围内彼此相等。
from pytest import approx def test_one():assert 0.1 + 0.2 == 0.3# 断言两个数字(或两组数字)在某个容差范围内彼此相等。 def test_two():assert 0.1 + 0.2 == approx(0.3)def test_three():assert (0.1 + 0.2, 0.2 + 0.4) == approx((0.3, 0.6))def test_four():assert {'a': 0.1 + 0.2, 'b': 0.2 + 0.4} == approx({'a': 0.3, 'b': 0.6})
2、pytest.fail,pytest.skip,pytest.xfail
将用例置为失败,跳过用例,用例预期失败。
import pytestdef test_one():pytest.fail(msg='使用给定消息显式地设置用例为失败状态')def test_two():pytest.skip(msg='使用给定消息跳过测试用例。')def test_three():pytest.xfail(reason='由于给定的原因,强制标记失败测试用例或测试准备函数。')
3、pytest.importorskip
导入并返回请求的模块名,如果无法导入模块,则跳过当前测试。
import pytest# 导入并返回请求的模块名,如果无法导入模块,则跳过当前测试。 def test_one():mod1 = pytest.importorskip(modname='selenium')print(mod1)def test_two():mod2 = pytest.importorskip(modname='python-module')print(mod2)
4、pytest.exit
退出测试
import pytestdef test_one():passdef test_two():passpytest.exit(msg='退出测试,注意test_two也没有执行')def test_three():pass
5、pytest.main
主函数模式
import pytestdef test_one():passdef test_two():pass# 主函数模式运行测试用例 if __name__ == '__main__':pytest.main()
6、pytest.param
与mark.parametrize()一起使用,标记参数化的子用例。
import pytest# 和parametrize结合使用,指定参数,标记子用例预期失败状态XFAIL @pytest.mark.parametrize("test_input,expected", [("3+5", 8), pytest.param("6*9", 42, marks=pytest.mark.xfail)]) def test_eval(test_input, expected):assert eval(test_input) == expected
7、pytest.raises
with pytest.raises(),结合上下文管理器,用来捕获异常。
import pytestexception_tuple = (ZeroDivisionError, AssertionError)# with pytest.raises(),结合上下文管理器,用来捕获异常 def test_one():with pytest.raises(AssertionError) as exc_info:assert 1 == 2print(exc_info)# 多个异常元组 def test_two():with pytest.raises(exception_tuple) as exc_info:print(1 / 0)# 如果代码块没有引发预期的异常AssertionError,则置为失败FAILED def test_three():with pytest.raises(ZeroDivisionError):assert 1 == 2# 如果代码块没有异常,,则置为失败FAILED def test_four():with pytest.raises(exception_tuple):assert 1 == 1
8、pytest.warns
with pytest.warns() ,结合上下文管理器,可以检查代码是否发出特定的警告信息,使用方法与 raises 类似。
import warnings import pytestdef my_warning():warnings.warn("my warning", UserWarning)# with pytest.warns() ,结合上下文管理器,可以检查代码是否发出特定的警告信息,使用方法与 raises 类似。 def test_warning():with pytest.warns(UserWarning):my_warning()
9、pytest.deprecated_call
with deprecated_call(),结合上下文管理器,可用于确保代码块触发 DeprecationWarning 或 PendingDeprecationWarning:
import warnings from pytest import deprecated_calldef api_call_v2():warnings.warn('use v3 of this api', DeprecationWarning)return 200# with deprecated_call(),结合上下文管理器,可用于确保代码块触发 DeprecationWarning 或 PendingDeprecationWarning: def test_deprecated_call():with deprecated_call():assert api_call_v2() == 200
10、pytest.register_assert_rewrite
参考: 断言重写。
register_assert_rewrite(*names): 注册一个或多个要在导入时重写的模块名称。
此函数将确保此模块或程序包内的所有模块将重写其assert语句。因此,您应确保在实际导入模块之前调用此方法,如果您是使用包的插件,则通常在__init__.py中调用。
抛出:TypeError- 如果给定的模块名称不是字符串。
11、pytest.freeze_includes
参考: 冻结pytest。
freeze_includes(): 返回pytest使用的模块名称列表,应由cx_freeze包含。
reference:
API Reference — pytest documentation
pytest合集(5)— Function函数相关推荐
- pytest合集(4)— 使用pytest-html插件生成HTML测试报告
1.pytest-html插件安装 pytest-html 是 pytest 的一个插件,它为测试结果生成 HTML 报告. 要求: Python >=3.6 或 PyPy3. pip inst ...
- pytest合集(10)— Hook钩子函数
一.钩子函数 钩子函数这个称呼是很多开发语言中都会涉及到的一个东西. 1.理解钩子函数 如何理解钩子函数 - 知乎 2.pytest的钩子函数 Hooks钩子函数是pytest框架预留的函数,通过这些 ...
- 【Python常用函数合集】clip函数、range函数等
你应该听说过,应用Python,可以让你处理一天的重复工作量,缩短到几分钟甚至更短.从此解放上班时间,研究更多更有效率的工作方法.进一步提升工作效率,让工作更出彩.这不是广告,这是实锤图片. 本 ...
- pytest合集(6)— Fixture夹具
一.关于夹具 1.夹具介绍 简单来说,pytest中的夹具就是用来实现测试前的环境准备,提供测试数据和测试后的环境清理动作.类似于unittest框架里的setup(前置处理),teardown(后置 ...
- pytest合集(8)— 测试用例和夹具的参数化
一.测试用例参数化 pytest.mark.parametrize() 语法糖: parametrize(argnames, argvalues, indirect=False, ids=None, ...
- pytest合集(3)— 命令行参数
1.命令行参数大全 使用 pytest -h 可以查看 pytest 的命令行参数,有 10 大类共 132 个. 详见:Python pytest 132 个命令行参数用法 - 习久性成 - 博客园 ...
- pytest合集(7)— Mark标记
一.mark标记 1.介绍 Pytest提供的mark标记,允许我们标记测试函数,测试类和整个模块.通过不同的标记实现不同的运行策略,如标记冒烟测试用例. 2.特点 使用装饰器@pytest.mark ...
- pytest合集(14)— allure快速入门
一.allure环境部署 1.allure介绍 Allure Framework 是一个开源的,灵活的,轻量级,多语言的测试报告框架(工具). allure支持多种测试框架,如Pytest.TestN ...
- 2021Autojs全网最全几十种小游戏和自阅合集 (含源码)
此代码由日赚3万网autojs开发交流群整理提供,欢迎大家一起技术交流,查看更多脚本和源码: 一键加群.如有侵权,请联系我删除! //汉字大英雄.疯狂弹力球.水果爱消消.愤怒的小鸡.魔性小飞机.填空我 ...
最新文章
- 招聘|字节跳动招计算机视觉方向算法实习生
- 优化内核报错及解决方法
- JS中定义式函数与变量时函数的差别
- Java的知识点18——数组存储表格数据、冒泡排序的基础算法、冒泡排序的优化算法、二分法查找
- web02--jsp数据传递
- 构造函数为什么不能是虚函数 ( 转载自C/C++程序员之家)
- 类加载机制(整个过程详解)
- 10099 The Tourist Guide
- 清明赏春攻略——来一场技术踏青!
- 获取前一天的时间安排表_【央美考研】2021年硕士研究生招生入学考试时间安排...
- 什么是面向对象_11、面向对象的基本特征是什么
- Android总结篇系列:Activity中几个主要函数详解
- LINUX编译GtkLP(开源的打印机图形化工具)
- Swift - 使用下划线(_)来分隔数值中的数字
- linux环境编程apue,《UNIX环境高级编程》中apue.h的问题
- w ndows10专业版连接不上网,Win10电脑连不上网怎么回事?Win10电脑连不上网解决办法...
- uv422转换为yuv420_利用libswscale转换yuyv422到yuv422p或rgb之间的转换, 视频翻转
- 一阶微分方程的物理意义_薛定谔方程的物理意义
- spring框架使用JavaMailSenderImpl发送邮件
- 如何在 LaTeX 中画一个树状图(使用tikz和tikz-qtree包中的宏绘制树、森林、二叉树)