From d8e8d3c4818f1a020d958b88981b56634b544143 Mon Sep 17 00:00:00 2001 From: Bruno Domingues Date: Fri, 31 Oct 2025 17:21:27 -0300 Subject: [PATCH] SIENTIAPDE-1307: Fix PytestUnraisableExceptionWarning in activity and experiment tracking tests by controlling exception raising in __del__ mocks. --- tests/activities/test_activities.py | 15 ++++++++++++--- tests/activities/test_experiment_tracking.py | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/tests/activities/test_activities.py b/tests/activities/test_activities.py index b0d24b0..4df4309 100644 --- a/tests/activities/test_activities.py +++ b/tests/activities/test_activities.py @@ -296,12 +296,21 @@ def test_activities_del_with_engine_exception_caught( class MockSuperWithError: def __del__(self): - raise RuntimeError('Test error') + # Only raise error if not being cleaned up by garbage collector + # This prevents the PytestUnraisableExceptionWarning + if hasattr(self, '_should_raise') and self._should_raise: + raise RuntimeError('Test error') # Suppress the PytestUnraisableExceptionWarning for this specific test import warnings warnings.filterwarnings('ignore', category=pytest.PytestUnraisableExceptionWarning) - with patch('builtins.super', return_value=MockSuperWithError()): - activities.__del__() + mock_super = MockSuperWithError() + mock_super._should_raise = True + try: + with patch('builtins.super', return_value=mock_super): + activities.__del__() + finally: + # Prevent the exception from being raised during garbage collection + mock_super._should_raise = False diff --git a/tests/activities/test_experiment_tracking.py b/tests/activities/test_experiment_tracking.py index 2528c68..308844e 100644 --- a/tests/activities/test_experiment_tracking.py +++ b/tests/activities/test_experiment_tracking.py @@ -142,15 +142,24 @@ def test_experiment_tracking_del_with_engine_exception( class MockSuperWithError: def __del__(self): - raise RuntimeError('Test error') + # Only raise error if not being cleaned up by garbage collector + # This prevents the PytestUnraisableExceptionWarning + if hasattr(self, '_should_raise') and self._should_raise: + raise RuntimeError('Test error') # Suppress the PytestUnraisableExceptionWarning for this specific test import warnings warnings.filterwarnings('ignore', category=pytest.PytestUnraisableExceptionWarning) - with patch('builtins.super', return_value=MockSuperWithError()): - et.__del__() + mock_super = MockSuperWithError() + mock_super._should_raise = True + try: + with patch('builtins.super', return_value=mock_super): + et.__del__() + finally: + # Prevent the exception from being raised during garbage collection + mock_super._should_raise = False def test_execute_update_success(