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

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

相关推荐
久久学姐3 天前
Python+Playwright+Pytest+BDD,用FSM打造高效测试框架
python·pytest·bdd·playwright·fsm
小白上线*^_^*3 天前
Pytest 自动化测试框架速通指南(二)
软件测试·pytest·python测试框架·ai测试基础
Logintern094 天前
pytest的AST 断言重写
pytest
Tinyfacture5 天前
接口自动化之添加商品(pytest)
python·测试工具·自动化·pytest
ji_shuke7 天前
从零深入:基于 Playwright + Pytest + Allure 的企业级 Web 端到端自动化测试框架实战
前端·自动化测试·docker·jenkins·pytest·allure·playwright
祉猷并茂,雯华若锦8 天前
Selenium+Pytest自动化测试框架实战
selenium·测试工具·pytest
祉猷并茂,雯华若锦10 天前
Pytest+Selenium自动化测试:失败自动截图集成Allure报告
selenium·测试工具·pytest
我的xiaodoujiao13 天前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest
一孤程16 天前
Pytest+Selenium搭建自动化框架-保姆级实战教程
selenium·自动化·pytest
名字还没想好☜20 天前
用 pytest fixture 组织可维护的测试:告别一堆重复的 setUp
数据库·python·oracle·pytest·测试