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 也可以當配置文件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM