pytest:如何从文件中读取装置列表?
我想通过pytest(via)断言主机上是否存在包。 我有一个文本文件中应该存在的包列表,我可以读取并放入数组中。我想用这个数组来参数化一个装置,这样我就可以在测试中使用它了 比如:pytest:如何从文件中读取装置列表?,pytest,fixtures,Pytest,Fixtures,我想通过pytest(via)断言主机上是否存在包。 我有一个文本文件中应该存在的包列表,我可以读取并放入数组中。我想用这个数组来参数化一个装置,这样我就可以在测试中使用它了 比如: @pytest.fixture def packages(): listfile = open("list.txt", "r") packages = listfile.read().splitlines() return packages 然后使用它来参数化测试: @pytest.mark
@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 ============================
/home/becker/molecular/local/lib/python2.7/site-packages/pluggy/__-init.py:617:in-call__
返回self.\u hookexec(self,self.\u非包装器+self.\u包装器,kwargs)
/home/becker/molecule/local/lib/python2.7/site-packages/pluggy/__;u-init___;.py:222:in hookexec
返回self.\u inner\u hookexec(钩子、方法、kwargs)
/home/becker/molecule/local/lib/python2.7/site-packages/pluggy/__-init.py:216:in
firstresult=hook.spec_opts.get('firstresult'),
/home/becker/molecule/local/lib/python2.7/site-packages/\u-pytest/python.py:197:in-pytest\u-pycollect\u-makeitem
res=列表(收集器类型函数(名称,obj))
/home/becker/molecule/local/lib/python2.7/site-packages/_-pytest/python.py:390:in _-genfunctions
self.ihook.pytest\u generate\u测试(metafunc=metafunc)
/home/becker/molecule/local/lib/python2.7/site-packages/pluggy/__-init.py:617:in-u-call__
返回self.\u hookexec(self,self.\u非包装器+self.\u包装器,kwargs)
/home/becker/molecule/local/lib/python2.7/site-packages/pluggy/__;u-init___;.py:222:in hookexec
返回self.\u inner\u hookexec(钩子、方法、kwargs)
/home/becker/molecule/local/lib/python2.7/site-packages/pluggy/__-init.py:216:in
firstresult=hook.spec_opts.get('firstresult'),
/home/becker/molecule/local/lib/python2.7/site-packages/\u-pytest/python.py:122:in-pytest\u-generate\u-tests
元函数参数化(*marker.args,**marker.kwargs)
/home/becker/molecule/local/lib/python2.7/site-packages/_-pytest/python.py:809:in参数化
argName、argvalues、self.function、self.config)
/home/becker/molecule/local/lib/python2.7/site packages/_pytest/mark/structures.py:102:in用于参数化
对于argvalues中的x]
E TypeError:“函数”对象不可编辑
!!!!!!!!!!!!!!!!!!! 中断:收集过程中出现1个错误!!!!!!!!!!!!!!!!!!!!
========================================0.41秒内出现1个错误============================
目前,pytest
不支持将夹具作为参数传递到pytest.mark.parametrize
。您可以在中跟踪相关讨论的当前状态
然而,固定装置也是功能。因此,正如注释中所建议的,您可以在parametrize
中调用fixture函数:
@pytest.mark.parametrize("name", packages())
def test_packages(host, name):
...
pytest
不支持将夹具作为参数传递到parametrize
(请参阅)。当参数化
展开时,夹具还没有进行评估。作为一种解决方法,您可以将fixture作为普通函数调用:@pytest.mark.parametrize(“name”,packages())
,因为packages
不需要任何其他参数。@hoefling感谢您的建议。这个变通方法正是我想要的。你想继续回答这个问题吗?