From 27060a70faa23e05f41f56f7c4fabb3df8f3152f Mon Sep 17 00:00:00 2001 From: "mendral-app[bot]" <233154221+mendral-app[bot]@users.noreply.github.com> Date: Wed, 10 Jun 2026 12:50:49 -0700 Subject: [PATCH] fix: disable Sentry SDK during test execution Add _is_test_environment() guard in init_sentry() that checks for PYTEST_CURRENT_TEST env var or pytest in sys.modules. This prevents test runs from reporting mock-derived exceptions to the production Sentry project. --- src/blaxel/core/common/sentry.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/blaxel/core/common/sentry.py b/src/blaxel/core/common/sentry.py index d2538820..02d0074d 100644 --- a/src/blaxel/core/common/sentry.py +++ b/src/blaxel/core/common/sentry.py @@ -1,6 +1,7 @@ import atexit import json import logging +import os import sys import threading import traceback @@ -227,10 +228,25 @@ def _trace_blaxel_exceptions(frame, event, arg): return _trace_blaxel_exceptions +def _is_test_environment() -> bool: + """Detect if the code is running inside a test framework.""" + # pytest sets this env var for each running test + if os.environ.get("PYTEST_CURRENT_TEST"): + return True + # Check if pytest has been imported (test session in progress) + if "pytest" in sys.modules: + return True + return False + + def init_sentry() -> None: """Initialize the lightweight Sentry client for SDK error tracking.""" global _sentry_initialized, _sentry_config, _handlers_registered try: + # Never report to Sentry during test execution + if _is_test_environment(): + return + dsn = settings.sentry_dsn if not dsn: return