From ce5ede8c129ed2b197d7882ee47396c3c73f2fc9 Mon Sep 17 00:00:00 2001 From: Albert Villanova del Moral <8515462+albertvillanova@users.noreply.github.com> Date: Wed, 5 Feb 2025 18:48:39 +0100 Subject: [PATCH] Suppress terminal logging in CI tests (#504) * Test logging to terminal is disabled for testing * Suppress terminal logging in CI tests * Pass verbosity_level to test terminal logging output * Refactor --- tests/conftest.py | 19 +++++++++++++++++++ tests/test_agents.py | 8 +++++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..ad587c5 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,19 @@ +from unittest.mock import patch + +import pytest + +from smolagents.agents import MultiStepAgent + + +original_multi_step_agent_init = MultiStepAgent.__init__ + + +@pytest.fixture(autouse=True) +def patch_multi_step_agent_with_suppressed_logging(): + with patch.object(MultiStepAgent, "__init__", autospec=True) as mock_init: + + def init_with_suppressed_logging(self, *args, verbosity_level=-1, **kwargs): + original_multi_step_agent_init(self, *args, verbosity_level=verbosity_level, **kwargs) + + mock_init.side_effect = init_with_suppressed_logging + yield diff --git a/tests/test_agents.py b/tests/test_agents.py index 80976d0..52808aa 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -481,7 +481,8 @@ class AgentTests(unittest.TestCase): assert "You can also give requests to team members." in manager_agent.system_prompt def test_code_agent_missing_import_triggers_advice_in_error_log(self): - agent = CodeAgent(tools=[], model=fake_code_model_import) + # Set explicit verbosity level to 1 to override the default verbosity level of -1 set in CI fixture + agent = CodeAgent(tools=[], model=fake_code_model_import, verbosity_level=1) with agent.logger.console.capture() as capture: agent.run("Count to 3") @@ -655,6 +656,11 @@ nested_answer() class TestMultiStepAgent: + def test_logging_to_terminal_is_disabled(self): + fake_model = MagicMock() + agent = MultiStepAgent(tools=[], model=fake_model) + assert agent.logger.level == -1, "logging to terminal should be disabled for testing using a fixture" + def test_step_number(self): fake_model = MagicMock() agent = MultiStepAgent(tools=[], model=fake_model)