前言

面试题:pytest如何执行不是test开头的用例?如执行 xxx_*.py这种文件的用例。

pytest.ini 配置文件可以修改用例的匹配规则。

pytest命令行参数

cmd打开输入pytest -h 查看命令行参数找到 [pytest] ini-options

  • python_files (args) 匹配 python 用例文件, 如test_*.py、 *_test.py
  • python_classes (args) 匹配 class 类名称 如Test*.py
  • python_functions (args) 匹配函数和class里面方法 如test_*
[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
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
console_output_style (string) console output: "classic", or with additional pr
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
rsyncdirs (pathlist) list of (relative) paths to be rsynced for remote dis
rsyncignore (pathlist) list of (relative) glob-style paths to be ignored for
looponfailroots (pathlist) directories to check for changes

修改匹配规则

pytest 默认查找用例匹配规则

  • 测试文件以test_开头(以_test结尾也可以)
  • 测试类以Test开头,并且不能带有 init 方法
  • 测试函数以test_开头

如果我们想匹配以 xxx_*.py的文件,pytest.ini 文件放到项目的根目录。

在 pytest.ini 文件添加一项 python_files 即可

[pytest]

python_files =  xxx_*.py

使用案例

写一个 xxx_yoyo.py 的文件用例

#  xxx_yoyo.py
# 作者:上海-悠悠 QQ交流群:779429633
def test_1():
print("hello") def test_2():
print("world")

cmd 输入 pytest 执行,就可以匹配到了

D:\soft\demo>pytest
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-4.5.0, py-1.5.4, pluggy-0.13.1
rootdir: D:\soft\code\web_pytest_2020, inifile: pytest.ini
plugins: allure-pytest-2.8.6
collected 2 items xxx_yoyo.py hello
.world
. ========================== 2 passed in 0.13 seconds ===========================

匹配测试用例类和方法相关配置参考如下

[pytest]

python_files =  xxx_*.py *_xxx.py
python_classes = Test*
python_functions = test_*

多个匹配规则中间用空格隔开,其它配置参考这篇https://www.cnblogs.com/yoyoketang/p/9550648.html

pytest文档40-pytest.ini配置用例查找规则(面试题)的更多相关文章

  1. pytest文档41-参数化 ids 用例描述为中文时控制台输出unicode编码问题(pytest_collection_modifyitems)

    前言 使用 pytest.mark.parametrize 参数化的时候,加 ids 参数用例描述有中文时,在控制台输出会显示unicode编码,中文不能正常显示. 使用 pytest_collect ...

  2. pytest文档45-allure添加环境配置(environment)

    前言 在 allure 报告首页 ENVIRONMENT 显示 'There are no environment variables' 没有环境变量的配置信息. 环境变量配置可以添加报告相关的配置参 ...

  3. pytest文档26-运行上次失败用例(--lf 和 --ff)

    前言 "80%的bug集中在20%的模块,越是容易出现bug的模块,bug是越改越多"平常我们做手工测试的时候,比如用100个用例需要执行,其中10个用例失败了, 当开发修复完bu ...

  4. pytest文档44-allure.dynamic动态生成用例标题

    前言 pytest 结合 allure 描述用例的时候我们一般使用 @allure.title 和 @allure.description 描述测试用例的标题和详情. 在用例里面也可以动态更新标题和详 ...

  5. pytest文档7-pytest-html生成html报告

    前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...

  6. pytest文档3-pycharm运行pytest

    前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...

  7. 使用Docsify做文档网站的详细配置教程

    使用Docsify做文档网站的详细配置教程 作者:xhemj 没错,它叫Docsify. xhemj的文档中心就是用这个写的 开源地址:https://github.com/docsifyjs/doc ...

  8. pytest文档18-配置文件pytest.ini

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

  9. pytest文档43-元数据使用(pytest-metadata)

    前言 什么是元数据?元数据是关于数据的描述,存储着关于数据的信息,为人们更方便地检索信息提供了帮助. pytest 框架里面的元数据可以使用 pytest-metadata 插件实现.文档地址http ...

随机推荐

  1. sublime3 激活

    起因 这段时间sublime一直抽风,每次打开都提示让我更新. 身为强迫症的我当然不能忍! 方法 关闭自动更新 点击菜单栏"Preferences"=> "Sett ...

  2. 营销经验总结:如何才能提升h5游戏代入感?

    HTML5游戏拥有即点即玩,无需下载,并具备传播性广的特点,这就使得商家看到了无限商机,如何让产品更加深入人心,是游戏推广最为重要的环节.优秀的代入感才是游戏产品宣传的关键,那么有哪些要素的支撑才能确 ...

  3. Java实现随机生成由字母、数字组合的N位随机数

    通过Math.random函数生成所需的字符所有序列,通过判断当前字符是否属于大小写.数字,符合者加入数组内,直到数组存储N位为止,最后把当前数组转为字符串返回调用处. /** * 随机生成由数字.字 ...

  4. Oracle Rman备份恢复和管理

    参考资料: Oracle之Rman入门指南 一步一步学Rman Rman简介 Rman-Recover manager恢复管理工具. Oracle集成了很多环境的一个数据库备份和恢复的工具. Rman ...

  5. matplotlib | Python强大的作图工具,让你从此驾驭图表(二)

    今天是数据处理专题的第10篇文章,我们继续来聊聊matplot这个工具库. 在上周的文章当中我们介绍了matplot的基本用法,以及展示了一些简单的例子,让大家直观地了解这个工具包.我们可以简单地将它 ...

  6. 知识全聚集 .Net Core 技术突破 | 如何实现一个模块化方案一

    简介 模块化的介绍一共2篇 这一篇我们实现一个功能非常简单的StartupModules模块化. 第二篇我们来实现一个ABP的模块化效果. 思考 其实来简单想一下模块化的实验思路,写个接口=>模 ...

  7. 基本的PID算法整理(水缸的例子有问题!!)

    一,先谈关于水缸漏水的问题 谈到PID原理入门的时候,大家经常会举的一个例子就是水缸漏水的例子.这里把一个解释水缸漏水的帖子放在这里:https://blog.csdn.net/qq_41736609 ...

  8. Java Web学习(九)网络协议详解

    一.基本概念 概念:协议是网络中计算机或设备之间进行通信的一系列规则的集合. 协议栈/族:在网络中为了完成通信而使用到的多层上的各种协议按照层次顺序的组合. 作用:建立对等层之间的虚拟通信.实现层次之 ...

  9. MySQL 5.7主从复制

    简介 主从复制是利用MySQL复制机制将数据复制到另外一台或多台MySQL服务器上,被复制的服务器称为主服务器,复制的服务器称为从服务器.一般是一主多从.主从复制的好处主要是数据备份.负载均衡(读写分 ...

  10. 一起构建Python生长土壤

    环境 环境安装 1. 解释器 Python https://www.python.org/ 2. IDE Pycharm https://www.jetbrains.com/pycharm/downl ...