Browse Source

Merge pull request #2592 from pallets/tests-env

reset standard os env after each test
pull/2593/head
David Lord 7 years ago committed by GitHub
parent
commit
2949306b7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 42
      tests/conftest.py
  2. 19
      tests/test_cli.py

42
tests/conftest.py

@ -6,16 +6,52 @@
:copyright: (c) 2015 by the Flask Team, see AUTHORS for more details. :copyright: (c) 2015 by the Flask Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details. :license: BSD, see LICENSE for more details.
""" """
import flask
import gc import gc
import os import os
import sys
import pkgutil import pkgutil
import pytest import sys
import textwrap import textwrap
import pytest
from _pytest import monkeypatch
import flask
from flask import Flask as _Flask from flask import Flask as _Flask
@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()
out = (
(os.environ, 'FLASK_APP', monkeypatch.notset),
(os.environ, 'FLASK_ENV', 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.
"""
monkeypatch._setitem.extend(_standard_os_environ)
class Flask(_Flask): class Flask(_Flask):
testing = True testing = True
secret_key = 'test key' secret_key = 'test key'

19
tests/test_cli.py

@ -23,9 +23,11 @@ from _pytest.monkeypatch import notset
from click.testing import CliRunner from click.testing import CliRunner
from flask import Flask, current_app from flask import Flask, current_app
from flask.cli import AppGroup, FlaskGroup, NoAppException, ScriptInfo, dotenv, \ from flask.cli import (
find_best_app, get_version, load_dotenv, locate_app, prepare_import, \ AppGroup, FlaskGroup, NoAppException, ScriptInfo, dotenv,
find_best_app, get_version, load_dotenv, locate_app, prepare_import,
with_appcontext with_appcontext
)
cwd = os.getcwd() cwd = os.getcwd()
test_path = os.path.abspath(os.path.join( test_path = os.path.abspath(os.path.join(
@ -33,19 +35,6 @@ test_path = os.path.abspath(os.path.join(
)) ))
@pytest.fixture(autouse=True)
def manage_os_environ(monkeypatch):
# can't use monkeypatch.delitem since we don't want to restore a value
os.environ.pop('FLASK_APP', None)
os.environ.pop('FLASK_DEBUG', None)
# use monkeypatch internals to force-delete environ keys
monkeypatch._setitem.extend((
(os.environ, 'FLASK_APP', notset),
(os.environ, 'FLASK_DEBUG', notset),
(os.environ, 'FLASK_RUN_FROM_CLI', notset),
))
@pytest.fixture @pytest.fixture
def runner(): def runner():
return CliRunner() return CliRunner()

Loading…
Cancel
Save