怎么新建pytest的ini文件_pytest.ini配置

怎么新建pytest的ini文件_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/166266.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【微信公众平台开发】借用微信内置图片浏览功能

    【微信公众平台开发】借用微信内置图片浏览功能

    2021年11月24日
    40
  • 【参赛作品29】基于openGauss数据库设计人力资源管理系统实验「建议收藏」

    【参赛作品29】基于openGauss数据库设计人力资源管理系统实验「建议收藏」本文作者:瓜西西本文主要面向openGauss数据库初学者,帮助初学者完成一些简单的数据库管理以及GUI,设计一个简单的人力资源管理系统。本文只包含部分代码,读者需要结合自己的数据库弹性公网、数据库用户及其密码等自身信息做出相应的修改。一、实验环境使用程序:putty.exe;IntelliJIDEA2021.1.1;apache-tomcat-9.0.46服务器名称:ecs-d8b3弹性公网:121.36.79.196端口号:26000表空间名:human_resource_

    2022年5月24日
    37
  • 长春python编程培训学校

    长春python编程培训学校近年来,教育部频频下发相关政策,强调编程教育的重要性,探究编程教育发展的新方式。从政策出台的趋势看,编程教育列入基础教育的趋势越来越大。在今年(2020年),教育部及各个省市又都下发了什么政策我们正好借着这个机会来盘点一下2020年各地编程教育政策汇总2020年2月教育部教育部公布了《2019年度普通高等学校本科专业备案和审批结果》,确定通过人工智能专业审批的高校达到180所。2020年4月四川四川省教育厅发布《关于加强初中学业水平考试命…

    2022年5月16日
    43
  • JAVA数组的定义及用法

    JAVA数组的定义及用法

    2021年12月5日
    51
  • 现在建网站你会选择自己建站还是在线建站平台?

    现在建网站你会选择自己建站还是在线建站平台?一 什么是网站 网上关于网站的定义很多 在此就不想重复了 简而言之网站就是创建者使用技术手段搭建 从而让访客可以通过域名访问并进行互动的互联网页面组合 通常网站由四个部分组成 1 域名 也就是我们通常称为网址的东东 比如 淘宝 taobao com 百度 baidu com 对于普通用户可以简单理解为域名既网址 域名是网站的访问入口 没有域名的话普通用户无法访问网站就好了 所以域名是网站的重要组成部分 2 程序 这里特指建站程序 网站虽然每一个页面都是由源代码组成的 但即使是程

    2025年6月26日
    3
  • LabVIEW图像分割算法(基础篇—6)

    LabVIEW图像分割算法(基础篇—6)图像分割是简化机器视觉算法的有效手段之一。它将图像分成一些有意义的区域,以便特征提取过程可基于这些区域提取目标的特征。

    2022年5月20日
    52

发表回复

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

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