【问题标题】:pytest: How do I read a list of fixtures from a file?pytest:如何从文件中读取设备列表?
【发布时间】:2018-07-05 12:23:54
【问题描述】:

我想通过 pytest(通过TestInfra)来断言主机上是否存在软件包。 我有一个应该在一个文本文件中的包列表,我可以读取它并将其放入一个数组中。我想使用该数组来参数化一个夹具,以便我可以在测试中使用它。

类似:

@pytest.fixture
def packages():
    listfile = open("list.txt", "r")
    packages = listfile.read().splitlines()
   return packages

然后用它来参数化一个测试:

@pytest.mark.parametrize("name", packages)
    def test_packages(host, name):
    assert host.package(name).is_installed

我得到的错误是

    /home/becker/molecule/local/lib/python2.7/site-packages/pluggy/__init__.py:617: in __call__
    return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
/home/becker/molecule/local/lib/python2.7/site-packages/pluggy/__init__.py:222: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
/home/becker/molecule/local/lib/python2.7/site-packages/pluggy/__init__.py:216: in <lambda>
    firstresult=hook.spec_opts.get('firstresult'),
/home/becker/molecule/local/lib/python2.7/site-packages/_pytest/python.py:197: in pytest_pycollect_makeitem
    res = list(collector._genfunctions(name, obj))
/home/becker/molecule/local/lib/python2.7/site-packages/_pytest/python.py:390: in _genfunctions
    self.ihook.pytest_generate_tests(metafunc=metafunc)
/home/becker/molecule/local/lib/python2.7/site-packages/pluggy/__init__.py:617: in __call__
    return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
/home/becker/molecule/local/lib/python2.7/site-packages/pluggy/__init__.py:222: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
/home/becker/molecule/local/lib/python2.7/site-packages/pluggy/__init__.py:216: in <lambda>
    firstresult=hook.spec_opts.get('firstresult'),
/home/becker/molecule/local/lib/python2.7/site-packages/_pytest/python.py:122: in pytest_generate_tests
    metafunc.parametrize(*marker.args, **marker.kwargs)
/home/becker/molecule/local/lib/python2.7/site-packages/_pytest/python.py:809: in parametrize
    argnames, argvalues, self.function, self.config)
/home/becker/molecule/local/lib/python2.7/site-packages/_pytest/mark/structures.py:102: in _for_parametrize
    for x in argvalues]
E   TypeError: 'function' object is not iterable
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.41 seconds ============================

【问题讨论】:

  • pytest 不支持将夹具作为参数传递给parametrize(请参阅issue #349)。当 parametrize 展开时,这些装置还没有被评估。作为一种解决方法,您可以将夹具作为普通函数调用:@pytest.mark.parametrize("name", packages()),因为packages 不需要任何其他参数。
  • @hoefling 感谢您的建议。这种解决方法几乎是我想要的。你想继续回答这个问题吗?

标签: pytest fixtures


【解决方案1】:

目前,pytest 不支持将夹具作为参数传递给pytest.mark.parametrize。您可以在issue #349跟踪相关讨论的当前状态。

不过,fixture 也是函数。因此,正如 cmets 中所建议的,您可以简单地调用 parametrize 中的夹具函数:

@pytest.mark.parametrize("name", packages())
def test_packages(host, name):
    ...

【讨论】:

    【解决方案2】:

    您可以使用 pytest-cases 将夹具作为参数传递:https://smarie.github.io/python-pytest-cases/(在许多其他非常有用的 pytest 扩展中)

    【讨论】:

      猜你喜欢
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多