Unit testing Flask Babel translations Unit testing Flask Babel translations flask flask

Unit testing Flask Babel translations


The code you have shown seems to work for me. Please see here complete example based on your description: https://github.com/loomchild/flask_babel_test. When I run ./flask_babel_test_test.py both tests pass.

Could you provide complete source code that allows to reproduce the problem?

Currently I can imagine the following solutions (both of them are present in commented-out sections in the example code linked above):

  1. There is some caching involved - try to execute flask.ext.babel.refresh() after updating default locale during the test and see if it helps.

  2. If you retrieve browser language automatically from Accept-Language HTTP header using localeselector, for example like this:

    @babel.localeselectordef get_locale():    translations = [str(translation) for translation in babel.list_translations()]    return request.accept_languages.best_match(translations)

    Then instead of modifying app config during the test, specify a header:

    rv = app.test_client().get('/', headers=[("Accept-Language", "pt_BR")])
  3. Flask-Babel is not able to find the translations directory during testing - try moving your test Python script or translations directory and see if it helps. I don't know how to customize the location of this directory in Flask-Babel, perhaps creating a symlink or running test from project root directory can help.