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

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


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/166346.html原文链接:https://javaforall.net

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


相关推荐

  • 学习PetShop3.0(4)购物车

    学习PetShop3.0(4)购物车终于到购物车了,在看这个之前应该已经明白了第三篇的那个模型,这样购物车基本也就明白了。来看一下ShoppingCart.aspx这个页。当你看好了一个宠物,比如可爱的GoldenRetriever,嘿嘿,那就点addtocart按钮,这时就会跳到ShoppingCart.aspx,url里带了这个宠物的id号,根据该id号程序将该宠物放到cart里面。然后你可以再去挑别的宠物,比如一只猫(……

    2022年10月16日
  • 美团Java面试一轮游,太激烈了,问啥啥不会,我该怎么办?

    美团Java面试一轮游,太激烈了,问啥啥不会,我该怎么办?一面1、自我介绍答:自我介绍是面试中唯一的自己主动介绍自己的环节,一定要好好把握好,你数据结构学的号可以手撕一个红黑树你就说我数据结构掌握地很好,反正就是要把自己的优势凸显出来,比如自己对于java的知识较熟悉,我介绍完自己的本科经历以后,我就说我是保送到本校继续读研究生,然后最末尾会加上自己熟悉java,然后面试官就会问java的一些东西;2、项目介绍及其亮点答:使劲吹…3、java的8种数据类型有哪些?答:感觉这个问题被问烂了,int,short,long,float,dou

    2022年7月7日
  • struts2使用验证文件实现校验

    struts2使用验证文件实现校验

    2021年6月16日
  • 如何利用matlab做BP神经网络分析(包括利用matlab神经网络工具箱)「建议收藏」

    利用MATLAB进行BP神经网络的预测(含有神经网络工具箱)最近一段时间在研究如何利用预测其销量个数,在网上搜索了一下,发现了很多模型来预测,比如利用回归模型、时间序列模型,GM(1,1)模型,可是自己在结合实际的工作内容,发现这几种模型预测的精度不是很高,于是再在网上进行搜索,发现神经网络模型可以来预测,并且有很多是结合时间序列或者SVM(支持向量机)等组合模型来进…

    2022年4月11日
  • 记录一次无加密门禁卡手机nfc复制过程

    记录一次无加密门禁卡手机nfc复制过程虽然小米说它的手机可以复制门禁卡,但是加密卡它不让复制,就连完全没有加密的门禁卡也无法复制,因为它不让你写入0扇区,你就没有对应的id号,那门怎么会开呢?必须要吐槽一下当然网上也有改它模拟的ID号方法,但那样可能会改变系统文件夹,会对以后带来风险原料:安卓软件taginfo和mifare经典工具,网上很好下载的如果检测到是无加密卡,就可以去淘宝买cuid卡了,…

    2022年5月27日
  • Struts2 拦截器的Interceptor接口与AbstractInterceptor抽象类

    Struts2 拦截器的Interceptor接口与AbstractInterceptor抽象类在学习Struts2框架的时候,遇到了拦截器,一开始拦截器都应该之间或者间接去实现Interceptor接口的,但是后来看到了AbstractInterceptor这个抽象类。之前学过的JAVA知识有点忘记了,所以特意查了一下关于抽象类和接口之间的关系。我们不想使用Interceptor接口去实现拦截器对象,是因为这个接口中有3个方法,但是我们在实际上我们主要用到的就是只有intercept()这

    2022年5月15日

发表回复

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

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