Skip to content

Commit

Permalink
Use skip_on_missing_imports to mark tests in test/agentchat/contrib/c…
Browse files Browse the repository at this point in the history
…apabilities files
  • Loading branch information
kumaranvpl committed Jan 24, 2025
1 parent 5297b55 commit 64105f2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}

Expand Down Expand Up @@ -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]
):
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 3 additions & 16 deletions test/agentchat/contrib/capabilities/test_teachable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"))
Expand Down
29 changes: 5 additions & 24 deletions test/agentchat/contrib/capabilities/test_vision_capability.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"
Expand All @@ -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
):
Expand All @@ -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."""
Expand Down

0 comments on commit 64105f2

Please sign in to comment.