前言

pytest配置文件能够改变pytest框架代码的运行规则。比如修改pytest收集用例的规则,添加命令行参数等等!下面我们来一一讲解常用的一些配置项

Help

通过命令pytest --help查看配置文件中可以添加的一些参数及选项,这些选项都是可以添加到pytest的配置文件的

[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 dire
console_output_style (string) console output: classic or with additional progr
usefixtures (args) list of default fixtures to be used with this project
python_files (args) glob-style file patterns for Python test module disco
python_classes (args) prefixes or glob names for Python test class discover
python_functions (args) prefixes or glob names for Python test function and m
disable_test_id_escaping_and_forfeit_all_rights_to_community_support (bool) di
xfail_strict (bool) default for the strict parameter of xfail markers whe
junit_suite_name (string) Test suite name for JUnit report
junit_logging (string) Write captured log messages to JUnit report: one of n
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.filterwar
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 "li
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
addopts (args) extra command line options
minversion (string) minimally required pytest version environment variables:
PYTEST_ADDOPTS extra command line options
PYTEST_PLUGINS comma-separated plugins to load during startup
PYTEST_DISABLE_PLUGIN_AUTOLOAD set to disable plugin auto-loading
PYTEST_DEBUG set to enable debug tracing of pytest's internals to see available markers type: pytest --markers
to see available fixtures type: pytest --fixtures
(shown according to specified file_or_dir or current dir if not specified; fixtures with leading '_' are only shown with the '-v' option

File

通常情况下我们会把配置文件放到我们的项目根目录下 命名为 pytest.ini, 项目在运行时会首先按照配置文件中设置的参数选项来运行,其次再遵守pytest的默认规则

使用

添加默认参数选项

我们都知道pytest可以在cmd中使用命令行运行脚本,通常是这样的 pytest -vqs 脚本, 我们通过这个命令来运行一下脚本,大概输出运行信息就是下面这种形式

D:\PytestAutoTestFrameWork\TestCases>pytest -vqs test_loginCase.py
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-4.4.1, py-1.6.0, pluggy-0.9.0
rootdir: D:\PytestAutoTestFrameWork, inifile: pytest.ini
plugins: rerunfailures-7.0, metadata-1.8.0, html-1.20.0, allure-pytest-2.6.2
collected 4 items test_loginCase.py ------------open browser------------
-------staring login-------
info: string upload url "https://mail.126.com"
info:switching to iframe "//div[@id="loginDiv"]/iframe"
info:clearing value
[Info:Starting find the element "//input[@name="email"]" by "xpath"!]
info:input "linuxxiaochao"
[Info:Starting find the element "//input[@name="email"]" by "xpath"!]
info:clearing value
[Info:Starting find the element "//input[@name="password"]" by "xpath"!]
info:input "xiaochao11520"
[Info:Starting find the element "//input[@name="password"]" by "xpath"!]
info:click "//a[@id="dologin"]"
info:switch back to default iframe
---------end login---------

现在们在我们的项目根目录下新建pytest.ini文件并输入下面的选项

[pytest]
addopts=-vqs

我们再次运行脚本仅通过命令pytest即可,这时候一会发现输出信息和上面一摸一样,这就是配置文件的作用

添加用例路径

[pytest]
testpaths=./TestCases

通过这样一项设置,我们可以把我们用例所在的目录添加到配置文件,这样我们在运行用例的时候,pytest会直接在配置文件所在的目录搜索用例

指定pytest的版本

[pytest]
minversion = 3.0

指定pytest忽略哪些搜索目录

[pytest]
norecursedirs = .* venv src *.egg dist build

标红部分为系统默认不会搜索的路径,前面是用户自定义的路径,注:当自定义时最好把系统默认的添加到后面

修改pytest收集用例的规则-测试类

pytest默认会搜索以test_开头/以_test结尾的文件,以Test开头的类,且以test_开头的测试函数为测试用例

我们希望pytest能够搜索以Test*开头,*Test结尾或者*Suite开头或结尾的类名字,我们需要添加这样的选项

[pytest]
python_classes = *Test Test* *Suite

修改pytest收集用例规则-修改测试模块

我们希望pytest可以搜索以check_*开头的文件

[pytest]
python_files=test_* *_test check_*

修改pytest收集用例规则-修改测试方法

我们希望可以搜索以check_* 开头的测试函数为测试用例

[pytest]
python_functions = test_* *_test check_*

添加生成报告选项

[pytest]
addopts = -v --rerun 1 --html=report.html --self-contained-html

添加mark标记

通常我们在编写测试用例的时候,会在用例上回添加一些标记来记录哪些用例需要运行,哪些需要跳过,哪些需要标记为failed等等,那么难免我们记不住这些标记,我们可以通过配置文件记录这些标记

[pytest]
markers=
loginTest: Run login test cases
contactTest: Run add contact test cases
sendMailTest: Run send mail test cases

我们添加完这些标记之后,可以通过 pytest --markers 查看我们自定义的mark

D:\PytestAutoTestFrameWork\TestCases>pytest --markers
@pytest.mark.loginTest: Run login test cases @pytest.mark.contactTest: Run add contact test cases @pytest.mark.sendMailTest: Run send mail test cases @pytest.mark.flaky(reruns=1, reruns_delay=0): mark test to re-run up to 'reruns' times. Add a delay of 'reruns_delay' seconds between re-runs. @pytest.mark.filterwarnings(warning): add a warning filter to the given test. see https://docs.pytest.org/en/latest/warnings.html#pytest-mark-filterwarnings @pytest.mark.skip(reason=None): skip the given test function with an optional reason. Example: skip(reason="no way of currently testing this") skips the test.

最后

pytest看的差不多了,其实小项目也写完了,一直没时间整理,也没时间写到博客上来,快51了,争取这几天发出来!

pytest进阶之配置文件的更多相关文章

  1. pytest十三:配置文件 pytest.ini

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

  2. pytest进阶

    参考文章 使用 pytest pytest 这个 库是一个第三方库,严格来说,它的设计思路不属于 xUnit 系列.但它使用起来比较方便,同时他又兼容 unittest 的用例:用 unittest ...

  3. Pytest进阶使用

    fixture 特点: 命令灵活:对于setup,teardown可以省略 数据共享:在conftest.py配置里写方法可以实现数据共享,不需要import导入,可以跨文件共享 scope的层次及神 ...

  4. pytest进阶之html测试报告

    前言 Pytest系列已经写了几篇文章了,也不知道对多少人有帮助,总之对于我自己来说该掌握的都已经掌握了,那么今天我们再来说说pytest如何生成一个完整的html测试报告,让你在吹牛逼的路上再多一份 ...

  5. pytest进阶之xunit fixture

    前言 今天我们再说一下pytest框架和unittest框架相同的fixture的使用, 了解unittest的同学应该知道我们在初始化环境和销毁工作时,unittest使用的是setUp,tearD ...

  6. pytest进阶之conftest.py

    前言 前面几篇随笔基本上已经了解了pytest 命令使用,收集用例,finxture使用及作用范围,今天简单介绍一下conftest.py文件的作用和实际项目中如是使用此文件! 实例场景 首先们思考这 ...

  7. pytest进阶之fixture

    前言 学pytest就不得不说fixture,fixture是pytest的精髓所在,就像unittest中的setup和teardown一样,如果不学fixture那么使用pytest和使用unit ...

  8. Pytest - 进阶功能fixture

    1. 概述 Pytest的fixture功能灵活好用,支持参数设置,便于进行多用例测试,简单便捷,颇有pythonic.如果要深入学习pytest,必学fixture. fixture函数的作用: 完 ...

  9. Pytest进阶之参数化

    前言 unittest单元测试框架使用DDT进行数据驱动测试,那么身为功能更加强大且更加灵活的Pytest框架怎么可能没有数据驱动的概念呢?其实Pytest是使用@pytest.mark.parame ...

随机推荐

  1. Django单元测试简明实践

    1.准备数据模式,Django空库测试需要所有相关数据模式必须在Django中定义,说白了,model不能有managed=Fasle,为了解决这个问题,你必须得有一个managed全部为True的S ...

  2. notepad++中双击选中字符串高亮颜色设置

    notepad++ 中最好用的功能就是双击选中,本文档中所有相同的内容高亮 不过有个问题就是当文档特别大,而且注释比较多的时候,我选中的内容高亮为绿色不太好找,那怎么设置呢? 设置--语言格式设置-- ...

  3. 安装ie时,报:此安装不支持您的操作系统的当前语言

    打开注册表(win的"运行"栏键入 regedit 再按 OK )的HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Nls/ ...

  4. python 正则表达式Re

    Python正则表达式指南这篇文章很好,推荐阅读. 本文则是简单记录下我自己学习Re的笔记, 环境是python3.5. 1.简单的Re语法 ^ 匹配字符串开始位置. $ 匹配字符串结束位置. \b ...

  5. SpringBoot19 集成SpringSecurity01 -> 环境搭建、SpringSecurity验证

    1 环境搭建 1.1 创建一个SpringBoot项目 项目脚手架 -> 点击前往 1.2 创建一个Restful接口 新建一个Controller类即可 package com.example ...

  6. Python_sqlite3

    import sqlite3 #导入模块 conn = sqlite3.connect('example.db') #连接数据库 c = conn.cursor() #创建表 c.execute('' ...

  7. 语音识别中的CTC算法的基本原理解释

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文作者:罗冬日 目前主流的语音识别都大致分为特征提取,声学模型,语音模型几个部分.目前结合神经网络的端到端的声学模型训练方法主要CTC和基 ...

  8. 如何使用 toml 配置 SpaceVim

    配置 SpaceVim 主要包括以下几个内容: 设置 SpaceVim 选项 启动/禁用模块 添加自定义插件 添加自定义按键映射以及插件配置 设置SpaceVim选项 原先,在 init.vim 文件 ...

  9. mysql为何不支持开窗函数?

    引用 在开窗函数出现之前存在着非常多用 SQL 语句非常难解决的问题,非常多都要通过复杂的相关子查询或者存储过程来完毕.为了解决这些问题,在2003年ISO SQL标准增加了开窗函数,开窗函数的使用使 ...

  10. 计算机网络相关:应用层协议(一):DNS

    DNS 1.概念  DNS是:  1)  一个有分层的DNS服务器实现的分布式数据库  2)一个使得主机能够查询分布式数据库的应用协议.  它运行在UDP之上,默认使用53号端口.  主要功能 是将主 ...