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
Provenance
- Commit
2949306- Tree
abb80a0- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
f2cf4d0- Parent 2
8bdf820
2 files changed+43 added−18 removed
modified:tests/conftest.py+39 added-3 removed
4 unmodified lines5 6 :copyright: (c) 2015 by the Flask Team, see AUTHORS for more details.7 :license: BSD, see LICENSE for more details.8 """9-import flask9 import gc10 import os12-import sys11 import pkgutil14-import pytest12+import sys13 import textwrap14+15+import pytest16+from _pytest import monkeypatch17+18+import flask19 from flask import Flask as _Flask20 21 22+@pytest.fixture(scope='session', autouse=True)23+def _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+ )36+37+ for _, key, value in out:38+ if value is monkeypatch.notset:39+ mp.delenv(key, False)40+ else:41+ mp.setenv(key, value)42+43+ yield out44+ mp.undo()45+46+47+@pytest.fixture(autouse=True)48+def _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)53+54+55 class Flask(_Flask):56 testing = True57 secret_key = 'test key'58 146 unmodified linesmodified:tests/test_cli.py+4 added-15 removed
21 unmodified lines22 from _pytest.monkeypatch import notset23 from click.testing import CliRunner24 25 from flask import Flask, current_app26-from flask.cli import AppGroup, FlaskGroup, NoAppException, ScriptInfo, dotenv, \27- find_best_app, get_version, load_dotenv, locate_app, prepare_import, \26+from flask.cli import (27+ AppGroup, FlaskGroup, NoAppException, ScriptInfo, dotenv,28+ find_best_app, get_version, load_dotenv, locate_app, prepare_import,29 with_appcontext30+)31 32 cwd = os.getcwd()33 test_path = os.path.abspath(os.path.join(34 os.path.dirname(__file__), 'test_apps'35 ))36 37 36-@pytest.fixture(autouse=True)37-def 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- ))47-48-38 @pytest.fixture39 def runner():40 return CliRunner()41 425 unmodified lines