简体   繁体   English

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

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

I've added an assert False statement in flask/helpers.py in the following class's __init__ method: 我在以下类的__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

and now when I'm running pytest I get the following traceback: 现在,当我运行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

I'm curious how an assert False statement can seem to cause an import failure when in the __init__ function. 我很好奇,在__init__函数中使用assert False语句可能会导致导入失败。 If the assert False was in the __get__ function I am still able to import the module. 如果assert False__get__函数中,我仍然可以导入模块。

This occurs for pytest 3.8.1 and 4.0.2. pytest 3.8.1和4.0.2会发生这种情况。

Does this mean that a decorator's __init__ is called during the importing of the module? 这是否意味着在模块导入期间会调用装饰器的__init__

When you defined a function or class, python interpreter executed the decorator. 定义函数或类时,python解释器将执行装饰器。

Then, it hit your assert line and raise an exception 然后,它到达您的断言行并引发异常

Your decorator will always raise an exception when being used. 装饰器在使用时总是会引发异常。 And remember that the decorator code is executed right after the def line it decorates. 请记住,装饰器代码在装饰的def行之后立即执行。 So, in most cases the module can not be imported without an exception. 因此,在大多数情况下,无一例外都无法导入模块。 Since pytest imports a module first it will never get to the point where it executes the tests. 由于pytest导入模块,因此它将永远无法执行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM