前言

pytest.ini文件是pytest的主配置文件;可以改变pytest的运行方式;它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行。
pytest.ini文件的位置一般放在项目的根目录下,不能随便放,也不能更改名字。
查看pytest.ini文件的配置选项
cmd下执行 pytest -h 或者 pytest --help 可以查看配置选项,找到如下内容:

[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:markers (linelist):   markers for test functionsempty_parameter_set_mark (string):default marker for empty parametersetsnorecursedirs (args): directory patterns to avoid for recursiontestpaths (args):     directories to search for tests when no files or directories are given in 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 projectpython_files (args):  glob-style file patterns for Python test module discoverypython_classes (args):prefixes or glob names for Python test class discoverypython_functions (args):prefixes or glob names for Python test function and method discoverydisable_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 (default: False)enable_assertion_pass_hook (bool):Enables the pytest_assertion_pass hook.Make sure to delete any previously generated pyc cache files.junit_suite_name (string):Test suite name for JUnit reportjunit_logging (string):Write captured log messages to JUnit report: one of no|log|system-out|system-err|out-err|alljunit_log_passing_tests (bool):Capture log information for passing tests to JUnit report:junit_duration_report (string):Duration time to report: one of total|calljunit_family (string):Emit XML for schema: one of legacy|xunit1|xunit2doctest_optionflags (args):option flags for doctestsdoctest_encoding (string):encoding used for doctest filescache_dir (string):   cache directory path.log_level (string):   default value for --log-levellog_format (string):  default value for --log-formatlog_date_format (string):default value for --log-date-formatlog_cli (bool):       enable log display during test run (also known as "live logging").log_cli_level (string):default value for --log-cli-levellog_cli_format (string):default value for --log-cli-formatlog_cli_date_format (string):default value for --log-cli-date-formatlog_file (string):    default value for --log-filelog_file_level (string):default value for --log-file-levellog_file_format (string):default value for --log-file-formatlog_file_date_format (string):default value for --log-file-date-formatlog_auto_indent (string):default value for --log-auto-indentfaulthandler_timeout (string):Dump the traceback of all threads if a test takes more than TIMEOUT seconds to finish.addopts (args):       extra command line optionsminversion (string):  minimally required pytest versionrequired_plugins (args):plugins that must be present for pytest to runrender_collapsed (bool):Open the report with all rows collapsed. Useful for very large reportsmax_asset_filename_length (string):set the maximum filename length for assets attached to the html report.rsyncdirs (pathlist): list of (relative) paths to be rsynced for remote distributed testing.rsyncignore (pathlist):list of (relative) glob-style paths to be ignored for rsyncing.looponfailroots (pathlist):directories to check for changes

常用的配置选项
markers
作用:测试用例中使用@pytest.mark.slow装饰器,如果不添加markers选项就会报warning
格式:list列表类型
写法:

# file_name: pytest.ini[pytest]
markers =slow: run slow mark casefast: run fast mark case

举例:
①当使用 @pytest.mark. 标记名称时,如果使用的自定义标记,当在执行测试用例追加命令行参数 -m= 标记名称时,虽然不会影响测试执行,但是在执行后会出现告警提示。

②此时可以在 pytest.ini 配置文件中增加 markers 字段注册标记名称。【将自定义的标签注册,注册后pytest可以识别出来后就不会报出警告信息】
[pytest] # 注册标记名称 markers = smoke: 冒烟测试用例 normal: 正常用例
③添加后字段注册标记名称后,使用 pytest --markers 命令行参数运行可以查看到添加的标记名称。

④再次执行用例:运行结果在控制台没有告警提示

【注意】:在自定义标签的的同时应该尽量避免使用如下关键字:【pytest测试框架的内置标签,可以直接使用,不需要重新在pytest.ini文件中注册】
① usefixtures ② usefixtures ③ skip ④ skipif ⑤ xfail ⑥ parametrize
xfail_strict
作用:设置xfail_strict=true可以让那边标记为@pytest.mark.xfail 但实际通过显示为XPASS 的测试用例被报告为失败。
格式:True、False(默认)、1、0

# file_name: pytest.ini[pytest]
markers =slow: run slow mark casefast: run fast mark casexfail_strict = true

举例:

# file_name: test_xfail.pyimport pytestclass Test_C:@pytest.mark.xfaildef test_c(self):print('\n------------------> test_c has ran')a = 'hello'b = 'hello world'assert a != bif __name__ == '__main__':pytest.main(['-s', 'test_xfail.py'])

没有设置xfail_strict = True 时,测试结果显示XPASS

设置xfail_strict = True 时,测试结果显示failed

addopts
作用:addopts参数可以更改默认的命令行选项;这个参数在我们需要在命令行中输入大一堆指令来执行测试用例时会用到,这个时候就可以配置文件中配置这个参数来代替,省掉很多重复的工作
例如:我想在测试结束之后,生成测试报告,失败的测试用例重跑两次,如果通过命令行输入指令来执行的话,指令会很长: pytest -v --reruns 2 --html=report.html --self-contained-html
如果每次执行都要输入上面的指令会很繁琐,这个时候我们通过配置addopts参数来解决这个问题:

# file_name: pytest.ini[pytest]
markers =slow: run slow mark casefast: run fast mark casexfail_strict = Trueaddopts = -v --reruns=2 --html=report.html --self-contained-html  # 多个命令行参数用空格分隔开,可以添加多个命令行参数

这样加上addopts后,我们再次进入cmd命令行执行时,只要输入pytest就可以默认带上这些参数了。
log_cli
作用:控制台实时输出日志
格式: log_cli=True 或 log_cli=False(默认),或者 log_cli=1 或 log_cli=0
①设置 log_cli=True 时,运行结果为:

② 设置 log_cli=False时,运行结果为:

结论:
当我们设置 log_cli=True时,在控制台使用命令行参数运行测试用例时可以看到程序里写入的日志;可以非常清晰的看出具体的是哪个package下的哪个module下的哪个测试用例是passed还是failed;
所以我们平时在调试代码是否有问题时推荐加上 log_cli=True ,当测试用例调试通过之后批量执行时就可以去掉了。

norecursedirs
①norecursedirs作用:pytest在收集测试用例的时候,会递归遍历当前目录下的所有子目录,当我们需要某些目录下的用例不要执行时,就可以通过设置norecursedirs参数来实现这个功能。

# file_name: pytest.ini[pytest]
markers =slow: run slow mark casefast: run fast mark casexfail_strict = Trueaddopts = -v --reruns=2 --html=report.html --self-contained-htmllog_cli = Falsenorecursedirs = venv report util log  # 多个目录需要空格分开,可以配置多个

上面的配置表示venv report util log这4个目录下的用例需要过滤掉不执行。
②pytest之命令行参数 --ignore 用法
文件目录结构如下:
├── demo
 ├── all
  │ ├── a_a_test
  │ │ └── test_1.py
  │ ├── b_a_test
  │ │ └── test_2.py
  │ └── c
  │ └── test_2.py
1、忽略 c 文件夹下的脚本:

# demo.pypytest --ignore=all/c # c后面不要有/,也不要写成c/*# 注意,多个目录忽略,加多个--ignore即可,中间用空格做间隔。

2、忽略某一类脚本:

# demo.pypytest --ignore-glob='*_a_test'  #  glob模糊匹配文件夹# 会忽略a_a_test 和 b_a_test 目录下的脚本

3、实例:可以作为pytest命令行参数之一集成至pytest.ini文件中的addopts参数中,与上述案例的效果相同

testpaths
testpaths 是一系列相对于根目录的路径,用于限定测试用例的搜索范围。只有在pytest未指定文件目录参数或测试用例标识符时,该选项才会启用

【注意】: ./ 代表根路径,会随着你执行所在目录变化而变化,如果你执行目录下没有TestCase文件夹,那么该命令无效
更改测试用例的收集规则
pytest默认的测试用例收集规则为:

  • 文件名匹配 test_*.py 或 *_test.py
  • 以 test_ 开头的函数
  • 以 Test_ 开头的类,不能包含_init_初始化方法
  • 类中以 test_ 开头的方法

上面的默认规则我们是可以通过配置文件的设置来修改的

# file_name: pytest.ini[pytest]
testpaths = xdist_studypython_files = test*.pypython_classes = Test*python_functions = test_*

testpaths:配置在哪个目录下搜索测试用例,可自定义,可以配置多个,多个用空格隔开
python_files:用来配置搜索的测试用例的文件名称,可自定义,可以配置多个,多个用空格隔开
python_classes:配置搜索的测试用例的类名,可自定义,可以配置多个,多个用空格隔开
python_functions:配置搜索的测试用例的方法名,可自定义,可以配置多个,多个用空格隔开
上面的配置表示:在xdist_study目录下,搜索以test开头,以.py结尾的文件,以Test开头的类,以test_开头的方法。
minversion
指定pytest的最低版本

如果有人使用老版本(小于3.0)pytest运行测试,就会得到一个报错信息
pytest.ini配置文件的参数值会被命令行覆盖
当ini配置文件的参数与执行测试用例文件的命令行参数重复时,命令行参数值会覆盖ini配置文件中定义的参数值。
举例:命令行参数值为3,pytest.ini参数值为2,实际执行的参数值为3

我这里给你们分享一下我所积累和真理的文档和学习资料有需要是领取就可以了

1、学习思路和方法

这个大纲涵盖了目前市面上企业百分之99的技术,这个大纲很详细的写了你该学习什么内容,企业会用到什么内容。总共十个专题足够你学习

2、想学习却无从下手,该如何学习?

这里我准备了对应上面的每个知识点的学习资料、可以自学神器,已经项目练手。

3、软件测试/自动化测试【全家桶装】学习中的工具、安装包、插件....

4、有了安装包和学习资料,没有项目实战怎么办,我这里都已经准备好了往下看

5、如何领取这些配套资料和学习思路图,以及项目实战源码。

这些资料都已经让我准备在一个php网页里面了,可以在里面领取扫码或者进Q群交流都可以暗号和备注是111
最后送上一句话:
世界的模样取决于你凝视它的目光,自己的价值取决于你的追求和心态,一切美好的愿望,不在等待中拥有,而是在奋斗中争取。
如果我的博客对你有帮助、如果你喜欢我的文章内容,请 “点赞” “评论” “收藏” 一键三连哦!

Pytest框架系列——配置文件Pytest.ini相关推荐

  1. pytest框架快速入门-pytest运行时参数说明,pytest详解,pytest.ini详解

    废话少说,我们直接上干货. 一.Pytest简介 1.pytest是一个非常成熟的全功能的Python测试框架,主要有一下几个特点: 简单灵活,容易上手,支持参数化 2.能够支持简单的单元测试和复杂的 ...

  2. pytest十三:配置文件 pytest.ini

    pytest 配置文件可以改变 pytest 的运行方式,它是一个固定的文件 pytest.ini 文件,读取配置信息,按指定的方式去运行. ini 配置文件pytest 里面有些文件是非 test ...

  3. 『德不孤』Pytest框架 — 14、Pytest参数化

    文章目录 1.Pytest参数化说明 2.Pytest参数化方式 3.parametrize装饰器参数说明 4.Pytest参数化(单个参数) 5.Pytest参数化(多个参数) 6.ids参数说明 ...

  4. 面经-软件测试面试常见面试题全套合集系列unittest框架面试题 pytest框架面试题 性能测试面试题 4-4

    目录 前言: 十二. Unittest框架 12.1  你们自动化用例是怎么管理的? 12.2  Web UI自动化都用到过哪些库? 12.3  Unittest框架的原理? 12.4  Unitte ...

  5. Pytest框架集成Allure定制测试报告详解(一)

    Allure简介 Allure是一款非常轻量级并且非常灵活的开源测试报告生成框架. 它支持绝大多数测试框架, 例如TestNG.Pytest.JUint等.它简单易用,易于集成.下面就Pytest如何 ...

  6. 自动化测试框架-pytest框架入门篇

    前段时间,九九姐更新了自动化测试中unittest框架,然后有很多小伙伴私聊九九姐,说想学习一下pytest框架的内容.既然大家已经说话了,九九姐怎么能不安排呢,所以今天就给大家说一说pytest框架 ...

  7. pytest配置文件pytest.ini

    Python微信订餐小程序课程视频 https://edu.csdn.net/course/detail/36074 Python实战量化交易理财系统 https://edu.csdn.net/cou ...

  8. 2022最新最全的pytest配置文件pytest.ini

    说明: pytest.ini是pytest的全局配置文件,一般放在项目的根目录下 是一个固定的文件-pytest.ini 可以改变pytest的运行方式,设置配置信息,读取后按照配置的内容去运行 点我 ...

  9. 【pytest】pytest配置文件pytest.ini详解

    文章目录 前言 pytest.ini的内容构成 配置项markers 配置项testpaths 配置项addopts 前言 说到配置,大家可能想到的是不经常更改的内容,比如Django里的settin ...

最新文章

  1. 搭建Windows Server 2008故障转移群集
  2. WEB API的安全问题
  3. 《Python 3程序开发指南(第2版•修订版)》——1.2 Python的关键要素
  4. 18_clickhouse副本同步与高可用功能验证,分布式表与集群配置,数据副本与复制表,ZooKeeper整合,创建复制表,副本同步机制,数据原子写入与去重,负载平衡策略,案例(学习笔记)
  5. [LeetCode]二进制求和
  6. 软工网络15团队作业4——Alpha阶段敏捷冲刺之Scrum 冲刺博客(Day5)
  7. python po设计模式_Python Selenium设计模式 - PO设计模式
  8. mysql定时任务 分钟_mysql定时任务
  9. oracle 设置忽略关键字,记oracle里continue关键字的陷阱
  10. Arduino学习笔记23
  11. 绝版经典《Linux与UNIX Shell编程指南》中文文字PDF版
  12. python下载文件的11种方式_python 下载文件的多种方法汇总
  13. php如何无水印解析快手,快手短视频无水印解析过程及代码
  14. php漂浮广告代码,JS实现网站悬浮广告的代码
  15. Oracle 并行操作
  16. 吊打面试官的硬核法宝~阿里架构师最新总结软件测试面试宝典
  17. An attribute defined in json.encoder line 158 hides this methodpylint(method-hidden)
  18. 8小时浓度均值即连续8个小时浓度的平均值
  19. tomcat下载与安装win11
  20. 店长必看:如何利用微信会员管理系统做好店铺营销和管理?

热门文章

  1. CANoe的使用之Diva工程新建
  2. 力扣 面试题 17.09. 第 k 个数
  3. django - html模板
  4. Matlab Robotic Toolbox安裝
  5. 撸一撸Spring Framework-IoC-概述
  6. 7-7 斐波那契(Fibonacci)数列前20项 (10 分)
  7. 重启计算机连线的标志是,win10右下角总出现一个小地球图标怎么办_win10电脑网络连接图标变成地球如何解决...
  8. Oracle 表创建和表管理
  9. oracle-表创建
  10. 高效的多维空间点索引算法 — Geohash 和 Google S2(转)