conftest.py
This file
Path
examples/tutorial/tests/conftest.py
62 lines · 1.4 KiB · Python
Last changed by
Merge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697
History of this fileTake it
Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 1–4why, one step back1import os2import tempfile34import pytestMerge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697
line 5why, one step back5Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
line 6why, one step back6from flaskr import create_appMerge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697
lines 7–8why, one step back7from flaskr.db import get_db8from flaskr.db import init_dbMerge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 9–10why, one step back910# read in SQL for populating test dataMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 11–12why, one step back11with open(os.path.join(os.path.dirname(__file__), "data.sql"), "rb") as f:12 _data_sql = f.read().decode("utf8")Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 13–20why, one step back131415@pytest.fixture16def app():17 """Create and configure a new app instance for each test."""18 # create a temporary file to isolate the database for each test19 db_fd, db_path = tempfile.mkstemp()20 # create the app with common test configMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 21why, one step back21 app = create_app({"TESTING": True, "DATABASE": db_path})Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 22–50why, one step back2223 # create the database and load test data24 with app.app_context():25 init_db()26 get_db().executescript(_data_sql)2728 yield app2930 # close and remove the temporary database31 os.close(db_fd)32 os.unlink(db_path)333435@pytest.fixture36def client(app):37 """A test client for the app."""38 return app.test_client()394041@pytest.fixture42def runner(app):43 """A test runner for the app's Click commands."""44 return app.test_cli_runner()454647class AuthActions(object):48 def __init__(self, client):49 self._client = client50Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 51why, one step back51 def login(self, username="test", password="test"):Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
line 52why, one step back52 return self._client.post(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 53why, one step back53 "/auth/login", data={"username": username, "password": password}Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 54–56why, one step back54 )5556 def logout(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 57why, one step back57 return self._client.get("/auth/logout")Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 58–62why, one step back585960@pytest.fixture61def auth(client):62 return AuthActions(client)