Merge pull request #2311 from bovarysme/use-yield-syntax
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Use the yield syntax in pytest's fixtures
authored by David Lord · committed by GitHub · parents e206764, 5963cb5 · Browse files · 3 files·+25 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+25 added−28 removed
- tests:+25 added−28 removed
Provenance
- Commit
a037762- Tree
78da07b- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
e206764- Parent 2
5963cb5
3 files changed+25 added−28 removed
modified:examples/minitwit/tests/test_minitwit.py+5 added-7 removed
13 unmodified lines14 from minitwit import minitwit15 16 17 @pytest.fixture18-def client(request):18+def client():19 db_fd, minitwit.app.config['DATABASE'] = tempfile.mkstemp()20 client = minitwit.app.test_client()21 with minitwit.app.app_context():22 minitwit.init_db()23 24- def teardown():25- """Get rid of the database again after each test."""26- os.close(db_fd)27- os.unlink(minitwit.app.config['DATABASE'])28- request.addfinalizer(teardown)29- return client24+ yield client25 26+ os.close(db_fd)27+ os.unlink(minitwit.app.config['DATABASE'])28 29+30 def register(client, username, password, password2=None, email=None):31 """Helper function to register a user"""32 if password2 is None:33 password2 = password116 unmodified linesmodified:tests/conftest.py+10 added-9 removed
20 unmodified lines21 os.path.abspath(os.path.join(22 os.path.dirname(__file__), 'test_apps'))23 )24 25+26 @pytest.fixture(autouse=True)26-def leak_detector(request):27- def ensure_clean_request_context():28- # make sure we're not leaking a request context since we are29- # testing flask internally in debug mode in a few cases30- leaks = []31- while flask._request_ctx_stack.top is not None:32- leaks.append(flask._request_ctx_stack.pop())33- assert leaks == []34- request.addfinalizer(ensure_clean_request_context)27+def leak_detector():28+ yield29 30+ # make sure we're not leaking a request context since we are31+ # testing flask internally in debug mode in a few cases32+ leaks = []33+ while flask._request_ctx_stack.top is not None:34+ leaks.append(flask._request_ctx_stack.pop())35+ assert leaks == []36 37+38 @pytest.fixture(params=(True, False))39 def limit_loader(request, monkeypatch):40 """Patch pkgutil.get_loader to give loader without get_filename or archive.41 94 unmodified linesmodified:tests/test_ext.py+10 added-12 removed
19 unmodified lines20 from flask._compat import PY221 22 23 @pytest.fixture(autouse=True)24-def disable_extwarnings(request, recwarn):24+def disable_extwarnings(recwarn):25 from flask.exthook import ExtDeprecationWarning26 27- def inner():28- assert set(w.category for w in recwarn.list) \29- <= set([ExtDeprecationWarning])30- recwarn.clear()27+ yield28 32- request.addfinalizer(inner)29+ assert set(w.category for w in recwarn.list) \30+ <= set([ExtDeprecationWarning])31+ recwarn.clear()32 33 34 @pytest.fixture(autouse=True)36-def importhook_setup(monkeypatch, request):35+def importhook_setup(monkeypatch):36 # we clear this out for various reasons. The most important one is37 # that a real flaskext could be in there which would disable our38 # fake package. Secondly we want to make sure that the flaskext39 # import hook does not break on reloading.16 unmodified lines56 cls.__name__ == 'ExtensionImporter':57 import_hooks += 158 assert import_hooks == 159 61- def teardown():62- from flask import ext63- for key in ext.__dict__:64- assert '.' not in key60+ yield61 66- request.addfinalizer(teardown)62+ from flask import ext63+ for key in ext.__dict__:64+ assert '.' not in key65 66 67 @pytest.fixture68 def newext_simple(modules_tmpdir):129 unmodified lines