Python的`assert False`如何阻止pytest工作?

时间:2019-01-24 04:48:09

标签: python import assert

我在以下类的assert False方法的flask/helpers.py中添加了__init__语句:

class locked_cached_property(object):
    """A decorator that converts a function into a lazy property.  The
    function wrapped is called the first time to retrieve the result
    and then that calculated result is used the next time you access
    the value.  Works like the one in Werkzeug but has a lock for
    thread safety.
    """

    def __init__(self, func, name=None, doc=None):

        assert False
        # ^ this is the problem

        self.__name__ = name or func.__name__
        self.__module__ = func.__module__
        self.__doc__ = doc or func.__doc__
        self.func = func
        self.lock = RLock()

    def __get__(self, obj, type=None):
        if obj is None:
            return self
        with self.lock:
            value = obj.__dict__.get(self.__name__, _missing)
            if value is _missing:
                value = self.func(obj)
                obj.__dict__[self.__name__] = value
            return value

现在,当我运行pytest时,我得到以下回溯:


Traceback (most recent call last):
  File "~/code/virtualenv/project_a/lib/python3.6/site-packages/_pytest/config/__init__.py", line 381, in _getconftestmodules
    return self._path2confmods[path]
KeyError: local('~/code/flask/tests')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "~/code/virtualenv/project_a/lib/python3.6/site-packages/_pytest/config/__init__.py", line 412, in _importconftest
    return self._conftestpath2mod[conftestpath]
KeyError: local('~/code/flask/tests/conftest.py')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "~/code/virtualenv/project_a/lib/python3.6/site-packages/_pytest/config/__init__.py", line 418, in _importconftest
    mod = conftestpath.pyimport()
  File "~/code/virtualenv/project_a/lib/python3.6/site-packages/py/_path/local.py", line 668, in pyimport
    __import__(modname)
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
  File "~/code/virtualenv/project_a/lib/python3.6/site-packages/_pytest/assertion/rewrite.py", line 290, in load_module
    six.exec_(co, mod.__dict__)
  File "~/code/flask/tests/conftest.py", line 19, in <module>
    import flask
  File "~/code/flask/flask/__init__.py", line 21, in <module>
    from .app import Flask, Request, Response
  File "~/code/flask/flask/app.py", line 26, in <module>
    from . import cli, json
  File "~/code/flask/flask/cli.py", line 32, in <module>
    from .helpers import get_debug_flag, get_env, get_load_dotenv
  File "~/code/flask/flask/helpers.py", line 894, in <module>
    class _PackageBoundObject(object):
  File "~/code/flask/flask/helpers.py", line 956, in _PackageBoundObject
    @locked_cached_property
  File "~/code/flask/flask/helpers.py", line 876, in __init__
    assert False
AssertionError
ERROR: could not load ~/code/flask/tests/conftest.py

我很好奇assert False函数在使用__init__函数时如何导致导入失败。如果assert False__get__函数中,我仍然可以导入模块。

对于pytest 3.8.1和4.0.2,会发生这种情况。

这是否意味着在模块导入期间调用了装饰器的__init__

2 个答案:

答案 0 :(得分:2)

定义函数或类时,python解释器将执行装饰器。

然后,它到达您的断言行并引发异常

答案 1 :(得分:1)

您的装饰器在使用时将始终引发异常。请记住,装饰器代码在装饰的def行之后立即执行。因此,在大多数情况下,无一例外都无法导入模块。由于pytest首先导入模块,因此它将永远无法执行测试。