Skip to content
Myr

conftest.py

This file

Path

examples/tutorial/tests/conftest.py

62 lines · 1.4 KiB · Python

Last changed by

Not known.

History of this file

Take it

1import os2import tempfile34import pytest56from flaskr import create_app7from flaskr.db import get_db8from flaskr.db import init_db910# read in SQL for populating test data11with open(os.path.join(os.path.dirname(__file__), "data.sql"), "rb") as f:12    _data_sql = f.read().decode("utf8")131415@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 config21    app = create_app({"TESTING": True, "DATABASE": db_path})2223    # 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:48    def __init__(self, client):49        self._client = client5051    def login(self, username="test", password="test"):52        return self._client.post(53            "/auth/login", data={"username": username, "password": password}54        )5556    def logout(self):57        return self._client.get("/auth/logout")585960@pytest.fixture61def auth(client):62    return AuthActions(client)