Skip to content

doctest causes a working out of context RuntimeError with request #2485

@alexprengere

Description

@alexprengere

Migrating from Flask 2.1 to 2.2 failed a few unit tests of mine. It looks like we can no longer import flask.request top level in a test file (reproduced across Python 3.7, 3.8, 3.9, 3.10):

from flask import Flask
from flask import request

def test_something():
    assert True

This gives a RuntimeError:

E   RuntimeError: Working outside of request context.
E
E   This typically means that you attempted to use functionality that needed
E   an active HTTP request. Consult the documentation on testing for
E   information about how to avoid this problem.

On my side the fix was simple: importing flask.request outside of the global scope, just when I actually need it (my actual tests create small Flask apps to test a client).

Edit: forgot to mention that this only occurs when pytest is run with --doctest-modules

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions