Pytest3种配置文件方式

简介: Pytest3种配置文件方式

配置介绍

pytest 的主配置文件,可以改变 pytest 的默认行为,执行 pytest -h,这里有很多配置均可用于 pytest.ini配置

(venv) D:\Python_test\pythonpp\pytest_>pytest -h
[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:
  markers (linelist):   Markers for test functions
  empty_parameter_set_mark (string):
                        Default marker for empty parametersets
  norecursedirs (args): Directory patterns to avoid for recursion
  testpaths (args):     Directories to search for tests when no files or directories are given on the command line
  filterwarnings (linelist):
                        Each line specifies a pattern for warnings.filterwarnings. Processed after -W/--pythonwarnings.
  usefixtures (args):   List of default fixtures to be used with this project
  python_files (args):  Glob-style file patterns for Python test module discovery
  python_classes (args):
                        Prefixes or glob names for Python test class discovery
  python_functions (args):
                        Prefixes or glob names for Python test function and method discovery
  disable_test_id_escaping_and_forfeit_all_rights_to_community_support (bool):
                        Disable string escape non-ASCII characters, might cause unwanted side effects(use at your own risk)
  console_output_style (string):
                        Console output: "classic", or with additional progress information ("progress" (percentage) | "count")
  xfail_strict (bool):  Default for the strict parameter of xfail markers when not given explicitly (d
to see available markers type: pytest --markers
to see available fixtures type: pytest --fixtures
(shown according to specified file_or_dir or current dir if not specified; fixtures with leading '_' are only shown with the '-v' option

输入pytest -h其实不止这些命令,我只是截取出本章最主要的部分。

配置案例

# pytest.ini
[pytest]
# 命令行执行参数
addopts = -vs
# 排除目录
norecursedirs = no_Case
# 默认执行目录
testpaths = ./
# 执行规则-class
python_classes = Test*
# 执行规则-py 文件
python_files = test*
# 执行规则-function
python_functions = test*
# xfail 标志规则
xfail_strict = false
# 自定义注册标志
markers =
    login: 登陆类标志
    information: 信息页
    index: 首页

pytest.ini 中最好不要用中文,如果使用的话,请将文件编码改成 gbk  ,否则还请删除ini配置文件中的中文。

目录结构

此处建议新建一个环境,如果你的pytest本就是一个新环境,没有其他的东西,可以不用新建。因为环境包如果过多,会对运行造成干扰。

pytest_
 Case
  test_a.py
 no_Case
  test_b.py
 pytest.ini
 run.py

pytest.ini上面已经展示了,看看run.py:

import pytest
if __name__ == '__main__':
    pytest.main()

就是执行入口,本章我们不用命令执行了。此外还有两个目录就是Case跟no_Case,放用例的地方,

命令样式讲解

addopts

[pytest]
addopts = -vs
# ---等价于---
pytest.main(["-vs"])

addopts可以接收很多了参数,换句话说main中能接收的参数,此处都能写。

目录规则

# 排除目录
norecursedirs = no_Case
# 默认执行目录
testpaths = ./

如果你发现目录不论怎么改都没有生效(能检测到用例),那么就请按照你上面所说重新弄一个环境。如果环境OK了,可以检测到目录了,会报错:拒绝访问亦或者ERROR  No escaped character,亦或者norecursedirs的目录用例运行了,那么都可以归结于目录路径写错了。 当然,你可以通过控制默认执行目录达到排除目录的效果。

用例执行规则

# 执行规则-class
python_classes = Test*
# 执行规则-py 文件
python_files = test*
# 执行规则-function
python_functions = test*

当然,pytest默认设置的也是class检测Test开头,用例是test,我们也能换成自己想要的样式:

class Qing_A:
    def qing_a(self):
        print("我是清安")
    def qing_b(self):
        print("我是拾贰")

那么pytest.ini因该如何写呢:

# pytest.ini
[pytest]
# 命令行执行参数
addopts = -vs
# 排除目录
norecursedirs = no_Case
# 默认执行目录
testpaths = ./
# 执行规则-class
python_classes = Qing*
# 执行规则-py 文件
python_files = test*
# 执行规则-function
python_functions = qing*

py文件命名此处我就没改,可以自己试试,类与函数用例我是改了。除了这样还可以:

# 执行规则-class
python_classes = Qing* *Qing
# 执行规则-py 文件
python_files = test*
# 执行规则-function
python_functions = qing* *test

代码出仅需要添加:

class B_Qing:
    def b_test(self):
        print("我是b用例")

就能检测到自定义的用例了,看看结果:

Case/test_a.py::Qing_A::qing_a 我是清安
PASSED
Case/test_a.py::Qing_A::qing_b 我是拾贰
PASSED
Case/test_a.py::B_Qing::b_test 我是b用例
PASSED

注意点

用例检测这里,如果你写了py,class,function,那么它会看着这样的逻辑进行检测,如果python_files都没有检测到了,剩下的python_classes以及python_functions也就不能进行了。其次是python_classes如果有则优先检测,如果没有则检测python_functions。

自定义标志

  1. pytest.ini 中最好不要用中文,如果使用的话,大家要将文件编码改成 gbk
  2. 标志名称没有限制,建议大家参考模块命名,要有业务含义,不要随心而写
  3. 所有的自定义标志,建议大家在 pytest.ini 中进行统一管理和通过命令参数--strict-markers 进行授权(pytest 其实不强制)
  4. pytest 中的 markers 配置,相当于我们对业务的一种设计归类,尤其是大项目时非常重要
# 自定义注册标志
markers =
    login: 登陆类标志
    information: 信息页
    index: 首页
import pytest
@pytest.mark.login
class Qing_A:
    def qing_a(self):
        print("我是清安")
    @pytest.mark.information
    def qing_b(self):
        print("我是拾贰")
@pytest.mark.index
class B_Qing:
    def b_test(self):
        print("我是b用例")

那么如何运行指定的标志呢:

[pytest]
# 命令行执行参数
addopts = -vs -m login
# 或者
addopts = -vs -m  "not login"
# 或者
addopts = -vs -m "login or index"
"""not login结果示例"""
Case/test_a.py::Qing_A::qing_a 我是清安
PASSED
Case/test_a.py::Qing_A::qing_b 我是拾贰
PASSED

乱码问题

有些人的情况或许跟我一样,pytest.ini输入的中文是乱码或者读取出来的是乱码,这时候可以在设置中的:

修改成GBK即可。

小结

关于并未完全讲完的一些参数可以来这里直接CTRL + F搜索:https://www.osgeo.cn/pytest/reference.html#ini-options-ref一定是pytest.ini文件吗?其他的配置文件不行吗。官方介绍到还有.toml,tox.ini,setup.cfg,其中setup.cfg是不被推荐使用的,官方文档这样说道:

💥警告 用法 setup.cfg 除非用于非常简单的用例,否则不推荐使用。 .cfg 文件使用不同于 pytest.ini 和 tox.ini 这可能会导致难以追踪的问题。如果可能,建议使用后一个文件,或者 pyproject.toml ,以保存pytest配置。

关于toml配置文件

[tool.pytest.ini_options]
addopts = "-vs -m login"
norecursedirs = "no_Case"
testpaths = "./"
python_classes = "Qing* *Qing"
python_files = "test*"
python_functions = "qing* *test"
xfail_strict = "false"
markers = ["login:登陆类标志", "information:信息页", "index:首页"]

如上是改写的pytest.ini配置文件的。写法上有些不一样,注意点即可。此外关于官网的介绍,其实其他地方也可以改成类似于markers的写法:

[tool.pytest.ini_options]
addopts = "-vs -m login"
norecursedirs = "no_Case"
testpaths = "./"
python_classes = ["Qing*","*Qing"]
python_files = "test*"
python_functions = ["qing*","*test"]
xfail_strict = "false"
markers = ["login:登陆类标志", "information:信息页", "index:首页"]

关于tox.ini配置文件

[pytest]
addopts = -vs --strict-markers -m "not index"
norecursedirs = no_Case
testpaths = ./
python_classes = Qing* *Qing
python_files = test*
python_functions = qing* *test
xfail_strict = false
markers =
    login: "login info"
    information: "information"
    index: "index"

此处我删除了中文,是因为GBK编码问题,不想处理了,直接删除采用英文省事。 假如你实在解决不论编码问题,就采用全英文吧。

目录
相关文章
|
数据采集 数据可视化 数据挖掘
交互式数据分析:使用Jupyter Notebooks和IPython提高生产力
【4月更文挑战第12天】Jupyter Notebooks和IPython是交互式数据分析的强大工具,提供了一个集成环境,支持多种编程语言,提升效率并减少错误。它们具有交互式编程、丰富库支持、可扩展性和协作功能。基本流程包括数据导入(如使用Pandas从CSV加载)、预处理、分析(利用Pandas、NumPy、Matplotlib等)、模型选择与训练(如Scikit-learn的RandomForestClassifier)以及模型评估和优化。
320 2
|
人工智能 Java
零基础五步骤,从零开始天猫精灵
零基础五步骤,从零开始天猫精灵
1355 1
零基础五步骤,从零开始天猫精灵
|
消息中间件 缓存 监控
GitHub上获赞上万的阿里亿级并发系统设计手册,让你吊打面试官
金九银十已经接近尾声,很多没有在这个时间段找到工作的小伙伴已经开始备战秋招了,在这里给大家分享一份阿里10亿级并发系统设计手册,专门给没有系统设计相关经验的小伙伴应对面试用的,下面将这么手册的内容以截图的形式展示给大家,有需要的小伙伴可以文末获取↓↓↓此份手册又份为六个部分,基础篇、数据库篇、缓存篇、消息队列篇、分布式服务篇、维护篇、实战篇共计328页 目录总览 基础篇 高并发代表着大流量,高并发系统设计的魅力就在于我们能够凭借自己的聪明才智设计巧妙的方案,从而抵抗巨大流量的冲击,带给用户更好的使用体验。这些方案好似能操纵流量,让流量更加平稳得被系统中的服务和组件处理。
GitHub上获赞上万的阿里亿级并发系统设计手册,让你吊打面试官
QGS
|
NoSQL 网络协议 Redis
Redis7配置哨兵模式(一主二从三哨兵)
Redis7配置哨兵模式(一主二从三哨兵)
QGS
737 1
|
9月前
|
机器学习/深度学习 人工智能 数据处理
[python 技巧] 快速掌握Streamlit: python快速原型开发工具
本文旨在快速上手python的streamlit库,包括安装,输入数据,绘制图表,基础控件,进度条,免费部署。
997 64
[python 技巧] 快速掌握Streamlit: python快速原型开发工具
|
监控 iOS开发
iOS 逆向编程(十八)Reveal 详细安装(以及安装问题解决)(下)
iOS 逆向编程(十八)Reveal 详细安装(以及安装问题解决)(下)
477 0
|
11月前
|
Java 关系型数据库 数据库连接
SpringBoot项目使用yml文件链接数据库异常
【10月更文挑战第3天】Spring Boot项目中数据库连接问题可能源于配置错误或依赖缺失。YAML配置文件的格式不正确,如缩进错误,会导致解析失败;而数据库驱动不匹配、连接字符串或认证信息错误同样引发连接异常。解决方法包括检查并修正YAML格式,确认配置属性无误,以及添加正确的数据库驱动依赖。利用日志记录和异常信息分析可辅助问题排查。
1057 11
|
关系型数据库 MySQL 分布式数据库
PolarDB开源社区动态:最新版本功能亮点与更新解读
【9月更文挑战第6天】随着云计算技术的发展,分布式数据库系统成为企业数据处理的核心。阿里云的云原生数据库PolarDB自开源以来备受关注,近日发布的最新版本在内核稳定性、性能、分布式CDC架构及基于时间点的恢复等方面均有显著提升,并新增了MySQL一键导入功能。本文将解读这些新特性并提供示例代码,帮助企业更好地利用PolarDB处理实时数据同步和离线分析任务,提升数据安全性。未来,PolarDB将继续创新,为企业提供更高效的数据处理服务。
523 4
|
jenkins Java Shell
解决jenkins结束后kill掉衍生进程
解决jenkins结束后kill掉衍生进程
|
jenkins 持续交付 网络安全
Jenkins Pipeline 流水线 - 完整构建 Pipeline Script 脚本
Jenkins Pipeline 流水线 - 完整构建 Pipeline Script 脚本
241 0

热门文章

最新文章