如何安装、利用、激活、禁用pytest插件

关注开源优测不迷路

大数据测试过程、策略及挑战

测试框架原理,构建成功的基石

在自动化测试工作之前,你应该知道的10条建议

在自动化测试中,重要的不是工具

在本文中,我们介绍如何安装和使用pytest第三方插件,对于如何自己开发pytest插件,我们后续写文继续更新。

通过python自带的pip命令,我们可以很轻易的安装、卸载pytest第三方插件:

安装pytest插件命令

nginx 复制代码
pip install pytest-plugin_name

卸载pytest插件命令

nginx 复制代码
pip uninstall pytest-plugin_name

注:plugin_name为pytest第三方插件名称

如果第三方pytest插件安装好了,pytest将会自动的发现和加载该插件,不需要你做过多的操作。

下面我们看一下一些常用的插件及介绍:

  • pytest-django: 为django应用编写测试,使用pytest集成

  • Pytest-twisted: 为twisted应用编写测试

  • Pytest-cov: 覆盖率报告,兼容分布式测试

  • pytest-xdist: 将测试分发到cpu和远程主机,以沙盒模式运行(允许分段错误存活),以loop on failing模式运行,在文件更改时自动重新运行失败的测试

  • Pytest-instafail: 在测试运行时报告失败。

  • Pytest-bdd: 使用bdd(行为驱动测试)编写测试。

  • Pytest-timeout: 根据函数标记或全局定义使测试超时

  • pytest-pep8: 使用pep8选项以启用pep8遵从性检查

  • Pytest-flakes: 使用pyflakes检查源代码

  • allure-pytest: 通过allure-pytest生成测试报告

为了查看pytest完整的插件列表,可以查看Pytest Plugin List --- pytest documentation链接:

https://docs.pytest.org/en/8.1.x/reference/plugin_list.html#plugin-list

这里官方提供的数百种插件供你选择和使用。

在pytset中提供了其他方式来加载插件,例如在模块中使用pytest------plugins或conftest文件来加载,具体方式如下

nginx 复制代码
pytest_plugins = ("app.testsupport.myplugin")

这样当测试模块或conftest插件被加载时,pytest_plugins指定的插件也会被加载。

根据官方的说明:并不建议在非根conftest.py文件中使用pytest_plugins变量来加载指定的插件,所以这种方式大家了解一下,不熟悉pytest底层机制的人,不建议使用。

如何查看pytest插件是激活状态的?

通过下面的命令即可知道那些pytest插件是出于可用状态,自己试试命令吧,看输出什么信息:

nginx 复制代码
pytest --trace-config

如何禁用pytest插件?

我们可以通过下面的命令禁用pytest插件。

nginx 复制代码
pytest -p no:NAME

NAME为插件名称

注: 对于某些我们不需要的插件,但又不想卸载,那我们可以使用这个命令。

相关推荐
摘星编程5 天前
AI 帮我写单测:pytest 覆盖率提升 40% 的协作日志
人工智能·pytest·测试驱动开发·代码覆盖率·ai协作开发
lucia_zl8 天前
pytest并发测试,资源问题导致用例失败解决办法
pytest
鱼鱼说测试8 天前
Selenium4+Pytest自动化测试框架实战
pytest
XYiFfang14 天前
【Pytest】解决Pytest中Teardown钩子的TypeError:实例方法与类方法的调用差异
python·pytest
Kingairy16 天前
Pytest 插件:pytest_runtest_protocol
python·pytest
AIZHINAN16 天前
Pytest 插件介绍和开发
测试工具·pytest·插件开发
灰阳阳18 天前
替身演员的艺术:pytest-mock 从入门到飙戏
自动化测试·python·pytest·unit testing·pytest-mock
年年测试22 天前
Playwright与PyTest结合指南
pytest
focksorCr22 天前
pytest 并发执行用例(基于受限的测试资源)
python·pytest