Ligare.web.testing.create_app

Fixtures for testing Ligare.web, Flask, and Connexion/OpenAPI applications.

Classes

ClientInjector(client, injector)

Flask test client and IoC container for the application created by PyTest fixtures.

CreateApp()

CreateFlaskApp()

CreateOpenAPIApp()

MockController(begin, end)

OpenAPIMockController(begin, end)

A fixture used to mock some internals of Connexion to enable GET requests in tests.

TestSessionMiddleware(app)

protocol Ligare.web.testing.create_app.AppGetter

A callable that instantiates a Flask application and returns the application with its IoC container.

Classes that implement this protocol must have the following methods / attributes:

__call__()

Call self as a function.

Return type:

CreateAppResult[TypeVar(T_app, bound= Flask | FlaskApp)]

class Ligare.web.testing.create_app.ClientInjector(client, injector)

Flask test client and IoC container for the application created by PyTest fixtures.

__init__(client, injector)
client: TypeVar(T_flask_client, bound= FlaskClient | TestClient)
injector: FlaskInjector
protocol Ligare.web.testing.create_app.ClientInjectorConfigurable

Get a Flask test client using the specified application configuration.

Parameters

configConfig

The custom application configuration used to instantiate the Flask app.

Returns

FlaskClientInjector[T_flask_client]

Classes that implement this protocol must have the following methods / attributes:

__call__(config, client_init_hook=None, app_init_hook=None)

Call self as a function.

Return type:

Generator[ClientInjector[TypeVar(T_flask_client, bound= FlaskClient | TestClient)], Any, None]

class Ligare.web.testing.create_app.CreateApp
app_name: str = 'test_app'
auto_mock_dependencies: bool = True
basic_config()
default_app_getter: Optional[AppGetter[TypeVar(T_app, bound= Flask | FlaskApp)]] = None
get_authenticated_request_context(app, user, mocker, roles=None)
mock_user(user, mocker, roles=None)
Return type:

MagicMock | AsyncMock | NonCallableMagicMock

openapi_config()
setup_method_fixture(mocker)
use_inmemory_database: bool = True
class Ligare.web.testing.create_app.CreateFlaskApp
flask_client(_get_basic_flask_app)
Return type:

ClientInjector[FlaskClient]

flask_client_configurable(mocker)
Return type:

ClientInjectorConfigurable[Flask, FlaskClient]

flask_request(flask_client)
Return type:

Generator[RequestContext, Any, None]

flask_request_configurable(flask_client_configurable)
Return type:

RequestConfigurable

class Ligare.web.testing.create_app.CreateOpenAPIApp
get_app(flask_app_getter)
openapi_client(request)
Return type:

ClientInjector[TestClient]

openapi_client_configurable(mocker)
Return type:

ClientInjectorConfigurable[FlaskApp, TestClient]

openapi_client_with_database(request)
Return type:

Generator[tuple[ClientInjector[TestClient], Connection], Any, None]

openapi_mock_controller(request, mocker)
openapi_request(openapi_client)
Return type:

Generator[RequestContext, Any, None]

openapi_request_configurable(openapi_client_configurable)
Return type:

RequestConfigurable

class Ligare.web.testing.create_app.MockController(begin, end)
begin: Callable[[], None]

Alias for field number 0

end: Callable[[], None]

Alias for field number 1

class Ligare.web.testing.create_app.OpenAPIMockController(begin: Callable[[], None], end: Callable[[], None])

A fixture used to mock some internals of Connexion to enable GET requests in tests.

An OpenAPI application should be created, and any bindings managed, before calling openapi_mock.begin(). Once begin() is called, changes to the application may result in undefined behavior.

openapi_mock.end() is called after the test finishes, and may also be explicitly called in the test.

Parameters

request_handlerCallable[[], Response]

A method used as the request handler for GET requests set to /. This parameter is set through parametrization, as shown in the example:

@pytest.mark.parametrize("openapi_mock", lambda: {}, indirect=["openapi_mock"])
def test_my_test(openapi_mock: OpenAPIMockController):
    ...

Returns

RequestContext

protocol Ligare.web.testing.create_app.RequestConfigurable

Get a Flask request context, creating a Flask test client that uses the specified application configuration and, optionally, specific request context arguments.

This creates the Flask test client using the ClientConfigurable fixture.

Parameters

configConfig

The custom application configuration used to instantiate the Flask app.

request_context_argsdict[Any, Any] | None, optional

The optional request context arguments set up in the request context. These may contain, for example, request headers, authentication credentials, etc.

Returns

RequestContext

Classes that implement this protocol must have the following methods / attributes:

__call__(config, request_context_args=None)

Call self as a function.

Return type:

RequestContext

class Ligare.web.testing.create_app.TestSessionMiddleware(app)
__init__(app)