iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >如何在pytest中使用pytest.ini配置文件
  • 428
分享到

如何在pytest中使用pytest.ini配置文件

2023-06-14 13:06:16 428人浏览 八月长安
摘要

这篇文章将为大家详细讲解有关如何在pytest中使用pytest.ini配置文件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。pytest配置文件可以改变pytest的运行方式,它是一个固定

这篇文章将为大家详细讲解有关如何在pytest中使用pytest.ini配置文件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

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: xinlangxfail_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 item02断言异常.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]# markmarkers =    weibo: this is weibo page    toutiao: toutiao    xinlang: xinlangxfail_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配置文件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

--结束END--

本文标题: 如何在pytest中使用pytest.ini配置文件

本文链接: https://www.lsjlt.com/news/271251.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 如何在pytest中使用pytest.ini配置文件
    这篇文章将为大家详细讲解有关如何在pytest中使用pytest.ini配置文件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。pytest配置文件可以改变pytest的运行方式,它是一个固定...
    99+
    2023-06-14
  • pytest中配置文件pytest.ini如何使用
    本篇内容介绍了“pytest中配置文件pytest.ini如何使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、pytest.ini说明...
    99+
    2023-06-30
  • pytest中配置文件pytest.ini使用
    目录一、pytest.ini说明二、pytest.ini设置1.addopts–设置自定义执行参数2. testpaths–设置执行路径3. markers&...
    99+
    2024-04-02
  • pytest配置文件pytest.ini的详细使用
    前言 pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行 非test文件 pytest里面有些文件是...
    99+
    2024-04-02
  • pytest配置文件pytest.ini的具体使用
    目录前言pytest.ini的内容构成配置项markers配置项testpaths配置项addopts前言 说到配置,大家可能想到的是不经常更改的内容,比如Django里的setti...
    99+
    2024-04-02
  • 如何在pytest中使用conftest.py文件
    这篇文章将为大家详细讲解有关如何在pytest中使用conftest.py文件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。一、conftest.py的特点可以跨.py文件调用,有多个.py...
    99+
    2023-06-08
  • 如何在yml配置文件中使用中文注解
    目录yml配置文件中使用中文注解问题根本原因解决yml配置文件简单语法及小坑yml文件使用方法yml配置文件中使用中文注解 问题 我们在yml中写注解一般是这样的 #xxxx 当我们...
    99+
    2022-11-13
    yml配置文件 中文注解 yml使用中文注解
  • linux中如何使用vim配置文件
    这篇文章将为大家详细讲解有关linux中如何使用vim配置文件,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。此vim配置有以下优点:1,按F5可以直接编译并执行C、C++、java代码以及执行shell脚...
    99+
    2023-06-13
  • java中properties配置文件如何使用
    在Java中,可以使用Properties类来读取和写入配置文件,配置文件通常是以.properties文件格式保存的。1. 读取配...
    99+
    2023-10-18
    java
  • 如何在nginx配置文件中使用环境变量
    这期内容当中小编将会给大家带来有关如何在nginx配置文件中使用环境变量,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。前言Nginx是一款面向性能设计的HTTP服务器,相较于Apache、lighttpd...
    99+
    2023-06-15
  • 如何在C#中读取配置文件
    如何在C#中读取配置文件,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。C#读取配置文件1.了解配置文件概述:应 用程序配置文件是标准的 XML 文件,XML 标...
    99+
    2023-06-18
  • SpringBoot如何使用applicationContext.xml配置文件
    目录使用applicationContext.xml配置文件applicationContext 加载配置文件案例多文件的加载方法使用applicationContext.xml配置...
    99+
    2024-04-02
  • 如何使用ASP.NET Core 配置文件
    目录前言Json配置文件的使用RedisHelper类XML配置文件的使用前言 在ASP.NET ,我们使用XML格式的.Config文件来作为配置文件,而在ASP.NET Core...
    99+
    2024-04-02
  • php如何在配置文件中设置时区
    本篇内容主要讲解“php如何在配置文件中设置时区”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“php如何在配置文件中设置时区”吧!php配置文件设置时区的方法:首先找到并打开配置文件“php.i...
    99+
    2023-06-20
  • Apache和PHP:如何在Linux中配置文件?
    Apache和PHP是众所周知的开源软件,它们在Web服务器和动态Web应用程序方面扮演着重要角色。在Linux中,Apache和PHP的安装和配置相对容易,但有时会出现一些问题。本文将为您介绍如何在Linux中配置Apache和PHP。 ...
    99+
    2023-06-30
    apache 文件 linux
  • git配置文件在哪?如何进行配置?
    Git是目前最流行的版本控制系统之一。在使用Git时,我们需要对其进行一些配置,如设置用户名和邮箱、添加忽略文件等。但是,新手很容易会遇到一个问题:Git配置文件在哪里?本文将为你介绍Git配置文件的位置以及如何进行Git配置。一、Git配...
    99+
    2023-10-22
  • 如何使用@PropertySource读取配置文件
    本篇内容主要讲解“如何使用@PropertySource读取配置文件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用@PropertySource读取配置文件”吧!@PropertySou...
    99+
    2023-06-29
  • 使用Spring Boot如何配置maven文件
    使用Spring Boot如何配置maven文件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。1.配置maven文件pom.xml<&#63;xml versio...
    99+
    2023-05-31
    springboot maven
  • 如何使用Linux下Crontab配置文件
    本篇内容介绍了“如何使用Linux下Crontab配置文件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!【全局(系统)配置文件】代码如下:/...
    99+
    2023-06-13
  • Nagios pnp中如何配置Nagios配置文件
    这篇文章主要为大家展示了“Nagios pnp中如何配置Nagios配置文件”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Nagios pnp中如何配置Nagios配置文件”这篇文章吧。1.Na...
    99+
    2023-06-16
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作