pytest合集(10)— pytest.ini文件

1、配置文件类型

配置文件一般位于项目的根目录。pytest支持的配置文件如下(按照优先级顺序排列):

  • pytest.ini:主配置文件,优先级最高。
  • pyproject.toml:6.0 版中的新功能,Python生态系统中软件打包的未来
  • tox.ini:tox项目的配置文件
  • setup.cfg:通用配置文件,除非非常简单的用例,否则不建议使用

2、配置文件选项

pytest -h 可以查看pytest命令行参数大全,其中 [pytest] ini-options 列出了所有配置选项列表,这些配置选项可以写入任意配置文件中,格式 name=value,一个配置选项如果有多个values需要使用空格分割,也可以使用分号添加注释。

常见配置选项如下:

addopts:命令行参数

addopts = --strict-markers :只允许使用已知标记,未在pytest.ini文件中注册的任何标记都将引发异常,这可用于防止用户意外输错标记名称。

xfail_strict = true:@pytest.mark.xfail标记预期失败的测试用例,如果执行成功,结果将标记为FAILED,而不再是XPASS了。

markers:标记

usefixtures:夹具

testpaths python_files,python_classes ,python_functions  :管理测试用例搜索范围

testpaths = testcases

python_files =     test_*  *_test  test*

python_classes =   Test*   test*

python_functions = test_*  test*

norecursedirs:需要忽略的搜索目录,pytest 收集测试用例时,会递归遍历所有子目录,包括某些你明知道没必要遍历的目录,遇到这种情况,可以使用 norecursedirs 参数简化 pytest 的搜索工作,默认忽略选项如下:

norecursedirs = .*  *.egg  _darcs build CVS dist node_modules venv {arch}

cache_dir: 自定义pytest缓存目录,可以是相对路径和绝对路径。pytest运行测试用例的时候,默认会在当前路径下创建.pytest_cache文件夹,即pytest缓存目录。

console_output_style:控制台输出样式

  • classic,经典的 pytest 输出
  • progress,类似于经典的 pytest 输出,但带有进度指示器 [ 66%]
  • count,类似于 progress ,但将进度显示为已完成的测试数量而不是百分比[2/3]

filterwarnings:警告过滤器,设置对匹配的警告应采取的过滤器和操作列表。默认情况下,测试会话期间发出的所有警告都将在测试会话结束时显示在摘要中。

minversion :指定运行测试所需的最小pytest版本。

Captured log:日志捕获。

  • log_level = info
  • log_format = %(asctime)s %(levelname)s %(message)s
  • log_date_format = %Y-%m-%d %H:%M:%S

Live Log:实时日志

  • log_cli = True
  • log_cli_level = INFO
  • log_cli_date_format = %Y-%m-%d %H:%M:%S
  • log_cli_format = %(asctime)s %(levelname)s %(message)s

日志文件:

  • log_file = logs/pytest-logs.txt
  • log_file_level = INFO
  • log_file_date_format = %Y-%m-%d %H:%M:%S
  • log_file_format = %(asctime)s %(levelname)s %(message)s

3、配置文件pytest.ini

pytest.ini是pytest框架的主配置文件,优先级最高,一般位于项目根目录中,pytest运行的时候会自动读取该文件的配置。用于指定和管理项目的配置选项。它采用INI文件格式,允许在文件中添加注释以提供对配置选项的说明和解释。

在pytest.ini文件中,你可以在每个配置选项的行之前使用分号 (;) 或井号 (#) 来添加注释。注释部分将被解释器忽略,不会影响配置选项的解析和使用。

下面是一个pytest.ini文件示例:

[pytest]
;命令行参数
addopts = -vs
;--strict-markers   只允许使用已知标记,未在pytest.ini文件中注册的任何标记都将引发异常。
;--html=./report/report.html    生成测试报告

;mark标记预期失败的测试用例,如果执行成功,结果将标记为FAILED,而不再是XPASS了。
xfail_strict = true

;注册自定义标记
markers =
    slow: marks tests as slow (deselect with '-m "not slow"')
    foo:custom mark1

;测试用例搜索范围
testpaths = testfixture
python_files = test_*  *_test  test*
python_classes = Test*   test*
python_functions = test_*  test*

;设置控制台输出样式:
console_output_style = progress

;指定运行测试所需的最小pytest版本。
minversion = 6.2.3

思考:pytest运行的时候是怎么读取pytest.ini配置文件?

新建pytest-test项目,目录如下:

testcases/test_sample.py 文件内容如下:

# content of test_sample.py
def test_one():
    pass

 testmodule/test_module.py 文件内容如下:

# content of test_module.py

class TestClass:
    def test_two(self):
        pass

    def test_three(self):
        pass

testmodule/testmodule1/test_module1.py 文件内容如下:

# content of test_module1.py
def test_four():
    pass

pytest.ini 主配置文件内容如下:

[pytest]
addopts = -v --html=./report/report.html

(1)在项目根目录下运行pytest结果如下:

 运行结果可知,控制台输出了测试的详细信息和生成了测试报告,跟pytest.ini文件中的命令行参数一致, 可知,项目根目录下的pytest.ini文件作用于项目下所有的测试用例。

(2)切换到testmodule路径下执行pytest:

运行结果可知,pytest只收集了testmodle模块下的所有测试用例,但是读取了项目根目录下pytest.ini文件,生成了测试报告。

(3)切换到testmodule1路径下执行pytest:

运行结果可知,pytest只收集了testmodle1模块下的所有测试用例,但是读取了项目根目录下pytest.ini文件,生成了测试报告。

(4)将pytest.ini文件移动到testcases目录下,仍在testmodule1路径下执行pytest:

运行结果可知,pytest运行的时候并没有读取到testcases目录下的pytest.ini文件。

总结:pytest运行的时候会去读取当前脚本路径及其父路径,直到项目根目录下的pytest.ini文件。

4、问题记录

运行pytest报错:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 11: invalid continuation byte

报错原因分析:

pytest.ini配置文件中有中文和某些未知的特殊字符,造成"utf-8"编码错误。

解决方案:

修改pytest.ini配置文件的编码方式为"GBK"

修改完后再次运行:


reference:

Configuration — pytest documentation

API Reference — pytest documentation

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值