DRYing up the test suite using pytest fixtures (#2306)

* add fixtures to conftest.py

* use fixtures in test_appctx.py

* use fixtures in test_blueprints.py

* use fixtures in test_depreciations.py

* use fixtures in test_regressions.py

* use fixtures in test_reqctx.py

* use fixtures in test_templating.py

* use fixtures in test_user_error_handler.py

* use fixtures in test_views.py

* use fixtures in test_basics.py

* use fixtures in test_helpers.py

* use fixtures in test_testing.py

* update conftest.py

* make docstrings  PEP-257 compliant

* cleanup

* switch dictonary format

* use pytest parameterization for test_json_as_unicode
This commit is contained in:
Christian Stade-Schuldt 2017-05-23 15:18:39 -07:00 committed by David Lord
parent 81c2440a05
commit 5b0b9717da
12 changed files with 1004 additions and 979 deletions

View file

@ -13,6 +13,40 @@ import sys
import pkgutil
import pytest
import textwrap
from flask import Flask as _Flask
class Flask(_Flask):
testing = True
secret_key = __name__
def make_response(self, rv):
if rv is None:
rv = ''
return super(Flask, self).make_response(rv)
@pytest.fixture
def app():
app = Flask(__name__)
return app
@pytest.fixture
def app_ctx(app):
with app.app_context() as ctx:
yield ctx
@pytest.fixture
def req_ctx(app):
with app.test_request_context() as ctx:
yield ctx
@pytest.fixture
def client(app):
return app.test_client()
@pytest.fixture
@ -63,12 +97,13 @@ def limit_loader(request, monkeypatch):
def get_loader(*args, **kwargs):
return LimitedLoader(old_get_loader(*args, **kwargs))
monkeypatch.setattr(pkgutil, 'get_loader', get_loader)
@pytest.fixture
def modules_tmpdir(tmpdir, monkeypatch):
'''A tmpdir added to sys.path'''
"""A tmpdir added to sys.path."""
rv = tmpdir.mkdir('modules_tmpdir')
monkeypatch.syspath_prepend(str(rv))
return rv
@ -82,10 +117,10 @@ def modules_tmpdir_prefix(modules_tmpdir, monkeypatch):
@pytest.fixture
def site_packages(modules_tmpdir, monkeypatch):
'''Create a fake site-packages'''
"""Create a fake site-packages."""
rv = modules_tmpdir \
.mkdir('lib')\
.mkdir('python{x[0]}.{x[1]}'.format(x=sys.version_info))\
.mkdir('lib') \
.mkdir('python{x[0]}.{x[1]}'.format(x=sys.version_info)) \
.mkdir('site-packages')
monkeypatch.syspath_prepend(str(rv))
return rv
@ -93,8 +128,9 @@ def site_packages(modules_tmpdir, monkeypatch):
@pytest.fixture
def install_egg(modules_tmpdir, monkeypatch):
'''Generate egg from package name inside base and put the egg into
sys.path'''
"""Generate egg from package name inside base and put the egg into
sys.path."""
def inner(name, base=modules_tmpdir):
if not isinstance(name, str):
raise ValueError(name)
@ -118,6 +154,7 @@ def install_egg(modules_tmpdir, monkeypatch):
egg_path, = modules_tmpdir.join('dist/').listdir()
monkeypatch.syspath_prepend(str(egg_path))
return egg_path
return inner
@ -125,6 +162,7 @@ def install_egg(modules_tmpdir, monkeypatch):
def purge_module(request):
def inner(name):
request.addfinalizer(lambda: sys.modules.pop(name, None))
return inner
@ -132,4 +170,4 @@ def purge_module(request):
def catch_deprecation_warnings(recwarn):
yield
gc.collect()
assert not recwarn.list
assert not recwarn.list, '\n'.join(str(w.message) for w in recwarn.list)