python之pytest相关常用插件及pytest.ini文件配置

1、pytest日常测试时常用插件及命令

具体命令使用方法及规则请跳转至:http://t.csdn.cn/Wsc7R

pytest-xdist 使用多线程执行用例 使用:-n 3 或者 “-n=2"

pytest-rerunfailures 用例失败重跑 使用:--reruns 3 或者 ”--reruns=3"

pytest-html 生成html报告 使用:--html=report.html

pytest-ordering 改变测试用例执行顺序,使用:@pytest.mark.run(order=1)

allure-pytest 生成allure级别的测试报告(需要下载allure文件) 使用:--alluredir=report & os.system(allure.bat serve report)

2、pytest.ini文件配置

pytest.ini 文件是pytest运行的主配置文件,可以改变pytest运行的默认行为,一般放在根目录下,格式如下:

[pytest]
addopts = -vs
testpaths = ./test_demo3.py

python_files = test_* check_*
python_classes = Test* Check*
python_functions = test_* check_*

markers =
    smoke:maoyan

1、addopts:默认执行命令项 可以加一些命令,如同:pytest.main(["-vs","-n=2"])

  1. -v:未加前只打印模块名,加v后打印类名、模块名、方法名,显示更详细的信息
  2. -s:表示输出调试信息,用于显示测试函数中print()打印的信息
  3. -vs:这两个参数可以一起使用
  4. -q:表示只显示整体测试结果
  5. -n:支持多线程或者分布式运行测试用例(需安装:pytest-xdist插件)
  6. --reruns :用例失败重跑(需安装:pytest-rerunfailures插件)
  7. -m ”标记“ 说明:执行特定的测试用例,如 -m smoke
addopts = -vs -n 3 --reruns 2 -m smoke

2、testpaths:pytest执行的文件路径,例如:./testcase

testpaths = ./testcase/test_demo3.py

3、pytest执行时默认找文件、类、方法

     pytest 默认的用例收集规则:

  1. 测试模块必须以 test_ 开头或以 _test 结尾;
  2. 测试类必须以 Test 开头,且不能有 __init__() ;
  3. 测试方法必须以 test_ 开头;

上面的默认规则可以修改或者增加收集规则:

pytest_fails:执行文件的搜索的模块文件名

python_classes:执行类的类名

python_functions:执行方法的方法名

python_files = test_* check_*
python_classes = Test* Check*
python_functions = test_* check_*

这里增加了以 check开头的文件、类、方法,若有check开头也会被执行

4、markers:标记

markers =
    smoke:maoyan

此时配置了smoke标记,当然标记可以添加很多

使用时在命令后加命令:-m smoke

当用例上面有 @pytest.mark.smoke 的装饰器就表示会执行此用例,其他用例不执行,例如:

@pytest.mark.smoke
def test_demo():
    pass

5、当pytest.ini文件配置完成后,在文件中用 pytest.main()时就不需要加参数了,执行时会自动找pytest.ini文件里的配置规则去执行

<think>我们正在讨论pytest框架中的pytest.ini配置文件pytest.inipytest的主配置文件,用于设置pytest的默认行为。它可以放在项目的根目录下,当运行pytest时,会自动读取该文件中的配置。 根据引用[1]和引用[2]中提到的pytest相关插件和用法,我们可以推断pytest.ini中通常配置一些全局的选项,比如指定测试文件的位置、添加命令行默认选项、注册插件等。 常见的配置项包括: 1. [pytest]:这是配置文件的节,所有的pytest配置都写在这个节下。 2. addopts:添加默认的命令行参数,例如:`addopts = -v -s` 表示默认添加详细输出和捕获输出。 3. testpaths:指定测试文件的搜索路径,例如:`testpaths = tests` 表示在tests目录下查找测试。 4. python_files:指定测试文件名的模式,例如:`python_files = test_*.py` 表示以test_开头的.py文件。 5. python_classes:指定测试类的模式,例如:`python_classes = Test*` 表示以Test开头的类。 6. python_functions:指定测试函数的模式,例如:`python_functions = test_*` 表示以test_开头的函数。 7. markers:用于注册自定义标记,避免运行时出现警告。例如: markers = slow: marks tests as slow (deselect with '-m "not slow"') smoke: smoke test 8. norecursedirs:指定pytest不递归进入的目录,例如:`norecursedirs = .* build dist` 避免进入一些非测试目录。 9. 配置插件:例如,如果要使用pytest-xdist插件,可以在配置文件中指定,但通常不需要在ini中特别配置,只要安装了就会生效。不过,有时可以配置xdist的一些参数。 另外,引用[3]和引用[4]提到了生成测试报告和持续集成,所以pytest.ini中也可能配置与测试报告相关的选项,比如allure的报告目录等。 下面是一个典型的pytest.ini文件示例: ```ini [pytest] addopts = -v -s --tb=short testpaths = tests python_files = test_*.py python_classes = Test* python_functions = test_* markers = slow: marks tests as slow smoke: smoke test norecursedirs = .* venv build dist ``` 注意:pytest.ini文件应该使用INI文件格式。 关于pytest.ini的更多配置,可以参考官方文档:https://docs.pytest.org/en/stable/reference.html#configuration-options 如果用户需要,我们还可以生成相关问题。</think>### pytest.ini 配置文件详解 `pytest.ini` 是 pytest 框架的核心配置文件,用于定义项目级别的默认行为和设置。该文件通常位于项目根目录,当运行 `pytest` 命令时会自动加载。以下是主要配置项: #### 1. **基本结构** ```ini [pytest] # 配置项在此处定义 addopts = -v --tb=short markers = slow: marks tests as slow smoke: smoke test suite ``` #### 2. **常用配置项** - **`addopts`** 指定默认命令行参数: ```ini addopts = -v # 详细输出 --durations=10 # 显示最慢的10个测试 --cov=src # 启用覆盖率检测 ``` - **`markers`** 注册自定义标记(避免 `PytestUnknownMarkWarning`): ```ini markers = db: require database access ui: selenium web tests ``` - **测试发现规则** ```ini python_files = test_*.py *_test.py # 测试文件命名模式 python_classes = Test* Check* # 测试类命名模式 python_functions = test_* check_* # 测试函数命名模式 ``` - **路径配置** ```ini testpaths = tests integration_tests # 指定测试目录 norecursedirs = .venv dist build # 忽略的目录 ``` #### 3. **插件配置示例** - **pytest-xdist 并行测试** ```ini [pytest] addopts = -n auto # 自动使用所有CPU核心 ``` - **Allure 报告集成** ```ini [pytest] addopts = --alluredir=./allure-results ``` - **覆盖率配置** ```ini [pytest] addopts = --cov=src --cov-report=html ``` #### 4. **环境变量管理** ```ini [pytest] env = DB_HOST=localhost API_KEY=test_key ``` #### 5. **完整配置示例** ```ini [pytest] addopts = -v --tb=short -n auto --cov=src --cov-report=html markers = slow: timeout > 1s integration: external service testpaths = tests norecursedirs = .* node_modules python_files = test_*.py env = ENV=staging ``` > **重要提示**: > 1. 配置优先级:命令行参数 > `pytest.ini` > 默认设置 > 2. 使用 `pytest --help` 查看所有可用选项 > 3. 验证配置:`pytest -c pytest.ini --collect-only`[^1][^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值