Skip to content

Commit c80df8a

Browse files
maxim-saplinqingyun-wudavorrunje
authored
Skip tests that depend on OpenAI via --skip-openai (#1097)
* --skip-openai * All tests pass * Update build.yml * Update Contribute.md * Fix for failing Ubuntu tests * More tests skipped, fixing 3.10 build * Apply suggestions from code review Co-authored-by: Qingyun Wu <[email protected]> * Added more comments * fixed test__wrap_function_* --------- Co-authored-by: Qingyun Wu <[email protected]> Co-authored-by: Davor Runje <[email protected]>
1 parent 3b0e059 commit c80df8a

19 files changed

+139
-107
lines changed

.github/workflows/build.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,15 @@ jobs:
4141
pip install -e .
4242
python -c "import autogen"
4343
pip install -e. pytest mock
44-
pip uninstall -y openai
4544
- name: Test with pytest
4645
if: matrix.python-version != '3.10'
4746
run: |
48-
pytest test
47+
pytest test --skip-openai
4948
- name: Coverage
5049
if: matrix.python-version == '3.10'
5150
run: |
5251
pip install -e .[test]
53-
pip uninstall -y openai
54-
coverage run -a -m pytest test --ignore=test/agentchat/contrib
52+
coverage run -a -m pytest test --ignore=test/agentchat/contrib --skip-openai
5553
coverage xml
5654
- name: Upload coverage to Codecov
5755
if: matrix.python-version == '3.10'

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,7 @@ test/my_tmp/*
171171

172172
# Storage for the AgentEval output
173173
test/test_files/agenteval-in-out/out/
174+
175+
# Files created by tests
176+
*tmp_code_*
177+
test/agentchat/test_agent_scripts/*

test/agentchat/contrib/test_agent_builder.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from packaging.requirements import Requirement
66
from autogen.agentchat.contrib.agent_builder import AgentBuilder
77
from autogen import UserProxyAgent
8+
from conftest import skip_openai
89

910
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
1011
from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST # noqa: E402
@@ -20,15 +21,15 @@
2021
from openai.types.completion import Completion
2122
from openai.types.completion_usage import CompletionUsage
2223
import diskcache
23-
24-
OPENAI_INSTALLED = True
2524
except ImportError:
26-
OPENAI_INSTALLED = False
25+
skip = True
26+
else:
27+
skip = False or skip_openai
2728

2829

2930
@pytest.mark.skipif(
30-
not OPENAI_INSTALLED,
31-
reason="do not run when dependency is not installed",
31+
skip,
32+
reason="openai not installed OR requested to skip",
3233
)
3334
def test_build():
3435
builder = AgentBuilder(config_path=oai_config_path, builder_model="gpt-4", agent_model="gpt-4")
@@ -57,8 +58,8 @@ def test_build():
5758

5859

5960
@pytest.mark.skipif(
60-
not OPENAI_INSTALLED,
61-
reason="do not run when dependency is not installed",
61+
skip,
62+
reason="openai not installed OR requested to skip",
6263
)
6364
def test_save():
6465
builder = AgentBuilder(config_path=oai_config_path, builder_model="gpt-4", agent_model="gpt-4")
@@ -93,8 +94,8 @@ def test_save():
9394

9495

9596
@pytest.mark.skipif(
96-
not OPENAI_INSTALLED,
97-
reason="do not run when dependency is not installed",
97+
skip,
98+
reason="openai not installed OR requested to skip",
9899
)
99100
def test_load():
100101
builder = AgentBuilder(config_path=oai_config_path, builder_model="gpt-4", agent_model="gpt-4")
@@ -128,8 +129,8 @@ def test_load():
128129

129130

130131
@pytest.mark.skipif(
131-
not OPENAI_INSTALLED,
132-
reason="do not run when dependency is not installed",
132+
skip,
133+
reason="openai not installed OR requested to skip",
133134
)
134135
def test_clear_agent():
135136
builder = AgentBuilder(config_path=oai_config_path, builder_model="gpt-4", agent_model="gpt-4")

test/agentchat/contrib/test_compressible_agent.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
import autogen
44
import os
5+
from conftest import skip_openai
56
from autogen.agentchat.contrib.compressible_agent import CompressibleAgent
67

78
here = os.path.abspath(os.path.dirname(__file__))
@@ -19,15 +20,15 @@
1920

2021
try:
2122
import openai
22-
23-
OPENAI_INSTALLED = True
2423
except ImportError:
25-
OPENAI_INSTALLED = False
24+
skip = True
25+
else:
26+
skip = False or skip_openai
2627

2728

2829
@pytest.mark.skipif(
29-
sys.platform in ["darwin", "win32"] or not OPENAI_INSTALLED,
30-
reason="do not run on MacOS or windows or dependency is not installed",
30+
sys.platform in ["darwin", "win32"] or skip,
31+
reason="do not run on MacOS or windows OR dependency is not installed OR requested to skip",
3132
)
3233
def test_mode_compress():
3334
conversations = {}
@@ -65,8 +66,8 @@ def test_mode_compress():
6566

6667

6768
@pytest.mark.skipif(
68-
sys.platform in ["darwin", "win32"] or not OPENAI_INSTALLED,
69-
reason="do not run on MacOS or windows or dependency is not installed",
69+
sys.platform in ["darwin", "win32"] or skip,
70+
reason="do not run on MacOS or windows OR dependency is not installed OR requested to skip",
7071
)
7172
def test_mode_customized():
7273
try:
@@ -135,8 +136,8 @@ def constrain_num_messages(messages):
135136

136137

137138
@pytest.mark.skipif(
138-
sys.platform in ["darwin", "win32"] or not OPENAI_INSTALLED,
139-
reason="do not run on MacOS or windows or dependency is not installed",
139+
sys.platform in ["darwin", "win32"] or skip,
140+
reason="do not run on MacOS or windows OR dependency is not installed OR requested to skip",
140141
)
141142
def test_compress_message():
142143
assistant = CompressibleAgent(
@@ -169,6 +170,10 @@ def test_compress_message():
169170
assert is_success, "Compression failed."
170171

171172

173+
@pytest.mark.skipif(
174+
skip,
175+
reason="do not run if dependency is not installed OR requested to skip",
176+
)
172177
def test_mode_terminate():
173178
assistant = CompressibleAgent(
174179
name="assistant",

test/agentchat/contrib/test_gpt_assistant.py

+18-17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import autogen
55
from autogen import OpenAIWrapper
6+
from conftest import skip_openai
67

78
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
89
from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST # noqa: E402
@@ -11,10 +12,10 @@
1112
import openai
1213
from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent
1314
from autogen.oai.openai_utils import retrieve_assistants_by_name
14-
15-
skip_test = False
1615
except ImportError:
17-
skip_test = True
16+
skip = True
17+
else:
18+
skip = False or skip_openai
1819

1920
config_list = autogen.config_list_from_json(
2021
OAI_CONFIG_LIST, file_location=KEY_LOC, filter_dict={"api_type": ["openai"]}
@@ -26,8 +27,8 @@ def ask_ossinsight(question):
2627

2728

2829
@pytest.mark.skipif(
29-
sys.platform in ["darwin", "win32"] or skip_test,
30-
reason="do not run on MacOS or windows or dependency is not installed",
30+
sys.platform in ["darwin", "win32"] or skip,
31+
reason="do not run on MacOS or windows OR dependency is not installed OR requested to skip",
3132
)
3233
def test_gpt_assistant_chat():
3334
ossinsight_api_schema = {
@@ -73,8 +74,8 @@ def test_gpt_assistant_chat():
7374

7475

7576
@pytest.mark.skipif(
76-
sys.platform in ["darwin", "win32"] or skip_test,
77-
reason="do not run on MacOS or windows or dependency is not installed",
77+
sys.platform in ["darwin", "win32"] or skip,
78+
reason="do not run on MacOS or windows OR dependency is not installed OR requested to skip",
7879
)
7980
def test_get_assistant_instructions():
8081
"""
@@ -97,8 +98,8 @@ def test_get_assistant_instructions():
9798

9899

99100
@pytest.mark.skipif(
100-
sys.platform in ["darwin", "win32"] or skip_test,
101-
reason="do not run on MacOS or windows or dependency is not installed",
101+
sys.platform in ["darwin", "win32"] or skip,
102+
reason="do not run on MacOS or windows OR dependency is not installed OR requested to skip",
102103
)
103104
def test_gpt_assistant_instructions_overwrite():
104105
"""
@@ -142,8 +143,8 @@ def test_gpt_assistant_instructions_overwrite():
142143

143144

144145
@pytest.mark.skipif(
145-
sys.platform in ["darwin", "win32"] or skip_test,
146-
reason="do not run on MacOS or windows or dependency is not installed",
146+
sys.platform in ["darwin", "win32"] or skip,
147+
reason="do not run on MacOS or windows OR dependency is not installed OR requested to skip",
147148
)
148149
def test_gpt_assistant_existing_no_instructions():
149150
"""
@@ -178,8 +179,8 @@ def test_gpt_assistant_existing_no_instructions():
178179

179180

180181
@pytest.mark.skipif(
181-
sys.platform in ["darwin", "win32"] or skip_test,
182-
reason="do not run on MacOS or windows or dependency is not installed",
182+
sys.platform in ["darwin", "win32"] or skip,
183+
reason="do not run on MacOS or windows OR dependency is not installed OR requested to skip",
183184
)
184185
def test_get_assistant_files():
185186
"""
@@ -212,8 +213,8 @@ def test_get_assistant_files():
212213

213214

214215
@pytest.mark.skipif(
215-
sys.platform in ["darwin", "win32"] or skip_test,
216-
reason="do not run on MacOS or windows or dependency is not installed",
216+
sys.platform in ["darwin", "win32"] or skip,
217+
reason="do not run on MacOS or windows OR dependency is not installed OR requested to skip",
217218
)
218219
def test_assistant_retrieval():
219220
"""
@@ -283,8 +284,8 @@ def test_assistant_retrieval():
283284

284285

285286
@pytest.mark.skipif(
286-
sys.platform in ["darwin", "win32"] or skip_test,
287-
reason="do not run on MacOS or windows or dependency is not installed",
287+
sys.platform in ["darwin", "win32"] or skip,
288+
reason="do not run on MacOS or windows OR dependency is not installed OR requested to skip",
288289
)
289290
def test_assistant_mismatch_retrieval():
290291
"""Test function to check if the GPTAssistantAgent can filter out the mismatch assistant"""

test/agentchat/contrib/test_teachable_agent.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import sys
44
from autogen import ConversableAgent, config_list_from_json
5+
from conftest import skip_openai
56

67
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
78
from test_assistant_agent import OAI_CONFIG_LIST, KEY_LOC # noqa: E402
@@ -12,7 +13,7 @@
1213
except ImportError:
1314
skip = True
1415
else:
15-
skip = False
16+
skip = False or skip_openai
1617

1718
try:
1819
from termcolor import colored

test/agentchat/test_assistant_agent.py

+13-21
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22
import sys
33
import pytest
44
import autogen
5+
from conftest import skip_openai
56
from autogen.agentchat import AssistantAgent, UserProxyAgent
67

8+
try:
9+
from openai import OpenAI
10+
except ImportError:
11+
skip = True
12+
else:
13+
skip = False or skip_openai
14+
715
KEY_LOC = "notebook"
816
OAI_CONFIG_LIST = "OAI_CONFIG_LIST"
917
here = os.path.abspath(os.path.dirname(__file__))
1018

1119

1220
@pytest.mark.skipif(
13-
sys.platform in ["darwin", "win32"],
14-
reason="do not run on MacOS or windows",
21+
sys.platform in ["darwin", "win32"] or skip,
22+
reason="do not run on MacOS or windows OR openai not installed OR requested to skip",
1523
)
1624
def test_ai_user_proxy_agent():
17-
try:
18-
import openai
19-
except ImportError:
20-
return
21-
2225
conversations = {}
2326
# autogen.ChatCompletion.start_logging(conversations)
2427

@@ -57,11 +60,8 @@ def test_ai_user_proxy_agent():
5760
print(conversations)
5861

5962

63+
@pytest.mark.skipif(skip, reason="openai not installed OR requested to skip")
6064
def test_gpt35(human_input_mode="NEVER", max_consecutive_auto_reply=5):
61-
try:
62-
import openai
63-
except ImportError:
64-
return
6565
config_list = autogen.config_list_from_json(
6666
OAI_CONFIG_LIST,
6767
file_location=KEY_LOC,
@@ -115,12 +115,8 @@ def test_gpt35(human_input_mode="NEVER", max_consecutive_auto_reply=5):
115115
assert not isinstance(user.use_docker, bool) # None or str
116116

117117

118+
@pytest.mark.skipif(skip, reason="openai not installed OR requested to skip")
118119
def test_create_execute_script(human_input_mode="NEVER", max_consecutive_auto_reply=10):
119-
try:
120-
import openai
121-
except ImportError:
122-
return
123-
124120
config_list = autogen.config_list_from_json(OAI_CONFIG_LIST, file_location=KEY_LOC)
125121
conversations = {}
126122
# autogen.ChatCompletion.start_logging(conversations)
@@ -160,12 +156,8 @@ def test_create_execute_script(human_input_mode="NEVER", max_consecutive_auto_re
160156
# autogen.ChatCompletion.stop_logging()
161157

162158

159+
@pytest.mark.skipif(skip, reason="openai not installed OR requested to skip")
163160
def test_tsp(human_input_mode="NEVER", max_consecutive_auto_reply=10):
164-
try:
165-
import openai
166-
except ImportError:
167-
return
168-
169161
config_list = autogen.config_list_from_json(
170162
OAI_CONFIG_LIST,
171163
file_location=KEY_LOC,

test/agentchat/test_async.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import pytest
22
import asyncio
33
import autogen
4+
from conftest import skip_openai
45
from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST
56

7+
try:
8+
from openai import OpenAI
9+
except ImportError:
10+
skip = True
11+
else:
12+
skip = False or skip_openai
13+
614

715
def get_market_news(ind, ind_upper):
816
data = {
@@ -45,13 +53,9 @@ def get_market_news(ind, ind_upper):
4553
return feeds_summary
4654

4755

56+
@pytest.mark.skipif(skip, reason="openai not installed OR requested to skip")
4857
@pytest.mark.asyncio
4958
async def test_async_groupchat():
50-
try:
51-
import openai
52-
except ImportError:
53-
return
54-
5559
config_list = autogen.config_list_from_json(OAI_CONFIG_LIST, KEY_LOC)
5660

5761
llm_config = {
@@ -91,12 +95,9 @@ async def test_async_groupchat():
9195
assert len(user_proxy.chat_messages) > 0
9296

9397

98+
@pytest.mark.skipif(skip, reason="openai not installed OR requested to skip")
9499
@pytest.mark.asyncio
95100
async def test_stream():
96-
try:
97-
import openai
98-
except ImportError:
99-
return
100101
config_list = autogen.config_list_from_json(OAI_CONFIG_LIST, KEY_LOC)
101102
data = asyncio.Future()
102103

0 commit comments

Comments
 (0)