pytest测试框架——pytest.ini用法
2024.01.17 15:43浏览量:5简介:介绍pytest.ini文件的用途以及如何使用它来改变pytest的运行方式,包括命令行选项、测试文件路径和标记等设置。
千帆应用开发平台“智能体Pro”全新上线 限时免费体验
面向慢思考场景,支持低代码配置的方式创建“智能体Pro”应用
立即体验
在pytest测试框架中,pytest.ini文件是一个重要的配置文件,用于读取各种配置信息,从而改变pytest的运行方式。下面将详细介绍如何使用pytest.ini文件进行配置和设置。
一、基本概念
pytest.ini文件是一个固定文件,其名称必须为pytest.ini,并且需要放置在项目根目录下。该文件用于存储pytest的配置信息,以便在运行测试时使用。
二、常用配置项
- 命令行选项
使用addopts参数可以更改默认的命令行选项。例如,运行完测试后生成报告,失败重跑3次等。将这些命令加入pytest.ini后,就可以在运行pytest时直接使用这些参数,而无需在命令行重复输入。
示例:addopts = -v --reruns 1 --html=report.html --self-contained-html
- 测试文件路径
使用testfile参数指定需要执行的测试用例所在的目录。多个目录之间用空格隔开。这样可以在运行pytest时指定特定的测试文件或目录。
示例:testfile = /testcase
- 标记设置
使用markers参数可以为测试用例添加标记,这些标记可以在运行测试时用来过滤或选择特定的测试用例。这些标记的别名可以根据实际需要进行定义。例如:
示例:
三、高级配置项markers = webtest: Run the webtest case, hello: Run the hello case
- 控制台实时输出日志
通过设置log_cli参数为True或False(默认),可以控制是否在控制台实时输出日志。如果设置为True,可以清晰地看到每个包或模块下的用例执行情况。这对于调试和跟踪测试执行过程非常有用。 - 递归遍历子目录
默认情况下,pytest在收集测试用例时会递归遍历所有子目录。如果有些目录不需要执行,可以使用norecursedirs参数来简化pytest的搜索工作。例如:
示例:
```makefile
norecursedirs = nodemodules .pytestcache .pytesttmppath .h2server .venv .mypycache build dist coverage .eggs .idea .git .pycache .tox .venv37 .venv38 .venv39 .venv310 .venv311 .venv312 .venv313 .venv314 .venv315 .venv316 .venv317 .venv318 .venv319 .venv320 .venv321 .venv322 .venv323 .venv324 .venv325 .venv326 .venv327 .venv328 .venv329 .venv330 .venv331 .venv332 .venv333 .venv340 .venv341 .venv342 .venv343 .venv344 .venv345 .venv346 .venv347 .venv348 .venv349 .venv350 .venv351 .venv352 .venv353 .venv354 .venv355 .venv356 .venv357 .venv358 .venv359 .venv360 nodemodules coverage pytest cache dist build pycache venvs venvs2 pycache pycache2 nodemodules venv venvs nodemodules dist build pycache build dist nodemodules build venvs venvs2 venv nodemodules build __pycache dist coverage _pytest cache pycache build dist node_modules __pycache venvs venvs2 venv node_modules build venvs venvs2 venv node_modules build __pycache dist coverage _pytest cache pycache build dist node_modules __pycache venvs venvs2 venv node_modules build venvs venvs2 venv node_modules build __pycache dist coverage _pytest cache pycache build dist node_modules __pycache

发表评论
登录后可评论,请前往 登录 或 注册