Pytest(14)pytest.ini配置文件

Pytest(14)pytest.ini配置文件前言pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行查看pytest.ini的配置选项pytest-h找到以下

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

前言

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

查看pytest.ini的配置选项

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 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 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.
  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.
  addopts (args):       extra command line options
  minversion (string):  minimally required pytest version
  required_plugins (args):
                        plugins that must be present for pytest to run
  base_url (string):    base url for the application under test.
  render_collapsed (bool):
                        Open the report with all rows collapsed. Useful for very large reports
  max_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

 

注意

pytest一定要放在项目的根目录,名字也要命名为pytest.ini
 

mark标记

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

[pytest]
markers =
  app:  Run the app case
  web:  Run the web case

 

addopts

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

比如:想测试完生成报告,失败重跑两次,如果在bash中写的话,命令会很长

pytest -v --reruns=2 --alluredir ./report --clean-alluredir

每次输入这么多,不太好记住,于是可以加到pytest.ini里

[pytest]
markers =
  app:  Run the app case
  web:  Run the web case
addopts = -v --reruns=2 --alluredir ./report --clean-alluredir

这样我下次打开cmd,直接输入pytest,它就能默认带上这些参数了
 

norecursedirs

作用:pytest 收集测试用例时,会递归遍历所有子目录,包括某些你明知道没必要遍历的目录,一般情况下项目的用例都放在case文件夹下,所以除了case文件夹,其他项目的路径都可以不必递归

默认设置: norecursedirs = .* build dist CVS _darcs {arch} *.egg
正确写法:在上面默认值后面加上除了case的所有路径,多个路径用空格隔开(一)

norecursedirs = .* build dist CVS _darcs {arch} *.egg API common configFile data logs report

 

自定义匹配规则

查看pytest -h 查看命令行参数找到 [pytest] ini-options

  • python_files (args) 匹配 python 用例文件, 如test_*.py、 *_test.py
  • python_classes (args) 匹配 class 类名称 如Test*.py
  • python_functions (args) 匹配函数和class里面方法 如test_*

假如我们想把匹配规则改为函数名以best_*开头

[pytest]

python_files =  test_*.py
python_classes = Test*
python_functions = best_*

这样以后pytest就匹配的都是以best开头的用例了

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/165680.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月30日 上午8:00
下一篇 2022年7月30日 上午8:00


相关推荐

  • Openclaw硬件部署成本对比:Mini Mac、电脑和AI边缘计算盒子

    Openclaw硬件部署成本对比:Mini Mac、电脑和AI边缘计算盒子

    2026年3月16日
    2
  • 以《简单易懂》的语言带你搞懂逻辑回归算法【附Python代码详解】机器学习系列之逻辑回归篇

    以《简单易懂》的语言带你搞懂逻辑回归算法【附Python代码详解】机器学习系列之逻辑回归篇目录必看前言逻辑回归算法1概述2基本原理3sklearn实现3.1导入数据(乳腺癌数据集)3.2建模3.3绘制学习曲线3.4网格搜索-确定最优参数结束语必看前言这一篇文章,我会详细从机器学习的角度介绍逻辑回归,以及如何利用Python来实现逻辑回归以及逻辑回归的实战模拟,另外我也会教大家如何利用网格搜索找到最优参数。干货满满!逻辑回归算法1概述分类技术是机器学习和数据挖掘应用中的重要组成部分。在数据科学中,绝大多数的问题属于分类问题。解决分类的算法也有很多种。如:KNN,使距

    2022年8月21日
    7
  • 免费流媒体服务器(AMS3.0 非AdobeMediaServer)

    免费流媒体服务器(AMS3.0 非AdobeMediaServer)自己从事流媒体近20载,从没有可用的流媒体服务器到现在服务器遍地开花.但尽管开源服务器众多,功能强大,但却没有可以直接拿来使用的.原因是配置安装困难,没有自己想要的接口,很难与现有系统对接.为解决这个问题,自己就做了一个简单的安装包,并且提供了众多的接口可以与WEB进行对接,这样配置管理大大简化,安装使用都非常简单.服务器单机下保守至少可以大码流3000并发吧,性能非常不错.你可以做集群.提供HTTPRTMP协议,支持HLS.详细可以看下面的使用说…

    2022年6月12日
    35
  • linux播放音乐,录音命令—–arecord,aplay

    linux播放音乐,录音命令—–arecord,aplay用了这么长时间的 Linux 系统 是不是还没有用 Linux 听过音乐 一般使用 Linux 系统的人都是纯属办公需要或者自己对软件开发玩的 很少是做日常生活电脑使用 在 linux 下也同样有播放音乐的软件 包括桌面式的音乐播放软件 这些桌面软件在 Linux 社区都能找到 现在要说的是 Linux 下怎么用命令去播放音乐 在 pc 机上或者根文件系统稍微正式一点的开发板上 都会安装有 alsa 软件 这是一个 aud

    2026年3月17日
    2
  • python2 手动安装更新pip

    python2 手动安装更新pip

    2021年10月22日
    112
  • 计组_浮点数表示/补码运算:定点数加减法/浮点数加减法(步骤+实例)「建议收藏」

    计组_浮点数表示/补码运算:定点数加减法/浮点数加减法(步骤+实例)「建议收藏」文章目录步骤问题实例对阶操作(基于补码)尾数求和(基于对阶后)规格化数相关知识左归操作右归操作回到本例:步骤设两个浮点数x=Sx⋅rjxy=Sy⋅rjy\begin{array}{l}x=S_{x}\cdotr^{j_{x}}\\y=S_{y}\cdotr^{j_{y}}\end{array}x=Sx​⋅rjx​y=Sy​⋅rjy​​(1)对阶,使两数的小数点位置对齐。(2)尾数求和,将对阶后的两尾数按定点加减运算规则求和(差)(3)规格化,为增加有效数字的位数,提高运

    2025年12月8日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号