Browse Source

Merge pull request #2311 from bovarysme/use-yield-syntax

Use the yield syntax in pytest's fixtures
pull/921/merge
David Lord 8 years ago committed by GitHub
parent
commit
a037762781
  1. 8
      examples/minitwit/tests/test_minitwit.py
  2. 7
      tests/conftest.py
  3. 14
      tests/test_ext.py

8
examples/minitwit/tests/test_minitwit.py

@ -15,18 +15,16 @@ from minitwit import minitwit
@pytest.fixture
def client(request):
def client():
db_fd, minitwit.app.config['DATABASE'] = tempfile.mkstemp()
client = minitwit.app.test_client()
with minitwit.app.app_context():
minitwit.init_db()
def teardown():
"""Get rid of the database again after each test."""
yield client
os.close(db_fd)
os.unlink(minitwit.app.config['DATABASE'])
request.addfinalizer(teardown)
return client
def register(client, username, password, password2=None, email=None):

7
tests/conftest.py

@ -22,16 +22,17 @@ def test_apps(monkeypatch):
os.path.dirname(__file__), 'test_apps'))
)
@pytest.fixture(autouse=True)
def leak_detector(request):
def ensure_clean_request_context():
def leak_detector():
yield
# make sure we're not leaking a request context since we are
# testing flask internally in debug mode in a few cases
leaks = []
while flask._request_ctx_stack.top is not None:
leaks.append(flask._request_ctx_stack.pop())
assert leaks == []
request.addfinalizer(ensure_clean_request_context)
@pytest.fixture(params=(True, False))

14
tests/test_ext.py

@ -21,19 +21,18 @@ from flask._compat import PY2
@pytest.fixture(autouse=True)
def disable_extwarnings(request, recwarn):
def disable_extwarnings(recwarn):
from flask.exthook import ExtDeprecationWarning
def inner():
yield
assert set(w.category for w in recwarn.list) \
<= set([ExtDeprecationWarning])
recwarn.clear()
request.addfinalizer(inner)
@pytest.fixture(autouse=True)
def importhook_setup(monkeypatch, request):
def importhook_setup(monkeypatch):
# we clear this out for various reasons. The most important one is
# that a real flaskext could be in there which would disable our
# fake package. Secondly we want to make sure that the flaskext
@ -58,13 +57,12 @@ def importhook_setup(monkeypatch, request):
import_hooks += 1
assert import_hooks == 1
def teardown():
yield
from flask import ext
for key in ext.__dict__:
assert '.' not in key
request.addfinalizer(teardown)
@pytest.fixture
def newext_simple(modules_tmpdir):

Loading…
Cancel
Save