Skip to content
Myr

conftest.py

This file

Path

examples/tutorial/tests/conftest.py

64 lines · 1.4 KiB · Python

Last changed by

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

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({20        'TESTING': True,21        'DATABASE': db_path,22    })2324    # create the database and load test data25    with app.app_context():26        init_db()27        get_db().executescript(_data_sql)2829    yield app3031    # close and remove the temporary database32    os.close(db_fd)33    os.unlink(db_path)343536@pytest.fixture37def client(app):38    """A test client for the app."""39    return app.test_client()404142@pytest.fixture43def runner(app):44    """A test runner for the app's Click commands."""45    return app.test_cli_runner()464748class AuthActions(object):49    def __init__(self, client):50        self._client = client5152    def login(self, username='test', password='test'):53        return self._client.post(54            '/auth/login',55            data={'username': username, 'password': password}56        )5758    def logout(self):59        return self._client.get('/auth/logout')606162@pytest.fixture63def auth(client):64    return AuthActions(client)