Skip to content
Myr

Merge pull request #2592 from pallets/tests-env

Before adoption: this commit came in with the history the project adopted, so no landing records it.

reset standard os env after each test

authored by David Lord · committed by GitHub · parents f2cf4d0, 8bdf820 · Browse files · 2 files+43 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+43 added−18 removed

  • tests:+43 added−18 removed

2 files+43 added−18 removed

modified:tests/conftest.py+39 added-3 removed
4 unmodified lines
56 :copyright: (c) 2015 by the Flask Team, see AUTHORS for more details.7 :license: BSD, see LICENSE for more details.8"""9import flask9import gc10import os12import sys11import pkgutil14import pytest12import sys13import textwrap1415import pytest16from _pytest import monkeypatch1718import flask19from flask import Flask as _Flask202122@pytest.fixture(scope='session', autouse=True)23def _standard_os_environ():24 """Set up ``os.environ`` at the start of the test session to have25 standard values. Returns a list of operations that is used by26 :func:`._reset_os_environ` after each test.27 """28 mp = monkeypatch.MonkeyPatch()29 out = (30 (os.environ, 'FLASK_APP', monkeypatch.notset),31 (os.environ, 'FLASK_ENV', monkeypatch.notset),32 (os.environ, 'FLASK_DEBUG', monkeypatch.notset),33 (os.environ, 'FLASK_RUN_FROM_CLI', monkeypatch.notset),34 (os.environ, 'WERKZEUG_RUN_MAIN', monkeypatch.notset),35 )3637 for _, key, value in out:38 if value is monkeypatch.notset:39 mp.delenv(key, False)40 else:41 mp.setenv(key, value)4243 yield out44 mp.undo()454647@pytest.fixture(autouse=True)48def _reset_os_environ(monkeypatch, _standard_os_environ):49 """Reset ``os.environ`` to the standard environ after each test,50 in case a test changed something without cleaning up.51 """52 monkeypatch._setitem.extend(_standard_os_environ)535455class Flask(_Flask):56 testing = True57 secret_key = 'test key'58
146 unmodified lines
modified:tests/test_cli.py+4 added-15 removed
21 unmodified lines
22from _pytest.monkeypatch import notset23from click.testing import CliRunner2425from flask import Flask, current_app26from flask.cli import AppGroup, FlaskGroup, NoAppException, ScriptInfo, dotenv, \27 find_best_app, get_version, load_dotenv, locate_app, prepare_import, \26from flask.cli import (27 AppGroup, FlaskGroup, NoAppException, ScriptInfo, dotenv,28 find_best_app, get_version, load_dotenv, locate_app, prepare_import,29 with_appcontext30)3132cwd = os.getcwd()33test_path = os.path.abspath(os.path.join(34 os.path.dirname(__file__), 'test_apps'35))363736@pytest.fixture(autouse=True)37def manage_os_environ(monkeypatch):38 # can't use monkeypatch.delitem since we don't want to restore a value39 os.environ.pop('FLASK_APP', None)40 os.environ.pop('FLASK_DEBUG', None)41 # use monkeypatch internals to force-delete environ keys42 monkeypatch._setitem.extend((43 (os.environ, 'FLASK_APP', notset),44 (os.environ, 'FLASK_DEBUG', notset),45 (os.environ, 'FLASK_RUN_FROM_CLI', notset),46 ))474838@pytest.fixture39def runner():40 return CliRunner()41
425 unmodified lines