怎么新建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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • tiptop开发webservice详细步骤

    tiptop开发webservice详细步骤4gl,webservice,tiptop,t100

    2022年7月12日
    20
  • Java新手、小白入门。多敲练习代码!!!

    Java新手、小白入门。多敲练习代码!!!如果你喜欢Java,但是想学不会!我建议你没事的时候敲敲这些代码,希望对你有用!publicclassDemo{ publicstaticvoidmain(String[]args){ System.out.print(“你好\n世界”); System.out.println(“你好\tJava”); System.out.println(“1.电脑要求相对干…

    2022年6月21日
    31
  • 电赛练习之旋转倒立摆

    电赛练习之旋转倒立摆2019年电赛已经结束,虽然结果不能令人满意,但闲下来,还是总结一下电赛学到的东西与失败的地方。这一次先来谈一下一阶旋转倒立摆。一、题目分析:拿到一道题目,其实最应该做的事情是分析题目,因为我们往往可以发现某些发挥题是在基础题的基础上进行的,但是,可能某些发挥题需要在基础题的基础上修改结构,我们也可以发现,题目中的某些问题具有相似性,当我们合并同类项的时候,可以把题目的要求变得简单。一下,我粘…

    2022年8月18日
    6
  • “Word在试图打开文件时遇到错误”的解决方法[通俗易懂]

    “Word在试图打开文件时遇到错误”的解决方法[通俗易懂] 大家都应该知道“.DOCX”格式只有Word2007或以上版本才可以打开,Word2003是无法打开的!正好我电脑上03和07都有,所以就答应了。接收文件打开后既然提示“Word在试图打开文件时遇到错误。请尝试下列方法”(如下图)。  还好,本人使用Office办公软件已经很多年了,各方面问题都遇见过,这点小难题难不到我的,三下两下就被我搞定了。相信遇到“W…

    2022年5月1日
    88
  • 图的两种遍历方式

    图的两种遍历方式遍历是指从某个节点出发,按照一定的的搜索路线,依次访问对数据结构中的全部节点,且每个节点仅访问一次。在二叉树基础中,介绍了对于树的遍历。树的遍历是指从根节点出发,按照一定的访问规则,依次访问树的每个节点信息。树的遍历过程,根据访问规则的不同主要分为四种遍历方式:(1)先序遍历(2)中序遍历(3)后序遍历(4)层次遍历类似的,图的遍历是指,从给定图中任意指定的顶点(称为初始点…

    2022年6月14日
    32
  • Linux文件系统类型介绍[通俗易懂]

    Linux文件系统类型介绍[通俗易懂]Linux把设备都当作文件一样来进行操作,这样就大大方便了用户的使用(在后面的Linux编程中可以更为明显地看出)。在Linux下与设备相关的文件一般都在/dev目录下,它包括两种,一种是块设备文件,另一种是字符设备文件。这就涉及到文件系统,以下介绍以下Linux文件系统。 1.ext2和ext3 ext3是现在Linux(包括RedHat,Mandrake下…

    2025年12月12日
    4

发表回复

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

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