无法使用 pytest 参数化功能来更新 YAML 配置

Unable to use pytest parameterize functionality to update the YAML config

我正在使用 ruamel.yaml 模块通过 python 更新 YAML。 我想使用 pytest 参数化功能来更新 YAML 配置。

这是 test.yaml 文件:

TestConfig:
  hostname: 10.2.4.6
  Organisation:
    Employee:
      Name: Smriti
      Skilss: Python 
      OrganizationName: ABC
      UserId: smriti_test@gmail.com

这是 conftest 文件,因此我们不需要显式导入夹具功能:

from ruamel.yaml import YAML
import pytest
@pytest.fixture(scope='function')
def yaml_loader(request):
   yaml = YAML()
   file_path = 'test.yaml'
   with open(file_path) as fp:
      data = yaml.load(fp)
      test = data['TestConfig']
      x = request.param
      print(x)
   with open(file_path, "w") as file:
      yaml.dump(data, file)
   print(data)

这里是 运行 测试的示例测试文件的实现,使用来自 conftest 的夹具并更新 YAML 中的配置并执行测试。

import pytest

class TestYAML:
    """ TestYAML """
    @pytest.mark.parametrize("yaml_loader",[("test['Organisation']['Employee']\
                                            ['Name']='Arushi'")],indirect=True)
    @pytest.mark.usefixtures("yaml_loader")
    def test_update_yamlconfig(self):
        pass

在结果中,我看到 x 正在将更新后的名称值打印到 Arushi,但在 YAML 文件中,配置没有更新。

如果你遇到 pytest 没有按你想要的那样运行的问题,你应该退后一步,确保 您的 yaml_laoder() 功能符合您的预期:

import sys
from pathlib import Path
from ruamel.yaml import YAML

yaml_str = """\
TestConfig:
  hostname: 10.2.4.6
  Organisation:
    Employee:
      Name: Smriti
      Skilss: Python 
      OrganizationName: ABC
      UserId: smriti_test@gmail.com
"""

Path('test.yaml').write_text(yaml_str)  # write out the file so that it is fresh every time

class R: pass
r = R()
r.param = "XXXXXX"

def yaml_loader(request):
   yaml = YAML()
   file_path = 'test.yaml'
   with open(file_path) as fp:
      data = yaml.load(fp)
      test = data['TestConfig']
      x = request.param
      print(x)
   with open(file_path, "w") as file:
      yaml.dump(data, file)
   print(data)

yaml_loader(r)

print('\n######### YAML #########\n')
print(Path('test.yaml').read_text())

给出:

XXXXXX
ordereddict([('TestConfig', ordereddict([('hostname', '10.2.4.6'), ('Organisation', ordereddict([('Employee', ordereddict([('Name', 'Smriti'), ('Skilss', 'Python'), ('OrganizationName', 'ABC'), ('UserId', 'smriti_test@gmail.com')]))]))]))])

######### YAML #########

TestConfig:
  hostname: 10.2.4.6
  Organisation:
    Employee:
      Name: Smriti
      Skilss: Python
      OrganizationName: ABC
      UserId: smriti_test@gmail.com

正如您可以从 YAML 内容中清楚地看到的那样,它永远不会更新 XXXXXX。这是因为您没有分配给 data 在写出 test.yaml 文件之前。

先解决这个问题,然后将代码注入到您的测试中。

经过几次尝试,我找到了这个问题的答案。

所以这是更新后的代码以供参考:- 对于测试文件,我采用了两个不同的参数,当我调用 fixtures 时我将单独分配该参数并且这有效

test_yaml.py

import pytest

class TestYAML:
    """ TestYAML """
    @pytest.mark.parametrize("yaml_loader", 
                           [("hostname","10.5.6.8")],indirect=True)
    @pytest.mark.usefixtures("yaml_loader")
    def test_update_yamlconfig(self):
        pass

这是我定义夹具的更新后的 conftest:-

from ruamel.yaml import YAML
import pytest
@pytest.fixture(scope='function')
def yaml_loader(request):
   yaml = YAML()
   file_path = 'test.yaml'
   file_path1 = 'my.yaml'
   with open(file_path) as fp:
      data = yaml.load(fp)
      test = data['TestConfig']
      test[request.param[0]] = request.param[1]
   with open(file_path1, "w") as file:
       yaml.dump(data, file)
   print(data)

你将得到如下输出:-

platform linux -- Python 3.8.0, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 -- 
/mnt/c/Users/smaheshw/PycharmProjects/YAML/venv/bin/python3.8
cachedir: .pytest_cache
rootdir: /mnt/c/Users/smaheshw/PycharmProjects/YAML
collected 1 item                                                                                                                                                                           

test_yaml.py::TestYAML::test_update_yamlconfig[yaml_loader0] Request parameters
hostname
ordereddict([('TestConfig', ordereddict([('hostname', '10.5.6.8'), ('Organisation', 
ordereddict([('Employee', ordereddict([('Name', 'Smriti'), ('Skilss', 'Python'), 
('OrganizationName', 'A
BC'), ('UserId', 'smriti_test@gmail.com')]))]))]))])
PASSED