kwork-api/tests/e2e/conftest.py
root 94978ccdf1 test: use session-scoped e2e_client fixture (login once per session)
- Add event_loop fixture with session scope
- Add e2e_client fixture that logs in ONCE for all catalog tests
- Add catalog_kwork_id fixture that fetches kwork ID ONCE
- All catalog tests now reuse the same authenticated client
- Reduces login calls from 10 to 1 per test session
2026-03-29 23:55:38 +00:00

96 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
E2E тесты для Kwork API.
Требуют реальных credentials и запускаются только локально.
"""
import asyncio
import os
from pathlib import Path
import pytest
from dotenv import load_dotenv
from kwork_api import KworkClient
# Загружаем .env
load_dotenv(Path(__file__).parent / ".env")
@pytest.fixture(scope="session")
def kwork_credentials():
"""Credentials для тестового аккаунта."""
return {
"username": os.getenv("KWORK_USERNAME"),
"password": os.getenv("KWORK_PASSWORD"),
}
@pytest.fixture(scope="session")
def require_credentials(kwork_credentials):
"""Пропускает тест если нет credentials."""
if not kwork_credentials["username"] or not kwork_credentials["password"]:
pytest.skip(
"E2E credentials not set. "
"Copy tests/e2e/.env.example to tests/e2e/.env and fill in credentials."
)
return kwork_credentials
@pytest.fixture(scope="session")
def event_loop():
"""Create session-scoped event loop for all E2E tests."""
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
@pytest.fixture(scope="session")
async def e2e_client(require_credentials):
"""
E2E клиент - логинится ОДИН РАЗ для всех тестов сессии.
Используется во всех тестах кроме test_auth.py (там тестируем сам логин).
"""
client = await KworkClient.login(
username=require_credentials["username"],
password=require_credentials["password"],
)
yield client
await client.close()
@pytest.fixture(scope="session")
async def catalog_kwork_id(e2e_client):
"""
Получить ID первого кворка из каталога.
Выполняется ОДИН РАЗ в начале сессии и переиспользуется.
"""
catalog = await e2e_client.catalog.get_list(page=1)
if len(catalog.kworks) > 0:
return catalog.kworks[0].id
return None
@pytest.fixture(scope="function")
def slowmo(request):
"""Задержка между тестами для rate limiting."""
slowmo = request.config.getoption("--slowmo", default=0)
if slowmo > 0:
import time
time.sleep(slowmo)
def pytest_configure(config):
"""Регистрация маркера e2e."""
config.addinivalue_line("markers", "e2e: mark test as end-to-end (requires credentials)")
def pytest_addoption(parser):
"""Добавляет опцию --slowmo."""
parser.addoption(
"--slowmo", type=float, default=0, help="Delay between tests in seconds (for rate limiting)"
)