Browse Source

Use the yield syntax in pytest's fixtures

pull/2311/head
bovarysme 8 years ago
parent
commit
5963cb5a51
  1. 12
      examples/minitwit/tests/test_minitwit.py
  2. 19
      tests/conftest.py
  3. 22
      tests/test_ext.py

12
examples/minitwit/tests/test_minitwit.py

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

19
tests/conftest.py

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

22
tests/test_ext.py

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

Loading…
Cancel
Save