Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

Fix imports, type hints and fixtures for goal oriented tests (#3415)

Reinier van der Leer3d89ed17879a
diff --git a/tests/conftest.py b/tests/conftest.py
index 2096870612..c5814b0a2a 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -5,7 +5,8 @@ import pytest
from autogpt.api_manager import ApiManager
from autogpt.config import Config
from autogpt.workspace import Workspace
-from tests.vcr.openai_filter import before_record_request
+
+pytest_plugins = ["tests.integration.agent_factory"]
@pytest.fixture()
@@ -25,8 +26,13 @@ def config(workspace: Workspace) -> Config:
# Do a little setup and teardown since the config object is a singleton
old_ws_path = config.workspace_path
+ old_file_logger_path = config.file_logger_path
+
config.workspace_path = workspace.root
+ config.file_logger_path = workspace.get_path("file_logger.txt")
yield config
+
+ config.file_logger_path = old_file_logger_path
config.workspace_path = old_ws_path
diff --git a/tests/integration/agent_factory.py b/tests/integration/agent_factory.py
index 2a88a7b1cb..1cda93eb44 100644
--- a/tests/integration/agent_factory.py
+++ b/tests/integration/agent_factory.py
@@ -1,14 +1,36 @@
-import os
+import pytest
from autogpt.agent import Agent
-from autogpt.app import CFG
from autogpt.commands.command import CommandRegistry
-from autogpt.config import AIConfig
-from autogpt.memory import get_memory
+from autogpt.config import AIConfig, Config
+from autogpt.memory import NoMemory, get_memory
from autogpt.prompts.prompt import DEFAULT_TRIGGERING_PROMPT
+from autogpt.workspace import Workspace
-def create_browser_agent(workspace):
+def agent_test_config(config: Config):
+ was_continuous_mode = config.continuous_mode
+ was_temperature = config.temperature
+ config.set_continuous_mode(True)
+ config.set_temperature(0)
+ yield config
+ config.set_continuous_mode(was_continuous_mode)
+ config.set_temperature(was_temperature)
+
+
+def memory_none(agent_test_config: Config):
+ was_memory_backend = agent_test_config.memory_backend
+
+ agent_test_config.set_memory_backend("no_memory")
+ yield get_memory(agent_test_config, init=True)
+
+ agent_test_config.set_memory_backend(was_memory_backend)
+
+
+def browser_agent(agent_test_config, memory_none: NoMemory, workspace: Workspace):
command_registry = CommandRegistry()
command_registry.import_commands("autogpt.commands.file_operations")
command_registry.import_commands("autogpt.commands.web_selenium")
@@ -25,16 +47,12 @@ def create_browser_agent(workspace):
],
)
ai_config.command_registry = command_registry
- CFG.set_continuous_mode(True)
- CFG.set_memory_backend("no_memory")
- CFG.set_temperature(0)
- memory = get_memory(CFG, init=True)
system_prompt = ai_config.construct_full_prompt()
agent = Agent(
ai_name="",
- memory=memory,
+ memory=memory_none,
full_message_history=[],
command_registry=command_registry,
config=ai_config,
@@ -47,7 +65,8 @@ def create_browser_agent(workspace):
return agent
-def create_writer_agent(workspace):
+def writer_agent(agent_test_config, memory_none: NoMemory, workspace: Workspace):
command_registry = CommandRegistry()
command_registry.import_commands("autogpt.commands.file_operations")
command_registry.import_commands("autogpt.app")
@@ -62,10 +81,7 @@ def create_writer_agent(workspace):
],
)
ai_config.command_registry = command_registry
- CFG.set_continuous_mode(True)
- CFG.set_memory_backend("no_memory")
- CFG.set_temperature(0)
- memory = get_memory(CFG, init=True)
+
triggering_prompt = (
"Determine which next command to use, and respond using the"
" format specified above:"
@@ -74,7 +90,7 @@ def create_writer_agent(workspace):
agent = Agent(
ai_name="",
- memory=memory,
+ memory=memory_none,
full_message_history=[],
command_registry=command_registry,
config=ai_config,
diff --git a/tests/integration/agent_utils.py b/tests/integration/agent_utils.py
index 3d03ef5eed..a899afcf32 100644
--- a/tests/integration/agent_utils.py
+++ b/tests/integration/agent_utils.py
@@ -1,7 +1,9 @@
-import concurrent
+import concurrent.futures
+from autogpt.agent.agent import Agent
-def run_interaction_loop(agent, timeout):
+
+def run_interaction_loop(agent: Agent, timeout: float | None):
with concurrent.futures.ThreadPoolExecutor() as executor:
future = executor.submit(agent.start_interaction_loop)
try:
diff --git a/tests/integration/goal_oriented/test_browse_website.py b/tests/integration/goal_oriented/test_browse_website.py
index 86dc39eaa6..9591f2f99d 100644
--- a/tests/integration/goal_oriented/test_browse_website.py
+++ b/tests/integration/goal_oriented/test_browse_website.py
@@ -1,27 +1,18 @@
-import os
-
import pytest
+from autogpt.agent import Agent
from autogpt.commands.file_operations import read_file
-from autogpt.config import Config
-from tests.integration.agent_factory import create_browser_agent
from tests.integration.agent_utils import run_interaction_loop
-from tests.utils import get_workspace_file_path, requires_api_key
-
-CFG = Config()
+from tests.utils import requires_api_key
@requires_api_key("OPENAI_API_KEY")
@pytest.mark.vcr
-def test_browse_website(workspace) -> None:
- CFG.workspace_path = workspace.root
- CFG.file_logger_path = os.path.join(workspace.root, "file_logger.txt")
-
- file_name = get_workspace_file_path(workspace, "browse_website.txt")
- agent = create_browser_agent(workspace)
+def test_browse_website(browser_agent: Agent) -> None:
+ file_path = browser_agent.workspace.get_path("browse_website.txt")
try:
- run_interaction_loop(agent, 40)
+ run_interaction_loop(browser_agent, 40)
# catch system exit exceptions
except SystemExit: # the agent returns an exception when it shuts down
- content = read_file(file_name)
+ content = read_file(file_path)
assert "£25.89" in content, f"Expected £25.89, got {content}"
diff --git a/tests/integration/goal_oriented/test_write_file.py b/tests/integration/goal_oriented/test_write_file.py
index a2f2fa5a12..da67235a83 100644
--- a/tests/integration/goal_oriented/test_write_file.py
+++ b/tests/integration/goal_oriented/test_write_file.py
@@ -1,27 +1,18 @@
-import os
-
import pytest
+from autogpt.agent import Agent
from autogpt.commands.file_operations import read_file
-from autogpt.config import Config
-from tests.integration.agent_factory import create_writer_agent
from tests.integration.agent_utils import run_interaction_loop
from tests.utils import requires_api_key
-CFG = Config()
-
@requires_api_key("OPENAI_API_KEY")
@pytest.mark.vcr
-def test_write_file(workspace) -> None:
- CFG.workspace_path = workspace.root
- CFG.file_logger_path = os.path.join(workspace.root, "file_logger.txt")
-
- file_name = str(workspace.get_path("hello_world.txt"))
- agent = create_writer_agent(workspace)
+def test_write_file(writer_agent: Agent) -> None:
+ file_path = str(writer_agent.workspace.get_path("hello_world.txt"))
try:
- run_interaction_loop(agent, 40)
+ run_interaction_loop(writer_agent, 40)
# catch system exit exceptions
except SystemExit: # the agent returns an exception when it shuts down
- content = read_file(file_name)
+ content = read_file(file_path)
assert content == "Hello World", f"Expected 'Hello World', got {content}"
diff --git a/tests/vcr/__init__.py b/tests/vcr/__init__.py
new file mode 100644
index 0000000000..e69de29bb2