conftest.py
This file
Path
examples/tutorial/tests/conftest.py
60 lines · 1.4 KiB · Python
Last changed by
Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
History of this fileTake it
Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 1–8why, one step back1import os2import tempfile34import pytest5from flaskr import create_app6from flaskr.db import get_db, init_db78# read in SQL for populating test dataMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 9–10why, one step back9with open(os.path.join(os.path.dirname(__file__), "data.sql"), "rb") as f:10 _data_sql = f.read().decode("utf8")Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 11–18why, one step back111213@pytest.fixture14def app():15 """Create and configure a new app instance for each test."""16 # create a temporary file to isolate the database for each test17 db_fd, db_path = tempfile.mkstemp()18 # create the app with common test configMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 19why, one step back19 app = create_app({"TESTING": True, "DATABASE": db_path})Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 20–48why, one step back2021 # create the database and load test data22 with app.app_context():23 init_db()24 get_db().executescript(_data_sql)2526 yield app2728 # close and remove the temporary database29 os.close(db_fd)30 os.unlink(db_path)313233@pytest.fixture34def client(app):35 """A test client for the app."""36 return app.test_client()373839@pytest.fixture40def runner(app):41 """A test runner for the app's Click commands."""42 return app.test_cli_runner()434445class AuthActions(object):46 def __init__(self, client):47 self._client = client48Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 49why, one step back49 def login(self, username="test", password="test"):Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
line 50why, one step back50 return self._client.post(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 51why, one step back51 "/auth/login", data={"username": username, "password": password}Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 52–54why, one step back52 )5354 def logout(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 55why, one step back55 return self._client.get("/auth/logout")Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 56–60why, one step back565758@pytest.fixture59def auth(client):60 return AuthActions(client)