Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 带有pytest的自定义配置变量_Python 3.x_Pytest_Pyramid - Fatal编程技术网

Python 3.x 带有pytest的自定义配置变量

Python 3.x 带有pytest的自定义配置变量,python-3.x,pytest,pyramid,Python 3.x,Pytest,Pyramid,目标-我正在尝试将配置变量“db_str”传递给我的pytest脚本(test_script.py) db_str变量在development.ini中定义 我试过使用命令 pytest -c development.ini regression_tests/test_script.py 但它不起作用 #contest.py code import pytest def pytest_addoption(parser): parser.addoption("--set-db_st",

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

db_str变量在development.ini中定义

我试过使用命令

pytest -c development.ini regression_tests/test_script.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")
错误

我尝试使用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")
Pytest代码

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脚本

逻辑如下:

  • 定义用于传递有关环境/配置文件信息的CLI参数
  • 获取pytest fixture中的CLI参数值
  • 解析配置文件
  • 使用
    get_database_string
    fixture中解析的配置来获取数据库连接字符串
  • 在测试中使用
    get\u database\u string
    fixture获取连接字符串
  • 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
    
    import pytest
    
    def test_function(get_database_string)
        print(get_database_string)
    
    >>

    如上所述:

    添加选项:

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

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

    获取选项:

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

    获取命令行选项的值

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

    conftest.py:

    def pytest_addoption(parser):
        parser.addini('foo', '')
    
    def test_func(request):
        request.config.getini('foo')
    
    test.py:

    def pytest_addoption(parser):
        parser.addini('foo', '')
    
    def test_func(request):
        request.config.getini('foo')
    

    您使用了4种不同的名称拼写:
    --set-db\u st
    --set-db\u str
    db\u str
    db\u string
    。你能选择一种拼写并在代码中的任何地方使用它吗?你能帮我解决以下错误吗?我正在获取environment=request.config.getoption(“--env”)NameError:name'request'未定义。在**def get_config():“>读取并返回访问配置文件的ConfigParser对象的函数:rtype:ConfigParser”“”>environment=request.config.getoption(--env”)**中,我还收到了返回get_config.get('db_string')类型的错误:get()缺少1个必需的位置参数:“option”@ghanishtnagpal我在回答中更新了代码段。我在
    get\u config fixture
    中缺少
    request
    作为参数。至于你的第二条评论,我也更新了代码。因此,为了能够从配置中获取数据,您应该执行类似于
    config['section name']['db_string']