接口测试 [求助] pytest hook 函数 pytest_generate_tests, 数据位置问题

爱吃米饭 · October 29, 2020 · 232 hits

pytest中开放了一些内置hook函数,可以实现定制。在研究的过程中,有一些问题不懂,求大佬指教:
问题是:将下面代码的myids, mydatas的赋值放到class下面,就会报如下错误;但是如果把赋值位置放到class外面,就能够成功。
求问这是什么原因呢?
任何帮助都将感激不尽🎀

# 测试代码:
import yaml

class TestParam:

with open('datas/a.yml') as f:
datas = yaml.safe_load(f)
myids = datas.keys()
mydatas = datas.values()

def test_param(self, param1):
print(f"param = {param1}")
print("动态生成测试用例")

#  conftest.py
def pytest_generate_tests(metafunc: "Metafunc") -> None:
if "param1" in metafunc.fixturenames:
metafunc.parametrize("param1",
metafunc.module.mydatas,
ids=metafunc.module.myids,
scope='function')
test_param.py:None (test_param.py)
../../../new_test_data/mno_test/venv/lib/python3.7/site-packages/pluggy/hooks.py:286: in __call__
return self._hookexec(self, self.get_hookimpls(), kwargs)
../../../new_test_data/mno_test/venv/lib/python3.7/site-packages/pluggy/manager.py:93: in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
../../../new_test_data/mno_test/venv/lib/python3.7/site-packages/pluggy/manager.py:87: in <lambda>
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
../../../new_test_data/mno_test/venv/lib/python3.7/site-packages/_pytest/python.py:246: in pytest_pycollect_makeitem
res = list(collector._genfunctions(name, obj))
../../../new_test_data/mno_test/venv/lib/python3.7/site-packages/_pytest/python.py:454: in _genfunctions
self.ihook.pytest_generate_tests.call_extra(methods, dict(metafunc=metafunc))
../../../new_test_data/mno_test/venv/lib/python3.7/site-packages/pluggy/hooks.py:324: in call_extra
return self(**kwargs)
../../../new_test_data/mno_test/venv/lib/python3.7/site-packages/pluggy/hooks.py:286: in __call__
return self._hookexec(self, self.get_hookimpls(), kwargs)
../../../new_test_data/mno_test/venv/lib/python3.7/site-packages/pluggy/manager.py:93: in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
../../../new_test_data/mno_test/venv/lib/python3.7/site-packages/pluggy/manager.py:87: in <lambda>
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
conftest.py:74: in pytest_generate_tests
metafunc.module.mydatas,
E AttributeError: module 'test_param' has no attribute 'mydatas'

No Reply at the moment.
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up