使用模拟/补丁测试flask应用程序会导致关于缺少翻译文件的flask_security错误

时间:2018-09-15 21:19:27

标签: python unit-testing flask mocking flask-security

我正在尝试将模拟和修补程序纳入Flask应用程序的测试中。我使用标准库中的unittest Python 2.7,并安装了模拟包;

现在,我正在尝试修补os.path.exists和我自己的功能之一。如果我修补了自己的函数,则将执行测试(目前仅为assert True)。如果我尝试修补os.path.exists,就像这样

[在帮助文件中定义并导入]

def mock_os_path_exists(arg):
    return True

然后测试

from flask_brain_db import db
from user.models import *

class UserTest(unittest.TestCase):
    def setUp(self):
        # <app config is here>
    def tearDown(self):
        # <teardown proc is here>

       def register(self, email, password=None):
           return self.app.post('/register', data=dict(
           email=email,
           password=password,
           password_confirm=password
           ),
           follow_redirects=True)

    def register_and_commit_user(self):
        rv = self.register('tom.higgins@mailinator.com', 'test123')
        user = User.query.filter_by(email='tom.higgins@mailinator.com').first()
        user.confirmed_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        db.session.commit()
        return rv

    @patch('os.path.exists')
    def test_label_view(self, mock_os_path_exists):
        self.register_and_commit_user()
        assert True

这样的错误:

======================================================================
ERROR: test_label_view (test.UserTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/mock/mock.py", line 1305, in patched
    return func(*args, **keywargs)
  File "/usr/src/app/flask_brain_db/test.py", line 281, in test_label_view
    self.register_and_commit_user()
  File "/usr/src/app/flask_brain_db/test.py", line 190, in register_and_commit_user
    rv = self.register('tom.higgins@mailinator.com', 'test123')
  File "/usr/src/app/flask_brain_db/test.py", line 116, in register
    follow_redirects=True)
  File "/usr/local/lib/python2.7/site-packages/werkzeug/test.py", line 840, in post
    return self.open(*args, **kw)
  File "/usr/local/lib/python2.7/site-packages/flask/testing.py", line 200, in open
    follow_redirects=follow_redirects
  File "/usr/local/lib/python2.7/site-packages/werkzeug/test.py", line 803, in open
    response = self.run_wsgi_app(environ, buffered=buffered)
  File "/usr/local/lib/python2.7/site-packages/werkzeug/test.py", line 716, in run_wsgi_app
    rv = run_wsgi_app(self.application, environ, buffered=buffered)
  File "/usr/local/lib/python2.7/site-packages/werkzeug/test.py", line 923, in run_wsgi_app
    app_rv = app(environ, start_response)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/local/lib/python2.7/site-packages/flask_security/decorators.py", line 236, in wrapper
    return f(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/flask_security/views.py", line 117, in register
    if form.validate_on_submit():
  File "/usr/local/lib/python2.7/site-packages/flask_wtf/form.py", line 101, in validate_on_submit
    return self.is_submitted() and self.validate()
  File "/usr/local/lib/python2.7/site-packages/wtforms/form.py", line 310, in validate
    return super(Form, self).validate(extra)
  File "/usr/local/lib/python2.7/site-packages/wtforms/form.py", line 152, in validate
    if not field.validate(self, extra):
  File "/usr/local/lib/python2.7/site-packages/wtforms/fields/core.py", line 206, in validate
    stop_validation = self._run_validation_chain(form, chain)
  File "/usr/local/lib/python2.7/site-packages/wtforms/fields/core.py", line 226, in _run_validation_chain
    validator(form, self)
  File "/usr/local/lib/python2.7/site-packages/flask_security/forms.py", line 47, in __call__
    self.message = get_message(self.message)[0]
  File "/usr/local/lib/python2.7/site-packages/flask_security/utils.py", line 333, in get_message
    return localize_callback(rv[0], **kwargs), rv[1]
  File "/usr/local/lib/python2.7/site-packages/werkzeug/local.py", line 375, in <lambda>
    __call__ = lambda x, *a, **kw: x._get_current_object()(*a, **kw)
  File "/usr/local/lib/python2.7/site-packages/flask_babelex/__init__.py", line 533, in gettext
    t = self.get_translations()
  File "/usr/local/lib/python2.7/site-packages/flask_babelex/__init__.py", line 519, in get_translations
    domain=self.domain)
  File "/usr/local/lib/python2.7/site-packages/babel/support.py", line 562, in load
    with open(filename, 'rb') as fp:
IOError: [Errno 2] No such file or directory: '/usr/local/lib/python2.7/site-packages/flask_security/translations/en_US.ISO8859-1/LC_MESSAGES/flask_security.mo'

尝试使用Google这个错误并没有发现任何相关的内容。好像我只需要一个文件。我不确定为什么修补内置方法会触发此事件,而修补自己的方法却不会触发该事件,但是如果我知道从哪里获取该文件,我会很乐意添加。我在烧瓶安全项目中寻找了它,但I don't see that it's there

0 个答案:

没有答案