Skip to content

Change in unit test behavior when using test_client breaks makes tests less realistic #214

Description

@jdimmerman

As of flask_babel 3.0.0, flask babel locale context is no longer reset between flask test_client requests. Consider the following unit test and config:

Test app has this locale_selector:

from flask import has_request_context, request

def locale_selector():
    if has_request_context():
        return request.accept_languages.best_match(LanguageType)
    return None

and then the test (assuming some basic flask app):

from flask_babel import get_locale

def test_get_locale(app):
    app.test_client.get(url_for("some_blueprint.some_route"), headers={"Accept-Language": "en"})
    assert str(get_locale()) == "en"

    app.test_client.get(url_for("some_blueprint.some_route"), headers={"Accept-Language": "es"})
    assert str(get_locale()) == "es"

This test succeeded in 2.x because get_locale() would call locale_selector on each test. In 3.0.0, the locale remains cached after the first request is made, causing the second assertion to fail.

To make the second test succeed, we can use refresh:

from flask_babel import get_locale, refresh

def test_get_locale(app):
    app.test_client.get(url_for("some_blueprint.some_route"), headers={"Accept-Language": "en"})
    assert str(get_locale()) == "en"

    refresh()

    app.test_client.get(url_for("some_blueprint.some_route"), headers={"Accept-Language": "es"})
    assert str(get_locale()) == "es"

However, this makes the tests more poorly reflect reality.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions