Я пытаюсь включить насмешку и исправления в тесты приложения Flask.Я использую Python 2.7, unittest в стандартной библиотеке, и я установил пакет mock;
Сейчас я пытаюсь пропатчить 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 не приводит к появлению чего-либо значимого.Кажется, мне просто нужен файл.Я не уверен, почему исправление встроенного метода вызывает это событие, а исправление моего собственного - нет, но я бы с радостью добавил этот файл, если бы знал, где его взять.Я искал это в проекте безопасности колбы, но Я не вижу, что это там .