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

How can Python's `assert False` prevent pytest from working?

我在下面的 class 的 __init__ 方法中的 flask/helpers.py 中添加了一个 assert False 语句:

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__

你的装饰器在使用时总是会抛出异常。请记住,装饰器代码是在它装饰的 def 行之后立即执行的。所以,在大多数情况下,模块不能无一例外地被导入。由于 pytest 首先导入一个模块,它永远不会到达执行测试的地步。

当您定义函数或 class 时,python 解释器执行装饰器。

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