pytest文档40-pytest.ini配置用例查找规则(面试题)

摘要:
pytest。ini配置文件可以修改用例的匹配规则。
前言

面试题: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:softdemo>pytest
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-4.5.0, py-1.5.4, pluggy-0.13.1
rootdir: D:softcodeweb_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配置用例查找规则(面试题)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇MFC编程入门之二十一(常用控件:编辑框Edit Control)Oracle12.2c统一审计(unified auditing)六问下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python selenium 延时的几种方法

解决网页加载缓慢的几种方法: 一、显性等待WebDriverWait,配合该类的until()和until_not()方法,就能够根据判断条件而进行灵活地等待 1 from selenium import webdriver 2 from selenium.webdriver.support.wait import WebDriverWait 3 4...

python中多进程multiprocessing、多线程threading、线程池threadpool

浅显点理解:进程就是一个程序,里面的线程就是用来干活的,,,进程大,线程小 一、多线程threading 简单的单线程和多线程运行:一个参数时,后面要加逗号 步骤:for循环,相当于多个线程——t=threading.Thread(target=函数名,args=(参数,))——t.start()——while threading.active_coun...

springmvc 整合 netty-socketio

1 maven <dependency><groupId>com.corundumstudio.socketio</groupId><artifactId>netty-socketio</artifactId><version>1.7.12</version></de...

http协议的POST传数据

PostRequest使用StreamWriter对象写入请求流,不需要使用HttpUtility.UrlEncode显示转码,而下面的需要显示转码,还需要将参数转为字节码 蛋疼…………。 public static string PostRequest(string url, string postData) { HttpWebRequest httpW...

python “ cannot import name OrderedDict”

本地测试一切ok,结果放到亚马逊云上去,运行出现错误:“ cannot import name OrderedDict”,于是找到错误根源,在于我在某处用到了OrderedDict, 在头部“from collections import OrderedDict”,  google有人指出“from ## import ##”不支持二次导入,如果包含该语句...

C#使用Process类杀死进程,执行命令等

c#之process类相关整理 一、根据进程名获取进程的用户名? 需要添加对 System.Management.dll 的引用 using System.Diagnostics; using System.Management; static void Main(string[] args) { foreach (Process p in Proc...