在 pytest 测试会话开始之前以编程方式查找 pytest 的根目录

问题描述 投票:0回答:1

如果我使用命令“pytest -s -v”运行测试用例 我的测试用例将获取 pytest.ini 文件所在的根目录路径。

但我想在 python 测试会话开始之前以编程方式在“conftest.py”文件中或从“api”文件夹中的文件中找到根目录。 请注意:我想在 pytest 测试会话开始之前获取根目录

我在互联网上进行了大量搜索,但没有得到满足我要求的答案。 请帮忙

我的Python自动化项目结构如下。

api-automation
  api
    packagename
       __init__.py
       user.py
       payloads
         a.json
         b.json    
  tests
    test_1.py
    test_2.py
    conftest.py
  setup.cfg
  setup.py
  pytest.ini
  README.rst

conftest.py内容如下

import pytest

@pytest.fixture(scope='session', autouse=True)
def root_directory(request):
    """
    :return:
    """
    return str(request.config.rootdir)

test_1.py的内容如下,

def test_first(root_directory):
    print("root_directory", root_directory)
python-3.x pytest
1个回答
0
投票

例如,自己定义变量,不要依赖 Pytest

import pathlib

ROOTDIR = pathlib.Path(__file__).resolve().parent.parent
© www.soinside.com 2019 - 2024. All rights reserved.