如何安装、利用、激活、禁用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为插件名称

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

相关推荐
m0_632482509 小时前
Jenkins + Pytest +allure接口自动化测试配置与操作
jenkins·集成测试·pytest·jenkins配置
李星星BruceL9 小时前
Pytest第三章(参考指南1)
python·自动化·pytest
哎呀呦呵9 小时前
pytest基本使用
python·pytest
Kristen_YXQDN9 小时前
PyCharm 中 pytest 运行 python 测试文件报错:D:\Python_file\.venv\Scripts\python.exe: No module named pytest
运维·开发语言·python·pycharm·pytest
Low--Key9 小时前
pytest框架快速入门
python·自动化·pytest
姜西西_9 小时前
自动化测试框架pytest之fixture
android·java·pytest
测试开发Kevin9 小时前
超级实用!汇总pytest中那些常用的参数
单元测试·pytest
Beaman10249 小时前
pytest框架
python·pytest
昔时扬尘处1 天前
【Files Content Replace】文件夹文件内容批量替换自动化测试脚本
c语言·python·pytest·adi
我的xiaodoujiao1 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 32--开源电商商城系统项目实战--如何区分登录状态
python·学习·测试工具·pytest