Browse Source

Use the yield syntax in pytest's fixtures

pull/2311/head
bovarysme 8 years ago
parent
commit
5963cb5a51
  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 @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):

7
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 # make sure we're not leaking a request context since we are
# testing flask internally in debug mode in a few cases # testing flask internally in debug mode in a few cases
leaks = [] leaks = []
while flask._request_ctx_stack.top is not None: while flask._request_ctx_stack.top is not None:
leaks.append(flask._request_ctx_stack.pop()) leaks.append(flask._request_ctx_stack.pop())
assert leaks == [] assert leaks == []
request.addfinalizer(ensure_clean_request_context)
@pytest.fixture(params=(True, False)) @pytest.fixture(params=(True, False))

14
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) \ assert set(w.category for w in recwarn.list) \
<= set([ExtDeprecationWarning]) <= set([ExtDeprecationWarning])
recwarn.clear() recwarn.clear()
request.addfinalizer(inner)
@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,13 +57,12 @@ 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 from flask import ext
for key in ext.__dict__: for key in ext.__dict__:
assert '.' not in key assert '.' not in key
request.addfinalizer(teardown)
@pytest.fixture @pytest.fixture
def newext_simple(modules_tmpdir): def newext_simple(modules_tmpdir):

Loading…
Cancel
Save