Skip to content

Commit

Permalink
Use pytest marker instead of --skip-docker flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaranvpl committed Jan 17, 2025
1 parent 919573b commit bc12644
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/core-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ jobs:
- name: Test with pytest skipping openai and docker tests
if: matrix.python-version != '3.10' && matrix.os != 'ubuntu-latest'
run: |
bash scripts/test-core-skip-llm.sh --skip-docker
bash scripts/test-core-skip-llm.sh -m "not docker"
- name: Coverage with Redis
if: matrix.python-version == '3.10'
run: |
Expand Down
14 changes: 13 additions & 1 deletion scripts/test-skip-llm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@
#
# SPDX-License-Identifier: Apache-2.0

bash scripts/test.sh -m "not (openai or gemini or anthropic)" "$@"
base_filter="not (openai or gemini or anthropic)"
args=()
while [[ $# -gt 0 ]]; do
if [[ "$1" == "-m" ]]; then
shift
base_filter="$base_filter and ($1)"
else
args+=("$1")
fi
shift
done

bash scripts/test.sh -m "$base_filter" "${args[@]}"
7 changes: 5 additions & 2 deletions test/coding/test_commandline_code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
from autogen.coding.local_commandline_code_executor import LocalCommandLineCodeExecutor

sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
from conftest import MOCK_OPEN_AI_API_KEY, skip_docker
from ..conftest import MOCK_OPEN_AI_API_KEY

if skip_docker or not is_docker_running() or not decide_use_docker(use_docker=None):
if not is_docker_running() or not decide_use_docker(use_docker=None):
skip_docker_test = True
classes_to_test = [LocalCommandLineCodeExecutor]
else:
Expand Down Expand Up @@ -79,6 +79,7 @@ def test_create_local() -> None:
assert executor is config["executor"]


@pytest.mark.docker
@pytest.mark.skipif(
skip_docker_test,
reason="docker is not running or requested to skip docker tests",
Expand Down Expand Up @@ -252,6 +253,7 @@ def test_local_commandline_code_executor_restart() -> None:


# This is kind of hard to test because each exec is a new env
@pytest.mark.docker
@pytest.mark.skipif(
skip_docker_test,
reason="docker is not running or requested to skip docker tests",
Expand All @@ -265,6 +267,7 @@ def test_docker_commandline_code_executor_restart() -> None:
assert result.exit_code == 0


@pytest.mark.docker
@pytest.mark.skipif(
skip_docker_test,
reason="docker is not running or requested to skip docker tests",
Expand Down
13 changes: 11 additions & 2 deletions test/coding/test_embedded_ipython_code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@
import pytest

from autogen.agentchat.conversable_agent import ConversableAgent
from autogen.code_utils import (
decide_use_docker,
is_docker_running,
)
from autogen.coding.base import CodeBlock, CodeExecutor
from autogen.coding.factory import CodeExecutorFactory

from ..conftest import MOCK_OPEN_AI_API_KEY, skip_docker
from ..conftest import MOCK_OPEN_AI_API_KEY

if not is_docker_running() or not decide_use_docker(use_docker=None):
skip_docker_test = True
else:
skip_docker_test = False

try:
from autogen.coding.jupyter import (
Expand All @@ -43,7 +52,7 @@ def __init__(self, **kwargs):
else:
classes_to_test = [EmbeddedIPythonCodeExecutor, LocalJupyterCodeExecutor]

if not skip_docker:
if not skip_docker_test:
classes_to_test.append(DockerJupyterExecutor)

skip = False
Expand Down
15 changes: 0 additions & 15 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,13 @@

import autogen

skip_docker = False

KEY_LOC = str((Path(__file__).parents[1] / "notebook").resolve())
OAI_CONFIG_LIST = "OAI_CONFIG_LIST"
MOCK_OPEN_AI_API_KEY = "sk-mockopenaiAPIkeysinexpectedformatsfortestingonly"

reason = "requested to skip"


# Registers command-line options like '--skip-docker' via pytest hook.
# When these flags are set, it indicates that tests requiring OpenAI or Redis (respectively) should be skipped.
def pytest_addoption(parser: pytest.Parser) -> None:
parser.addoption("--skip-docker", action="store_true", help="Skip all tests that require docker")


# pytest hook implementation extracting command line args and exposing it globally
@pytest.hookimpl(tryfirst=True)
def pytest_configure(config: pytest.Config) -> None:
global skip_docker
skip_docker = config.getoption("--skip-docker", False)


class Credentials:
"""Credentials for the OpenAI API."""

Expand Down
7 changes: 5 additions & 2 deletions test/test_code_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
is_docker_running,
)

from .conftest import Credentials, skip_docker
from .conftest import Credentials

here = os.path.abspath(os.path.dirname(__file__))

if skip_docker or not is_docker_running() or not decide_use_docker(use_docker=None):
if not is_docker_running() or not decide_use_docker(use_docker=None):
skip_docker_test = True
else:
skip_docker_test = False
Expand Down Expand Up @@ -179,6 +179,7 @@ def scrape(url):
assert len(codeblocks) == 1 and codeblocks[0] == ("", "source setup.sh")


@pytest.mark.docker
@pytest.mark.skipif(skip_docker_test, reason="docker is not running or requested to skip docker tests")
def test_execute_code(use_docker=True):
# Test execute code and save the code to a file.
Expand Down Expand Up @@ -243,6 +244,7 @@ def test_execute_code(use_docker=True):
assert isinstance(image, str)


@pytest.mark.docker
@pytest.mark.skipif(skip_docker_test, reason="docker is not running or requested to skip docker tests")
def test_execute_code_with_custom_filename_on_docker():
with tempfile.TemporaryDirectory() as tempdir:
Expand All @@ -257,6 +259,7 @@ def test_execute_code_with_custom_filename_on_docker():
assert image == "python:codetest.py"


@pytest.mark.docker
@pytest.mark.skipif(
skip_docker_test,
reason="docker is not running or requested to skip docker tests",
Expand Down
4 changes: 2 additions & 2 deletions website/docs/contributor-guide/tests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ See [here](https://github.com/ag2ai/ag2/blob/main/notebook/contributing.md#testi
## Skip flags for tests

- `-m`: Used to select or deselect specific groups of tests marked with pytest markers, such as tests for LLM services like OpenAI, Gemini etc. For example, you can mark tests with `@pytest.mark.openai` and then use `-m openai` to run only those tests.
- `--skip-docker`: Skips tests that explicitly require Docker.
- `-m "not docker"`: Skips tests that explicitly require Docker.
- `-m "not redis"`: Skips tests that require a Redis server.


**Examples:**

To skip tests that require access to LLM services and Docker, run:
```bash
bash scripts/test-core-skip-llm.sh --skip-docker
bash scripts/test-core-skip-llm.sh -m "not docker"
```

To run tests for all LLM services (OpenAI, Gemini, etc.), use:
Expand Down

0 comments on commit bc12644

Please sign in to comment.