pytest配置文件pytest.ini的详细使用


Posted in Python onApril 17, 2021

前言

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

非test文件

pytest里面有些文件是非test文件

  • pytest.ini:pytest的主配置文件,可以改变pytest的默认行为
  • conftest.py:测试用例的一些fixture配置
  • _init_.py:识别该文件夹为python的package包

查看pytest.ini的配置选项

cmd执行

pytest --help

找到这部分内容

[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 in the command line.
  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 (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 report
  junit_logging (string):
                        Write captured log messages to JUnit report: one of no|log|system-out|system-err|out-err|all
  junit_log_passing_tests (bool):
                        Capture log information for passing tests to JUnit report:
  junit_duration_report (string):
                        Duration time to report: one of total|call
  junit_family (string):
                        Emit XML for schema: one of legacy|xunit1|xunit2
  doctest_optionflags (args):
                        option flags for doctests
  doctest_encoding (string):
                        encoding used for doctest files
  cache_dir (string):   cache directory path.
  filterwarnings (linelist):
                        Each line specifies a pattern for warnings.filterwarnings. Processed after -W/--pythonwarnings.
  log_print (bool):     default value for --no-print-logs
  log_level (string):   default value for --log-level
  log_format (string):  default value for --log-format
  log_date_format (string):
                        default value for --log-date-format
  log_cli (bool):       enable log display during test run (also known as "live logging").
  log_cli_level (string):
                        default value for --log-cli-level
  log_cli_format (string):
                        default value for --log-cli-format
  log_cli_date_format (string):
                        default value for --log-cli-date-format
  log_file (string):    default value for --log-file
  log_file_level (string):
                        default value for --log-file-level
  log_file_format (string):
                        default value for --log-file-format
  log_file_date_format (string):
                        default value for --log-file-date-format
  log_auto_indent (string):
                        default value for --log-auto-indent
  faulthandler_timeout (string):
                        Dump the traceback of all threads if a test takes more than TIMEOUT seconds to finish. Not
                        available on Windows.
  addopts (args):       extra command line options
  minversion (string):  minimally required pytest version
  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

pytest.ini应该放哪里?

就放在项目根目录下 ,不要乱放,不要乱起其他名字

接下来讲下常用的配置项

marks

作用:测试用例中添加了 @pytest.mark.webtest 装饰器,如果不添加marks选项的话,就会报warnings

格式:list列表类型

写法:

[pytest]
markers =
    weibo: this is weibo page
    toutiao: toutiao
    xinlang: xinlang

xfail_strict

作用:设置xfail_strict = True可以让那些标记为@pytest.mark.xfail但实际通过显示XPASS的测试用例被报告为失败

格式:True 、False(默认),1、0

写法:

[pytest]

# mark标记说明
markers =
    weibo: this is weibo page
    toutiao: toutiao
    xinlang: xinlang

xfail_strict = True

具体代码栗子

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

@pytest.mark.xfail()
def test_case1():
    a = "a"
    b = "b"
    assert a != b

collecting ... collected 1 item

02断言异常.py::test_case1 XPASS [100%]

============================= 1 xpassed in 0.02s ==============================

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

collecting ... collected 1 item

02断言异常.py::test_case1 FAILED                                         [100%]
02断言异常.py:54 (test_case1)
[XPASS(strict)] 

================================== FAILURES ===================================
_________________________________ test_case1 __________________________________
[XPASS(strict)] 
=========================== short test summary info ===========================
FAILED 02断言异常.py::test_case1
============================== 1 failed in 0.02s ==============================

addopts

作用:addopts参数可以更改默认命令行选项,这个当我们在cmd输入一堆指令去执行用例的时候,就可以用该参数代替了,省去重复性的敲命令工作

比如:想测试完生成报告,失败重跑两次,一共运行两次,通过分布式去测试,如果在cmd中写的话,命令会很长

pytest -v --rerun=2 --count=2 --html=report.html --self-contained-html -n=auto

每次都这样敲不太现实,addopts就可以完美解决这个问题

[pytest]

# mark
markers =
    weibo: this is weibo page
    toutiao: toutiao
    xinlang: xinlang

xfail_strict = True

# 命令行参数
addopts = -v --reruns=1 --count=2 --html=reports.html --self-contained-html -n=auto

加了addopts之后,我们在cmd中只需要敲pytest就可以生效了!!

log_cli

作用:控制台实时输出日志

格式:log_cli=True 或False(默认),或者log_cli=1 或 0

log_cli=0的运行结果

pytest配置文件pytest.ini的详细使用

log_cli=1的运行结果

pytest配置文件pytest.ini的详细使用

结论

很明显,加了log_cli=1之后,可以清晰看到哪个package下的哪个module下的哪个测试用例是否passed还是failed;

所以平时测试代码是否有问题的情况下推荐加!!!但如果拿去批量跑测试用例的话不建议加,谁知道会不会影响运行性能呢?

norecursedirs

作用:pytest 收集测试用例时,会递归遍历所有子目录,包括某些你明知道没必要遍历的目录,遇到这种情况,可以使用 norecursedirs 参数简化 pytest 的搜索工作【还是挺有用的!!!】

默认设置: norecursedirs = .* build dist CVS _darcs {arch} *.egg

正确写法:多个路径用空格隔开

[pytest]

norecursedirs = .* build dist CVS _darcs {arch} *.egg venv src resources log report util

更改测试用例收集规则

pytest默认的测试用例收集规则

  • 文件名以 test_*.py 文件和 *_test.py
  • 以  test_ 开头的函数
  • 以  Test 开头的类,不能包含 __init__ 方法
  • 以  test_ 开头的类里面的方法

我们是可以修改或者添加这个用例收集规则的;当然啦,是建议在原有的规则上添加的,如下配置

[pytest]

python_files =     test_*  *_test  test*
python_classes =   Test*   test*
python_functions = test_*  test*

到此这篇关于pytest配置文件pytest.ini的详细使用的文章就介绍到这了,更多相关pytest.ini配置内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python 登录网站详解及实例
Apr 11 Python
Python编程实现双击更新所有已安装python模块的方法
Jun 05 Python
Python MySQL数据库连接池组件pymysqlpool详解
Jul 07 Python
python3.6.3安装图文教程 TensorFlow安装配置方法
Jun 24 Python
python实现弹跳小球
May 13 Python
Python的bit_length函数来二进制的位数方法
Aug 27 Python
Python函数式编程指南:对生成器全面讲解
Nov 19 Python
Python.append()与Python.expand()用法详解
Dec 18 Python
python模拟预测一下新型冠状病毒肺炎的数据
Feb 01 Python
Python tkinter模版代码实例
Feb 05 Python
python函数map()和partial()的知识点总结
May 26 Python
Pytorch中的学习率衰减及其用法详解
Jun 05 Python
用Python简陋模拟n阶魔方
Python OpenCV快速入门教程
python小程序之飘落的银杏
Python Numpy之linspace用法说明
Apr 17 #Python
用Python的绘图库(matplotlib)绘制小波能量谱
用基于python的appium爬取b站直播消费记录
解决numpy数组互换两行及赋值的问题
Apr 17 #Python
You might like
PHP对XML内容进行修改和删除实例代码
2016/10/26 PHP
php7下的filesize函数
2019/09/30 PHP
总结一些js自定义的函数
2006/08/05 Javascript
WordPress JQuery处理沙发头像
2009/06/22 Javascript
菜鸟javascript基础整理1
2010/12/06 Javascript
善用事件代理,警惕闭包的性能陷阱。
2011/01/20 Javascript
javascript管中窥豹 形参与实参浅析
2011/12/17 Javascript
JavaScript及jquey实现多个数组的合并操作
2014/09/06 Javascript
JavaScript生成福利彩票双色球号码
2015/05/15 Javascript
js图片翻书效果代码分享
2015/08/20 Javascript
基于javascript实现图片滑动效果
2016/05/07 Javascript
将json转换成struts参数的方法
2016/11/08 Javascript
jquery插件开发之选项卡制作详解
2017/08/30 jQuery
微信小程序url与token设置详解
2017/09/26 Javascript
基于js文件加载优化(详解)
2018/01/03 Javascript
layui的数据表格+springmvc实现搜索功能的例子
2019/09/28 Javascript
vue-cli点击实现全屏功能
2020/03/07 Javascript
Django1.7+python 2.78+pycharm配置mysql数据库教程
2014/11/18 Python
Python的Tornado框架异步编程入门实例
2015/04/24 Python
Python获取昨天、今天、明天开始、结束时间戳的方法
2018/06/01 Python
面向对象学习之pygame坦克大战
2019/09/11 Python
在CentOS7下安装Python3教程解析
2020/07/09 Python
Python自动创建Excel并获取内容
2020/09/16 Python
python爬虫构建代理ip池抓取数据库的示例代码
2020/09/22 Python
孕妇装中的著名品牌:Isabella Oliver(伊莎贝拉·奥利弗)
2016/10/31 全球购物
搬家公司的创业计划书
2014/01/01 职场文书
应届中专生自荐书范文
2014/02/13 职场文书
财务人员的自我评价范文
2014/03/03 职场文书
市场开发与营销专业求职信范文
2014/05/01 职场文书
机械机修工岗位职责
2014/08/03 职场文书
销售竞赛活动方案
2014/08/23 职场文书
2014年个人债务授权委托书范本
2014/09/22 职场文书
勤俭节约倡议书范文
2015/04/29 职场文书
好段摘抄大全(48句)
2019/08/08 职场文书
中国十大神话动漫电影排行榜 哪吒登顶 白蛇缘起排第七
2022/03/21 国漫
Ruby序列化和持久化存储 Marshal和Pstore介绍
2022/04/18 Ruby