AttributeError: 'function' object has no attribute 'im_class'

AttributeError: 'function' object has no attribute 'im_class'

升级pytest后,我们的自定义插件失败是这样的:

Traceback (most recent call last):
  File "/home/foo_eins_d/local/lib/python2.7/site-packages/_pytest/main.py", line 94, in wrap_session
    session.exitstatus = doit(config, session) or 0
  File "/home/foo_eins_d/local/lib/python2.7/site-packages/_pytest/main.py", line 124, in _main
    config.hook.pytest_collection(session=session)
  File "/home/foo_eins_d/local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 724, in __call__
    return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
  File "/home/foo_eins_d/local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 338, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/home/foo_eins_d/local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 333, in <lambda>
    _MultiCall(methods, kwargs, hook.spec_opts).execute()
  File "/home/foo_eins_d/local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 596, in execute
    res = hook_impl.function(*args)
  File "/home/foo_eins_d/local/lib/python2.7/site-packages/_pytest/main.py", line 133, in pytest_collection
    return session.perform_collect()
  File "/home/foo_eins_d/local/lib/python2.7/site-packages/_pytest/main.py", line 567, in perform_collect
    config=self.config, items=items)
  File "/home/foo_eins_d/local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 724, in __call__
    return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
  File "/home/foo_eins_d/local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 338, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/home/foo_eins_d/local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 333, in <lambda>
    _MultiCall(methods, kwargs, hook.spec_opts).execute()
  File "/home/foo_eins_d/local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 596, in execute
    res = hook_impl.function(*args)
  File "/home/foo_eins_d/local/lib/python2.7/site-packages/pytest_djangotools/hooks.py", line 71, in pytest_collection_modifyitems
    if not _safe_to_run(item._obj.im_class):
AttributeError: 'function' object has no attribute 'im_class'

插件代码:

def pytest_collection_modifyitems(session, config, items):
    new=[]
    for item in items:
        if not _safe_to_run(item._obj.im_class):
            continue
        new.append(item)
    items[:] = new

通过 item._obj.im_class 我们从测试方法访问了 class。

如何获取测试用例class?

并非所有 item 对象都是绑定方法。 pytest 也可以 运行 普通函数 作为测试,并且没有 TestCase 与之相关联。

你可以跳过这些:

def pytest_collection_modifyitems(session, config, items):
    new=[]
    for item in items:
        if not hasattr(item._obj, 'im_class'):
            # not a method, can't check if safe
            continue
        if not _safe_to_run(item._obj.im_class):
            continue
        new.append(item)
    items[:] = new