kwork-api/tests/e2e/conftest.py

97 lines
2.8 KiB
Python

"""
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")
# Отключаем auto mode для pytest-asyncio чтобы использовать один event loop
pytest_plugins = ('pytest_asyncio',)
@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="module")
def client(require_credentials):
"""
E2E клиент для всех тестов в модуле.
Авторизуется один раз и переиспользуется во всех тестах модуля.
"""
async def create_client():
return await KworkClient.login(
username=require_credentials["username"],
password=require_credentials["password"],
)
client = asyncio.run(create_client())
yield client
asyncio.run(client.close())
@pytest.fixture(scope="module")
def catalog_kwork_id(client):
"""
Получить ID первого кворка из каталога для тестов.
Переиспользуется во всех тестах модуля.
"""
async def get_first_kwork():
catalog = await client.catalog.get_list(page=1)
if len(catalog.kworks) > 0:
return catalog.kworks[0].id
return None
return asyncio.run(get_first_kwork())
@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)"
)