pytest.ini
是 pytest 框架的配置文件,用于自定义 pytest 的行为和设置。它通常位于项目的根目录下。
[pytest]
# 配置项 = 值[pytest]
# 设置测试文件命名模式
python_files = test_*.py *_test.py
# 设置测试类命名模式
python_classes = Test* *Test
# 设置测试函数命名模式
python_functions = test_* *_test
# 设置测试搜索路径
testpaths = tests unit_tests case_tests
# 忽略特定目录
norecursedirs = .* build dist node_modules
# 设置基础URL(用于API测试)
base_url = https://www.baidu.com
# 添加默认命令行参数
addopts = -v --tb=short -m "not slow"
# 注册自定义标记
markers =
slow: 标记运行缓慢的测试
smoke: 冒烟测试
ui: UI相关测试
integration: 集成测试
regression: 回归测试[pytest]
# 控制台日志设置
log_cli = true
log_cli_level = INFO
log_cli_format = %(asctime)s [%(levelname)s] %(name)s: %(message)s
log_cli_date_format = %H:%M:%S
# 文件日志设置
log_file = logs/pytest.log
log_file_level = DEBUG
log_file_format = %(asctime)s [%(levelname)s] %(name)s: %(message)s
log_file_date_format = %Y-%m-%d %H:%M:%S[pytest]
# 设置最大失败次数后停止
maxfail = 5
# 设置测试超时时间(需要pytest-timeout插件)
timeout = 30
# 设置测试执行顺序(需要pytest-random-order插件)
random_order_enabled = true
random_order_seed = 42[pytest]
# HTML报告设置(需要pytest-html插件)
htmlpath = reports/pytest_report.html[pytest]
# 指定要加载的插件
plugins = my_custom_plugin
# Allure报告配置
allure_report_dir = reports/allure
allure_features = feature1, feature2[pytest]
# 设置环境变量
env =
API_KEY=test_key
DB_URL=sqlite:///:memory:pytest --versionpytest.ini 纳入版本控制系统pytest.iniPytestUnknownMarkWarning: Unknown pytest.mark.slow解决方案:在 pytest.ini 中定义标记
[pytest]
markers =
slow: marks tests as slow使用绝对路径或相对于根目录的路径:
log_file = logs/pytest.log
htmlpath = reports/pytest_report.html在配置文件中设置的环境变量可能会被系统环境变量覆盖,优先使用系统环境变量。
可根据项目需求灵活调整pytest配置项
#Pytest #Python #Pytest.ini配置