Noob_test/Ticket/features/environment.py

42 lines
1.8 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.

from __future__ import annotations
import os
import traceback
from typing import Any, Callable
import allure # pyright: ignore[reportMissingImports]
from allure_commons.types import AttachmentType # pyright: ignore[reportMissingImports]
def before_scenario(context: Any, scenario: Any) -> None: # noqa: ARG001
# behave context не типизирован, поэтому сохраняем список без аннотации справа (pyright ругается).
context._cleanup_fns = [] # type: ignore[attr-defined]
# GraphQL endpoint выбирается в worklib.graphql_client.execute_graphql:
# - явный аргумент graphql_url
# - env GRAPHQL_URL
# - DEFAULT_GRAPHQL_URL
#
# Для Mockoon (proxy или чистый mock) достаточно перед запуском тестов выставить:
# GRAPHQL_URL=http://localhost:8080/graphql
#
# Если пользователь выставил env USE_MOCKOON=1, аккуратно подменим GRAPHQL_URL,
# но не трогаем его, если он уже задан явно.
if os.getenv("USE_MOCKOON") in {"1", "true", "True"} and not os.getenv("GRAPHQL_URL"):
os.environ["GRAPHQL_URL"] = "http://localhost:8081/graphql"
context.graphql_url = os.getenv("GRAPHQL_URL")
def after_scenario(context: Any, scenario: Any) -> None: # noqa: ARG001
cleanup_fns: list[Callable[[], None]] = getattr(context, "_cleanup_fns", [])
while cleanup_fns:
fn = cleanup_fns.pop()
try:
with allure.step(f"Cleanup: {getattr(fn, '__name__', 'cleanup')}"):
fn()
except Exception:
allure.attach(
traceback.format_exc(),
name="Cleanup error",
attachment_type=AttachmentType.TEXT,
)