Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Unreleased
it's disabled in config. Previously, only disabling worked. :issue:`5916`
- ``Flask.select_jinja_autoescape`` uses case-insensitive comparison instead
of only lower case file extensions. :pr:`6012`
- Fix compatibility with pytest-9.1 by removing use of private API. :issue:`6071`


Version 3.1.3
Expand Down
43 changes: 13 additions & 30 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,26 @@
import sys

import pytest
from _pytest import monkeypatch

from flask import Flask
from flask.globals import app_ctx as _app_ctx


@pytest.fixture(scope="session", autouse=True)
def _standard_os_environ():
"""Set up ``os.environ`` at the start of the test session to have
standard values. Returns a list of operations that is used by
:func:`._reset_os_environ` after each test.
"""
mp = monkeypatch.MonkeyPatch()

@RonnyPfannschmidt RonnyPfannschmidt Jul 14, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actualy keep this fixture and use

with pytest.MonkeyPatch.context() as mp:
...
yield ....

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry; actually, does that change anything? I mean, either way we need a function scope fixture, and my understanding is that it will undo the changes just the same.

out = (
(os.environ, "FLASK_ENV_FILE", monkeypatch.notset),
(os.environ, "FLASK_APP", monkeypatch.notset),
(os.environ, "FLASK_DEBUG", monkeypatch.notset),
(os.environ, "FLASK_RUN_FROM_CLI", monkeypatch.notset),
(os.environ, "WERKZEUG_RUN_MAIN", monkeypatch.notset),
)

for _, key, value in out:
if value is monkeypatch.notset:
mp.delenv(key, False)
else:
mp.setenv(key, value)

yield out
mp.undo()


@pytest.fixture(autouse=True)
def _reset_os_environ(monkeypatch, _standard_os_environ):
"""Reset ``os.environ`` to the standard environ after each test,
in case a test changed something without cleaning up.
def _standard_os_environ(monkeypatch):
"""Set up ``os.environ`` at the start of every test to have
standard values.
"""
monkeypatch._setitem.extend(_standard_os_environ)
for key in (
"FLASK_ENV_FILE",
"FLASK_APP",
"FLASK_DEBUG",
"FLASK_RUN_FROM_CLI",
"WERKZEUG_RUN_MAIN",
):
monkeypatch.delenv(key, False)

yield


@pytest.fixture
Expand Down
8 changes: 4 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import click
import pytest
from _pytest.monkeypatch import notset
from click.testing import CliRunner

from flask import Blueprint
Expand Down Expand Up @@ -535,9 +534,8 @@ def dotenv_not_available():

@need_dotenv
def test_load_dotenv(monkeypatch):
# can't use monkeypatch.delitem since the keys don't exist yet
for item in ("FOO", "BAR", "SPAM", "HAM"):
monkeypatch._setitem.append((os.environ, item, notset))
monkeypatch.delenv(item, False)

monkeypatch.setenv("EGGS", "3")
monkeypatch.chdir(test_path)
Expand All @@ -560,14 +558,15 @@ def test_load_dotenv(monkeypatch):
@need_dotenv
def test_dotenv_path(monkeypatch):
for item in ("FOO", "BAR", "EGGS"):
monkeypatch._setitem.append((os.environ, item, notset))
monkeypatch.delenv(item, False)

load_dotenv(test_path / ".flaskenv")
assert Path.cwd() == cwd
assert "FOO" in os.environ


def test_dotenv_optional(monkeypatch):
monkeypatch.delenv("FOO", False)
monkeypatch.setitem(sys.modules, "dotenv", None)
monkeypatch.chdir(test_path)
load_dotenv()
Expand All @@ -576,6 +575,7 @@ def test_dotenv_optional(monkeypatch):

@need_dotenv
def test_disable_dotenv_from_env(monkeypatch, runner):
monkeypatch.delenv("FOO", False)
monkeypatch.chdir(test_path)
monkeypatch.setitem(os.environ, "FLASK_SKIP_DOTENV", "1")
runner.invoke(FlaskGroup())
Expand Down
Loading