使用 pytest 自定义配置变量

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

AIM - 我正在尝试将配置变量“db_str”传递给我的 pytest 脚本(test_script.py)

db_str变量在development.ini中定义

我尝试过使用命令

pytest -c development.ini regression_tests/test_script.py 

但是没有成功

错误

>       conn_string = config['db_string']
        KeyError: 'db_string' 

我尝试使用conftest.py,但没有成功

#contest.py code
import pytest

def pytest_addoption(parser):
   parser.addoption("--set-db_st", 
   action="store",help="host='localhost' dbname='xyz' user='portaladmin'")

@pytest.fixture
def db_str(request):
   return request.config.getoption("--set-db_str")

Py测试代码

from S4M_pyramid.modelimport MyModel
from S4M_pyramid.lib.deprecated_pylons_globals import config

import subprocess

config['db_str'] = db_str
def test_get_dataset_mapping_id():
   result = MyModel.get_dataset_mapping_id()
   assert len(result) >1

如何将变量“db_str”从development.ini或任何其他ini文件传递到pytest脚本

python-3.x pytest pyramid
3个回答
3
投票

逻辑如下:

  1. 定义将用于传递有关环境/配置文件的信息的 CLI 参数
  2. 在 pytest 夹具中获取 CLI 参数值
  3. 解析配置文件
  4. 使用
    get_database_string
    固定装置中解析的配置来获取数据库连接字符串
  5. 在测试中使用
    get_database_string
    夹具来获取连接字符串

conftest.py

import os

from configparser import ConfigParser

# in root of the project there is file project_paths.py
# with the following code ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
import project_paths 


def pytest_addoption(parser):
    """Pytest hook that defines list of CLI arguments with descriptions and default values

    :param parser: pytest specific argument
    :return: void
    """
    parser.addoption('--env', action='store', default='development',
                     help='setup environment: development')



@pytest.fixture(scope="function")
def get_database_string(get_config):
    """Fixture that returns db_string

    :param get_config: fixture that returns ConfigParser object that access 
    to config file
    :type: ConfigParser

    :return: Returns database connection string
    :rtype: str
    """
    return get_config['<section name>']['db_string']


@pytest.fixture(scope="function")
def get_config(request):
    """Functions that reads and return  ConfigParser object that access 
    to config file

    :rtype: ConfigParser
    """
    environment = request.config.getoption("--env")
    config_parser = ConfigParser()
    file_path = os.path.join(project_paths.ROOT_DIR, '{}.ini'.format(environment))
    config_parser.read(file_path)
    return config_parser

test_file.py

import pytest

def test_function(get_database_string)
    print(get_database_string)

>>> <data base string from development.ini>


3
投票

pytest_addoption所述:

添加选项:

要添加命令行选项,请调用 parser.addoption(...)。

要添加 ini 文件值,请调用 parser.addini(...)。

获取选项:

稍后可以通过配置对象分别访问选项:

config.getoption(name) 检索命令行选项的值。

config.getini(name)检索从ini样式文件读取的值。

conftest.py:

def pytest_addoption(parser):
    parser.addini('foo', '')

测试.py:

def test_func(request):
    request.config.getini('foo')

0
投票

我是来自 Java 背景的 Python 新手,只是想了解以下语法的作用,

  1. 什么是 param(请求),它是如何传递的以及调用 request.config 时它会做什么?什么是配置?

def test_func(请求): request.config.getini('foo')

© www.soinside.com 2019 - 2024. All rights reserved.