Skip to content
Myr

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 file

Take it

1import os2import tempfile34import pytest5from flaskr import create_app6from flaskr.db import get_db, init_db78# read in SQL for populating test data9with open(os.path.join(os.path.dirname(__file__), "data.sql"), "rb") as f:10    _data_sql = f.read().decode("utf8")111213@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 config19    app = create_app({"TESTING": True, "DATABASE": db_path})2021    # 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 = client4849    def login(self, username="test", password="test"):50        return self._client.post(51            "/auth/login", data={"username": username, "password": password}52        )5354    def logout(self):55        return self._client.get("/auth/logout")565758@pytest.fixture59def auth(client):60    return AuthActions(client)