前言
面試題: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