pytest文档65-内置 request 读取项目的根目录 rootdir
前言
写自动化测试项目的时候,经常要用到配置文件,比如读取数据库相关的配置,希望单独放到 config 配置文件,方便维护。
pytest 的内置 fixture 可以获取到配置相关的信息,request.config.rootdir 用于获取项目的跟目录。
config 配置文件
再项目下新建一个 config 文件,相关配置信息用 yaml 文件维护数据
在conftest.py 下写读取配置文件的 fixture, 这里我设置为 autouse=True 主要是为了查看打印读取到的目录
import pytest
import os
import yaml
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
@pytest.fixture(scope="session", autouse=True)
def dbinfo(request):
dbfile = os.path.join(request.config.rootdir,
"config",
"dbenv.yml")
print("dbinfo file path :%s" % dbfile)
with open(dbfile) as f:
dbenv_config = yaml.load(f.read(), Loader=yaml.SafeLoader)
print(dbenv_config)
return dbenv_config
rootdir 读取
打开 cmd 命令行,在项目的跟目录运行用例
pytest -s
D:\wangyiyun\webauto>pytest -s
================================================= test session starts =================================================
platform win32 -- Python 3.6.6, pytest-4.5.0, py-1.9.0, pluggy-0.13.1
rootdir: D:\wangyiyun\webauto
plugins: allure-pytest-2.8.6
collected 5 items
case\test_1.py dbinfo file path :D:\wangyiyun\webauto\config\dbenv.yml
{'host': '47.104.x.x', 'port': 3306, 'user': 'root', 'passwd': 123456, 'db': 'test'}
test xxx
.
case\test_x1.py test 111111
.test 22222222
.test 3333333
.test 444444444
.
=================
这时候可以看到读取到的配置文件地址:D:\wangyiyun\webauto\config\dbenv.yml
在项目根目录运行用例是标准的运行姿势,但是有些小伙伴会 cd 到 case 目录,运行单个用例
D:\wangyiyun\webauto>cd case
D:\wangyiyun\webauto\case>pytest test_1.py
================================================= test session starts =================================================
platform win32 -- Python 3.6.6, pytest-4.5.0, py-1.9.0, pluggy-0.13.1
rootdir: D:\wangyiyun\webauto\case
plugins: allure-pytest-2.8.6
collected 1 item
test_1.py E [100%]
======================================================= ERRORS ========================================================
______________________________________________ ERROR at setup of test_x _______________________________________________
request = <SubRequest 'dbinfo' for <Function test_x>>
@pytest.fixture(scope="session", autouse=True)
def dbinfo(request):
dbfile = os.path.join(request.config.rootdir,
"config",
"dbenv.yml")
print("dbinfo file path :%s" % dbfile)
> with open(dbfile) as f:
E FileNotFoundError: [Errno 2] No such file or directory: 'D:\\wangyiyun\\webauto\\case\\config\\dbenv.yml'
..\conftest.py:14: FileNotFoundError
------------------------------------------------ Captured stdout setup ------------------------------------------------
dbinfo file path :D:\wangyiyun\webauto\case\config\dbenv.yml
=============================================== 1 error in 0.08 seconds ===============================================
这个时候就会出现报错:No such file or directory: 'D:\wangyiyun\webauto\case\config\dbenv.yml'
因为此时的项目跟目录就变成了 rootdir: D:\wangyiyun\webauto\case
接下来我们需要解决的问题时,不管在哪个目录运行,它的项目跟目录应该都是我们的工程目录 D:\wangyiyun\webauto
pytest.ini
pytest 运行用例的时候项目的 rootdir 当没有 pytest.ini 配置文件的时候会根据 conftest.py 找到它的跟目录。
由于前面没有用到pytest.ini 配置文件,导致不同目录运行用例的 rootdir 不一样。
当项目下存在 pytest.ini 配置文件的时候,会认为 pytest.ini 所在的目录是 rootdir 目录, 所以我们一般会把 pytest.ini 配置文件放到项目的跟目录。
如果里面没有内容,放个空的也行
这时候不管在哪个目录运行用例都不会有问题了
D:\wangyiyun\webauto\case>pytest test_1.py
======================== test session starts ==============
platform win32 -- Python 3.6.6, pytest-4.5.0, py-1.9.0, pluggy-0.13.1
rootdir: D:\wangyiyun\webauto, inifile: pytest.ini
plugins: allure-pytest-2.8.6
collected 1 item
test_1.py . [100%]
========================1 passed in 0.03 seconds =============
pytest的配置文件除了 pytest.ini,还有 tox.ini 和 setup.cfg 也可以当配置文件
网易云完整视频课程《pytest+yaml 框架使用与开发》https://study.163.com/course/courseMain.htm?courseId=1213419817&share=2&shareId=480000002230338
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2019-12-01 jmeter压测学习9-响应断言
2019-12-01 jmeter压测学习8-压测带token的接口
2017-12-01 selenium+python自动化84-chrome手机wap模式(登录淘宝页面)
2016-12-01 Selenium2+python自动化7-xpath定位
2016-12-01 Selenium2+python自动化6-八种元素元素定位(Firebug和firepath)
2016-12-01 Selenium2+python自动化5-操作浏览器基本方法