AttributeError:'function'对象没有属性'im_class'

时间:2016-04-05 12:46:41

标签: python pytest introspection

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

    BrowserDriver.switchTo().defaultContent();

插件代码:

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 我们从test-method访问了该类。

如何获得TestCase类?

1 个答案:

答案 0 :(得分:3)

并非所有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