From 64105f223579d57207b7279c1de8a145a194b66d Mon Sep 17 00:00:00 2001 From: Kumaran Rajendhiran Date: Fri, 24 Jan 2025 05:32:42 +0000 Subject: [PATCH] Use skip_on_missing_imports to mark tests in test/agentchat/contrib/capabilities files --- .../test_image_generation_capability.py | 9 +++--- .../capabilities/test_teachable_agent.py | 19 ++---------- .../capabilities/test_vision_capability.py | 29 ++++--------------- 3 files changed, 12 insertions(+), 45 deletions(-) diff --git a/test/agentchat/contrib/capabilities/test_image_generation_capability.py b/test/agentchat/contrib/capabilities/test_image_generation_capability.py index 4002c12863..5b359f1f48 100644 --- a/test/agentchat/contrib/capabilities/test_image_generation_capability.py +++ b/test/agentchat/contrib/capabilities/test_image_generation_capability.py @@ -25,7 +25,6 @@ with optional_import_block() as result: from PIL import Image -skip_requirement = not result.is_successful filter_dict = {"model": ["gpt-4o-mini"]} @@ -102,7 +101,7 @@ def test_dalle_image_generator(dalle_config: dict[str, Any]): # Using cartesian product to generate all possible combinations of resolution, quality, and prompt @pytest.mark.parametrize("gen_config_1", itertools.product(RESOLUTIONS, QUALITIES, PROMPTS)) @pytest.mark.parametrize("gen_config_2", itertools.product(RESOLUTIONS, QUALITIES, PROMPTS)) -@pytest.mark.skipif(skip_requirement, reason="Dependencies are not installed.") +@skip_on_missing_imports(["PIL"], "unknown") def test_dalle_image_generator_cache_key( dalle_config: dict[str, Any], gen_config_1: tuple[str, str, str], gen_config_2: tuple[str, str, str] ): @@ -125,7 +124,7 @@ def test_dalle_image_generator_cache_key( assert cache_key_1 != cache_key_2 -@pytest.mark.skipif(skip_requirement, reason="Dependencies are not installed.") +@skip_on_missing_imports(["PIL"], "unknown") def test_image_generation_capability_positive(monkeypatch, image_gen_capability): """Tests ImageGeneration capability to generate images by calling the ImageGenerator. @@ -153,7 +152,7 @@ def test_image_generation_capability_positive(monkeypatch, image_gen_capability) assert auto_reply not in processed_message -@pytest.mark.skipif(skip_requirement, reason="Dependencies are not installed.") +@skip_on_missing_imports(["PIL"], "unknown") def test_image_generation_capability_negative(monkeypatch, image_gen_capability): """Tests ImageGeneration capability to generate images by calling the ImageGenerator. @@ -181,7 +180,7 @@ def test_image_generation_capability_negative(monkeypatch, image_gen_capability) assert auto_reply == processed_message -@pytest.mark.skipif(skip_requirement, reason="Dependencies are not installed.") +@skip_on_missing_imports(["PIL"], "unknown") def test_image_generation_capability_cache(monkeypatch): """Tests ImageGeneration capability to cache the generated images.""" test_image_size = (256, 256) diff --git a/test/agentchat/contrib/capabilities/test_teachable_agent.py b/test/agentchat/contrib/capabilities/test_teachable_agent.py index 59d9a7ab63..374cfcde30 100755 --- a/test/agentchat/contrib/capabilities/test_teachable_agent.py +++ b/test/agentchat/contrib/capabilities/test_teachable_agent.py @@ -11,17 +11,10 @@ from autogen import ConversableAgent from autogen.agentchat.contrib.capabilities.teachability import Teachability from autogen.formatting_utils import colored -from autogen.import_utils import optional_import_block +from autogen.import_utils import skip_on_missing_imports from ....conftest import Credentials -with optional_import_block() as result: - import chromadb # noqa: F401 - - -skip = not result.is_successful - - # Specify the model to use by uncommenting one of the following lines. # filter_dict={"model": ["gpt-4-1106-preview"]} # filter_dict={"model": ["gpt-4-0613"]} @@ -130,10 +123,7 @@ def use_task_advice_pair_phrasing(credentials: Credentials): @pytest.mark.openai -@pytest.mark.skipif( - skip, - reason="do not run if dependency is not installed or requested to skip", -) +@skip_on_missing_imports(["chromadb"], "teachable") def test_teachability_code_paths(credentials_gpt_4o_mini: Credentials): """Runs this file's unit tests.""" total_num_errors, total_num_tests = 0, 0 @@ -162,10 +152,7 @@ def test_teachability_code_paths(credentials_gpt_4o_mini: Credentials): @pytest.mark.openai -@pytest.mark.skipif( - skip, - reason="do not run if dependency is not installed or requested to skip", -) +@skip_on_missing_imports(["chromadb"], "teachable") def test_teachability_accuracy(credentials_gpt_4o_mini: Credentials): """A very cheap and fast test of teachability accuracy.""" print(colored("\nTEST TEACHABILITY ACCURACY", "light_cyan")) diff --git a/test/agentchat/contrib/capabilities/test_vision_capability.py b/test/agentchat/contrib/capabilities/test_vision_capability.py index dad08ab0f0..c54b3cf5ed 100644 --- a/test/agentchat/contrib/capabilities/test_vision_capability.py +++ b/test/agentchat/contrib/capabilities/test_vision_capability.py @@ -11,15 +11,12 @@ from autogen.agentchat.contrib.capabilities.vision_capability import VisionCapability from autogen.agentchat.conversable_agent import ConversableAgent -from autogen.import_utils import optional_import_block +from autogen.import_utils import optional_import_block, skip_on_missing_imports with optional_import_block() as result: from PIL import Image # noqa: F401 -skip_test = not result.is_successful - - @pytest.fixture def lmm_config(): return { @@ -42,24 +39,14 @@ def conversable_agent(): return ConversableAgent(name="conversable_agent", llm_config=False) -@pytest.mark.skipif( - skip_test, - reason="do not run if dependency is not installed", -) +@skip_on_missing_imports(["PIL"], "unknown") def test_add_to_conversable_agent(vision_capability, conversable_agent): vision_capability.add_to_agent(conversable_agent) assert hasattr(conversable_agent, "process_last_received_message") -@pytest.mark.skipif( - skip_test, - reason="do not run if dependency is not installed", -) +@skip_on_missing_imports(["PIL"], "unknown") @patch("autogen.oai.client.OpenAIWrapper") -@pytest.mark.skipif( - skip_test, - reason="do not run if dependency is not installed", -) def test_process_last_received_message_text(mock_lmm_client, vision_capability): mock_lmm_client.create.return_value = MagicMock(choices=[MagicMock(message=MagicMock(content="A description"))]) content = "Test message without image" @@ -76,10 +63,7 @@ def test_process_last_received_message_text(mock_lmm_client, vision_capability): "autogen.agentchat.contrib.capabilities.vision_capability.VisionCapability._get_image_caption", return_value="A sample image caption.", ) -@pytest.mark.skipif( - skip_test, - reason="do not run if dependency is not installed", -) +@skip_on_missing_imports(["PIL"], "unknown") def test_process_last_received_message_with_image( mock_get_caption, mock_convert_base64, mock_get_image_data, vision_capability ): @@ -105,10 +89,7 @@ def caption_func(image_url: str, image_data=None, lmm_client=None) -> str: return caption_func -@pytest.mark.skipif( - skip_test, - reason="do not run if dependency is not installed", -) +@skip_on_missing_imports(["PIL"], "unknown") class TestCustomCaptionFunc: def test_custom_caption_func_with_valid_url(self, custom_caption_func): """Test custom caption function with a valid image URL."""