📝 面试求职: 「面试试题小程序」 ,内容涵盖 测试基础、Linux操作系统、MySQL数据库、Web功能测试、接口测试、APPium移动端测试、Python知识、Selenium自动化测试相关、性能测试、性能测试、计算机网络知识、Jmeter、HR面试,命中率杠杠的。(大家刷起来…)
📝 职场经验干货:
5. pytest 常用配置
pytest除了通过命令行参数来控制运行行为时,也可以通过pytest.ini文件来改变其运行规则。
5.1 pytest.ini 配置
通过pytest --help 可以查看配置文件中可以添加的参数和相应的选项,如下所示:
[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg|pyproject.toml file found:
markers (linelist): Register new markers fortestfunctions
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 on the command
line
filterwarnings (linelist):
Each line specifies a pattern for warnings.filterwarnings. Processed after
-W/--pythonwarnings.
consider_namespace_packages (bool):
Consider namespace packages when resolving module names during import
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 testfunction 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" | "progress-even-when-capture-no" (forces progress even when
capture=no)
verbosity_test_cases (string):
Specify a verbosity level fortestcase execution, overriding the main level. Higher
levels will provide more detailed information about each testcase executed.
xfail_strict (bool): Default for the strict parameter of xfail markers when not given explicitly (default:
False)
tmp_path_retention_count (string):
How many sessions should we keep the `tmp_path` directories, according to
`tmp_path_retention_policy`.
tmp_path_retention_policy (string):
Controls which directories created by the `tmp_path` fixture are kept around, based on
test outcome. (all/failed/none)
enable_assertion_pass_hook (bool):
Enables the pytest_assertion_pass hook. Make sure to delete any previously generated pyc
cache files.
verbosity_assertions (string):
Specify a verbosity level for assertions, overriding the main level. Higher levels will
provide more detailed explanation when an assertion fails.
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
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_mode (string):
Default value for --log-file-mode
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
pythonpath (paths): Add paths to sys.path
faulthandler_timeout (string):
Dump the traceback of all threads if a test takes more than TIMEOUT seconds to finish
addopts (args): Extra command line options
minversion (string): Minimally required pytest version
required_plugins (args):
Plugins that must be present for pytest to run
5.2 @pytest.marker 标记
在pytest.ini中设置markers后,在测试方法上面即可以使用这些markers。配置示例如下所示:
[pytest]
markers=Smoke: Run the smoke test function
P1: Highest Prority test function
P2: Medium Prority test function
P3: Lower Prority test function
Regression: Regression test function
在命令行中运行以下命令,可以查看气配置的markers
C:\Users\Surpass\Documents\PyCharmProjects\Pytest\03-codes\Content-05>pytest --markers
@pytest.mark.smoke: Run the smoke test function
@pytest.mark.P1: Highest Prority test function
@pytest.mark.P2: Medium Prority test function
@pytest.mark.P3: Lower Prority test function
@pytest.mark.Regression: Regression test function
测试代码如下所示:
# @IDE: PyCharm
# @Project: PyCharmProjects
# @File: test_pytest_markers_01.py
# @Time 2025-03-14 22:29
# @Author: Surpass Lee
# @E-mail: surpassmebyme@gmail.com
import pytest
@pytest.mark.Smoke
def test_pytest_ini_01():
print("\ntest pytest markers")
@pytest.mark.P1
def test_pytest_ini_02():
print("\ntest pytest markers")
@pytest.mark.P1
@pytest.mark.Regression
def test_pytest_ini_03():
print("\ntest pytest markers")
运行以下命令后结果:
C:\Users\Surpass\Documents\PyCharmProjects\Pytest\03-codes\Content-05>pytest -s -m "P1" --collect-only
collected 3 items / 1 deselected / 2 selected
<Dir Content-05>
<Module test_pytest_markers_01.py>
<Function test_pytest_ini_02>
<Function test_pytest_ini_03>
最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】