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 也可以当配置文件
2020年第五期《python接口自动化+测试开发》课程,10月11号开学(火热报名中!)
本期上课时间:10月11号-1月3号,每周六、周日晚上20:30-22:30

(0)

相关推荐

  • Python测试工具——nose简介

    好多天没来打卡了.博主最近一直在把碎片化知识转化为知识体系的过程中挣扎. Python语言.selenium.unittest框架.HTMLTestRunner框架都有所了解,也写了一批脚本去做项目的 ...

  • 5 分钟掌握 Python 中常见的配置文件

    为什么要写配置文件 在开发过程中,我们常常会用到一些固定参数或者是常量.对于这些较为固定且常用到的部分,往往会将其写到一个固定文件中,避免在不同的模块代码中重复出现从而保持核心代码整洁. 这个固定文件 ...

  • pytest文档62-内置fixture之request

    前言 request 是 pytest 的内置 fixture , "为请求对象提供对请求测试上下文的访问权,并且在fixture被间接参数化的情况下具有可选的"param&quo ...

  • pytest文档14-函数传参和fixture传参数request

    前言 为了提高代码的复用性,我们在写用例的时候,会用到函数,然后不同的用例去调用这个函数. 比如登录操作,大部分的用例都会先登录,那就需要把登录单独抽出来写个函数,其它用例全部的调用这个登陆函数就行. ...

  • pytest文档18-配置文件pytest.ini

    前言 pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行. ini配置文件 pytest里面有些文件是非test文件 py ...

  • pytest文档51-内置fixture之cache使用

    前言 pytest 运行完用例之后会生成一个 .pytest_cache 的缓存文件夹,用于记录用例的ids和上一次失败的用例. 方便我们在运行用例的时候加上-lf 和 -ff 参数,快速运行上一次失 ...

  • pytest文档63-内置fixture之pytestconfig

    前言 前面讲 request 是pytest的一个内置 fixture ,作用是获取测试的上下文,可以通过request.config 获取配置对象. pytestconfig 的作用跟 reques ...

  • pytest文档64-内置 pytestconfig 动态添加和获取 pytest.ini 配置参数

    前言 前面讲 pytestconfig 的时候,可以获取到 pytest.ini 里面的配置参数. 我们在写项目自动化用例的时候,有一些配置参数希望能加到配置里面,如configid, product ...

  • pytest文档1-环境准备与入门

    前言 首先说下为什么要学pytest,在此之前相信大家已经掌握了python里面的unittest单元测试框架,那再学一个框架肯定是需要学习时间成本的. 刚开始我的内心是拒绝的,我想我用unittes ...

  • pytest文档2-用例运行规则

    用例设计原则 文件名以test_*.py文件和*_test.py 以test_开头的函数 以Test开头的类 以test_开头的方法 所有的包pakege必须要有__init__.py文件 help帮 ...

  • pytest文档3-pycharm运行pytest

    前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...