属性错误:"生成器"对象没有属性"追加"



我曾经将此代码作为单元测试运行,但突然间不起作用了

代码:

import requests
import pytest

class TestSample:
url = 'https://something/apiname'
@pytest.fixture()
def post_request(self, data):
url = self.url + '/1'
res = requests.post(url, headers={"Content-Type": "application/json"},json=data)
yield res

@pytest.mark.parametrize("data,result",
[( {"ids": ["524234", "12341", "97555"]},200))]
def test_post_sample_data(self, data, result, post_request):
rs = post_request
assert rs.status_code == result

错误:

platform win32 -- Python 3.6.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
plugins: hypothesis-5.37.4, cases-2.3.0
test_selectsample.py:None (test_selectsample.py)
....AppDataLocalProgramsPythonPython36libsite-packagespluggyhooks.py:286: in __call__
return self._hookexec(self, self.get_hookimpls(), kwargs)
....AppDataLocalProgramsPythonPython36libsite-packagespluggymanager.py:93: in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
....AppDataLocalProgramsPythonPython36libsite-packagespluggymanager.py:87: in <lambda>
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
....AppDataLocalProgramsPythonPython36libsite-packages_pytestpython.py:249: in pytest_pycollect_makeitem
initialnames, node, ignore_args=self._get_direct_parametrize_args(node)
....AppDataLocalProgramsPythonPython36libsite-packagespytest_casesplugin.py:614: in getfixtureclosure
_init_fixnames, super_closure, arg2fixturedefs = create_super_closure(fm, parentnode, fixturenames, ignore_args)
....AppDataLocalProgramsPythonPython36libsite-packagespytest_casesplugin.py:668: in create_super_closure
_merge(sorted_fixturenames, _init_fixnames)
....AppDataLocalProgramsPythonPython36libsite-packagespytest_casesplugin.py:655: in _merge
into_list.append(item)
E   AttributeError: 'generator' object has no attribute 'append'
collected 0 items / 1 error

====================================错误==============================

_____________________ ERROR collecting test_selectids.py ______________________
....AppDataLocalProgramsPythonPython36libsite-packagespluggyhooks.py:286: in __call__
return self._hookexec(self, self.get_hookimpls(), kwargs)
....AppDataLocalProgramsPythonPython36libsite-packagespluggymanager.py:93: in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
res = list(collector._genfunctions(name, obj))
....AppDataLocalProgramsPythonPython36libsite-packages_pytestpython.py:458: in _genfunctions
initialnames, node, ignore_args=self._get_direct_parametrize_args(node)
....AppDataLocalProgramsPythonPython36libsite-packagespytest_casesplugin.py:614: in getfixtureclosure
_init_fixnames, super_closure, arg2fixturedefs = create_super_closure(fm, parentnode, fixturenames, ignore_args)
....AppDataLocalProgramsPythonPython36libsite-packagespytest_casesplugin.py:668: in create_super_closure
_merge(sorted_fixturenames, _init_fixnames)
....AppDataLocalProgramsPythonPython36libsite-packagespytest_casesplugin.py:655: in _merge
into_list.append(item)
E   AttributeError: 'generator' object has no attribute 'append'
=========================== short test summary info ===========================
ERROR test_selectsample.py::TestSample - AttributeError: 'generator' object ha...
!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
============================== 1 error in 0.35s ===============================
Process finished with exit code 0
Assertion failed
Assertion failed

知道是什么原因造成的吗?我该如何预防?提前感谢

pytest.mark.parametrize的第二个参数需要是元组列表。在你这样的例子中:
@pytest.mark.parametrize("data,result", [
({"ids": ["524234", "12341", "97555"]}, 200),
])
def test_post_sample_data(self, data, result, post_request):
rs = post_request
assert rs.status_code == result

编辑:

  • 尝试用@pytest.fixture替换post_request之上的@pytest.fixture()
  • 夹具具有data参数,该参数看起来不会在任何地方填充

此问题已通过更改解释器的版本得到修复。我把蟒蛇3.6改成了3.8,问题解决了!希望这篇文章对有用

最新更新