/
marsela13
/
QR_Code_Generator
Обзор
Документация
Войти
/
marsela13
/
QR_Code_Generator
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
tests/conftest.py
75 строк
2 KB
marsela13
create: conftest.py, test_qr_service.py, test_routes.py, __init__.py
28 апр 2026, 20:29
Верифицирован
28 апр 2026, 20:29
4616371
Код
Авторство
О чём код?
""" pytest fixtures shared across all test modules. """ import pytest from app import create_app from app.database import db as _db from app.models import User from app.auth_utils import hash_password @pytest.fixture(scope="session") def app(): """Create a Flask application configured for testing.""" application = create_app("testing") return application @pytest.fixture(scope="session") def _db_setup(app): """Create all tables once per test session.""" with app.app_context(): _db.create_all() yield _db _db.drop_all() @pytest.fixture(autouse=True) def db_session(app, _db_setup): """ Wrap each test in a transaction that is rolled back afterwards, keeping tests isolated without recreating the schema. """ with app.app_context(): connection = _db_setup.engine.connect() transaction = connection.begin() _db_setup.session.bind = connection yield _db_setup _db_setup.session.remove() transaction.rollback() connection.close() @pytest.fixture def client(app): """Flask test client.""" return app.test_client() @pytest.fixture def registered_user(db_session, app): """Create and persist a test user, return the User instance.""" with app.app_context(): user = User( username="testuser", password_hash=hash_password("password123"), ) db_session.session.add(user) db_session.session.commit() db_session.session.refresh(user) return user @pytest.fixture def auth_client(client, registered_user, app): """A test client that is already logged in as *registered_user*.""" with app.app_context(): client.post( "/login", data={"username": "testuser", "password": "password123"}, follow_redirects=True, ) return client