zl程序教程

您现在的位置是:首页 >  后端

当前栏目

pytest配置文件pytest.ini

2023-09-11 14:14:25 时间

说明:

  1. pytest.ini是pytest的全局配置文件,一般放在项目的根目录下
  2. 是一个固定的文件-pytest.ini
  3. 可以改变pytest的运行方式,设置配置信息,读取后按照配置的内容去运行

pytest.ini 设置参数

  1. addopts 设置自定义执行参数,pytest运行时以此设置为默认执行条件

例如:

进行如下设置后

执行pytest时默认执行的是pytest -v -s test_f.py

[pytest]
addopts = -v -s test_f.py
  1. filterwarnings 隐藏一些不推荐使用的警告
[pytest]
filterwarnings = ignore:.*U.*mode is deprecated:DeprecationWarning
  1. 设置执行路径 testpaths

当从[rootdir目录执行pytest时,如果在命令行中没有给出特定的目录,文件或测试ID,则设置应搜索测试的目录列表。

设置testpaths后,只在设置的路径中查找测试用例并执行,可配置多个,空格隔开

如下,只查找testcase下的测试用例并执行

[pytest]
testpaths = ./testcase
  1. timeout 超时

    超时30s后执行用例失败

[pytest]
timeout = 30
  1. norecursedirs

    pytest.ini配置norecursedirs= lxk test.py 不搜索执行对应文件夹下或文件下的用例,和testpaths配置完全相反的效果,可配置多个,空格隔开

  2. markers 分组参数

    用于对用例分组

[pytest]
markers =
    smoking :
    high    :
    medium  :
    lower   :
测试用例中标识,运行pytest -v -m smoking,只执行还有smoking标记的测试用例
@pytest.mark.smoking
def test():
    pass
  1. 设置pytest最小版本,个人理解就是一个提示,如果超过设置的最小版本有些pytest的功能不支持可能会报错,设置超过当前使用的版本,也可正常运行
minversion=6.0

cmd下使用 pytest -h 命令查看pytest.ini的设置选项:

在这里插入图片描述
在这里插入图片描述