Included pytest fixtures



Automatically used test fixture. Returns the application instance-under-test with a valid app context.


flask_unchained.pytest.maybe_inject_extensions_and_services(app, request)[source]

Automatically used test fixture. Allows for using services and extensions as if they were test fixtures:

def test_something(db, mail, security_service, user_manager):
    # assert important stuff

NOTE: This only works on tests themselves; it will not work on test fixtures



Yields an instance of FlaskCliRunner. Example usage:

from your_package.commands import some_command

def test_some_command(cli_runner):
    result = cli_runner.invoke(some_command)
    assert result.exit_code == 0
    assert result.output.strip() == 'output of some_command'



Yields an instance of HtmlTestClient. Example usage:

def test_some_view(client):
    r = client.get('some.endpoint')

    # r is an instance of :class:`HtmlTestResponse`
    assert r.status_code == 200
    assert 'The Page Title' in r.html



Yields an instance of ApiTestClient. Example usage:

def test_some_view(api_client):
    r = api_client.get('some.endpoint.returning.json')

    # r is an instance of :class:`ApiTestResponse`
    assert r.status_code == 200
    assert 'some_key' in r.json



Fixture to record which templates (if any) got rendered during a request. Example Usage:

def test_some_view(client, templates):
    r = client.get('some.endpoint')
    assert r.status_code == 200
    assert templates[0].template.name == 'some/template.html'
    assert templates[0].context.get('some_ctx_var') == 'expected value'


class flask_unchained.pytest.FlaskCliRunner(app, **kwargs)[source]

Extended from upstream to run commands within the Flask app context.

The CLI runner provides functionality to invoke a Click command line script for unit testing purposes in a isolated environment. This only works in single-threaded systems without any concurrency as it changes the global interpreter state.

  • charset – the character set for the input and output data. This is UTF-8 by default and should not be changed currently as the reporting to Click only works in Python 2 properly.

  • env – a dictionary with environment variables for overriding.

  • echo_stdin – if this is set to True, then reading from stdin writes to stdout. This is useful for showing examples in some circumstances. Note that regular prompts will automatically echo the input.

invoke(cli=None, args=None, **kwargs)[source]

Invokes a command in an isolated environment. The arguments are forwarded directly to the command line script, the extra keyword arguments are passed to the main() function of the command.

This returns a Result object.

  • cli – the command to invoke

  • args – the arguments to invoke

  • input – the input data for sys.stdin.

  • env – the environment overrides.

  • catch_exceptions – Whether to catch any other exceptions than SystemExit.

  • extra – the keyword arguments to pass to main().

  • color – whether the output should contain color codes. The application can still override this explicitly.


class flask_unchained.pytest.HtmlTestClient(*args, **kwargs)[source]

Like FlaskClient, except it supports passing an endpoint as the first argument directly to the HTTP get/post/etc methods (no need to use url_for, unless your URL rule has parameter names that conflict with the keyword arguments of EnvironBuilder). It also adds support for following redirects. Example usage:

def test_something(client: HtmlTestClient):
    r = client.get('site_controller.index')
    assert r.status_code == 200

Follow redirects on a response after inspecting it. Example usage:

def test_some_view(client):
    r = client.post('some.endpoint.that.redirects', data=data)
    assert r.status_code == 302
    assert r.path == url_for('some.endpoint')

    r = client.follow_redirects(r)
    assert r.status_code == 200


class flask_unchained.pytest.HtmlTestResponse(response=None, status=None, headers=None, mimetype=None, content_type=None, direct_passthrough=False)[source]

Like flask.wrappers.Response, except extended with methods for inspecting the parsed URL and automatically decoding the response to a string.

property scheme[source]

Returns the URL scheme specifier of the response’s url, eg http or https.

property netloc[source]

Returns the network location part the response’s url.

property path[source]

Returns the path part of the response’s url.

property params[source]

Returns the parameters for the last path element in the response’s url.

property query[source]

Returns the query component from the response’s url.

property fragment[source]

Returns the fragment identifier from the response’s url.

property html[source]

Returns the response’s data parsed to a string of html.


class flask_unchained.pytest.ApiTestClient(*args, **kwargs)[source]

Like HtmlTestClient except it supports automatic serialization to json of data, as well as setting the Accept and Content-Type headers to application/json.

open(*args, **kwargs)[source]

Takes the same arguments as the EnvironBuilder class with some additions: You can provide a EnvironBuilder or a WSGI environment as only argument instead of the EnvironBuilder arguments and two optional keyword arguments (as_tuple, buffered) that change the type of the return value or the way the application is executed.

Changed in version 0.5: If a dict is provided as file in the dict for the data parameter the content type has to be called content_type now instead of mimetype. This change was made for consistency with werkzeug.FileWrapper.

The follow_redirects parameter was added to open().

Additional parameters:

  • as_tuple – Returns a tuple in the form (environ, result)

  • buffered – Set this to True to buffer the application run. This will automatically close the application for you as well.

  • follow_redirects – Set this to True if the Client should follow HTTP redirects.


class flask_unchained.pytest.ApiTestResponse(response=None, status=None, headers=None, mimetype=None, content_type=None, direct_passthrough=False)[source]

Like HtmlTestResponse except it adds methods for automatically parsing the response data as json and retrieving errors from the response data.

property json[source]

Returns the response’s data parsed from json.

property errors[source]

If the response contains the key errors, return its value, otherwise returns an empty dictionary.


class flask_unchained.pytest.RenderedTemplate(template, context)

A namedtuple returned by the templates() fixture.

property context

Alias for field number 1

property template

Alias for field number 0