From a779c10b495417f9ff8c31c9a3357efa25103dfa Mon Sep 17 00:00:00 2001 From: RaiseYI Date: Fri, 25 Oct 2024 12:41:19 +0800 Subject: [PATCH 1/2] Add tests for tools --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/pallavag/claude-computer-use-macos?shareId=XXXX-XXXX-XXXX-XXXX). --- README.md | 18 ++++++++++++++++ tests/test_tools.py | 50 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 tests/test_tools.py diff --git a/README.md b/README.md index 490d6aa..b395190 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,24 @@ Replace `'Open Safari and look up Anthropic'` with your desired instruction. **Note:** If you do not provide an instruction via the command line, the script will use the default instruction specified in `main.py`. You can edit `main.py` to change this default instruction. +## Running Tests + +To run the tests using `pytest`, follow these steps: + +1. **Install `pytest` if you haven't already:** + + ```bash + pip install pytest + ``` + +2. **Run the tests:** + + ```bash + pytest + ``` + +This will automatically discover and run all the test files in the `tests` directory. + ## Exiting the Script You can quit the script at any time by pressing `Ctrl+C` in the terminal. diff --git a/tests/test_tools.py b/tests/test_tools.py new file mode 100644 index 0000000..c7ece26 --- /dev/null +++ b/tests/test_tools.py @@ -0,0 +1,50 @@ +import pytest +from computer_use_demo.tools import BashTool, ComputerTool, EditTool, ToolCollection, ToolError + +@pytest.fixture +def bash_tool(): + return BashTool() + +@pytest.fixture +def computer_tool(): + return ComputerTool() + +@pytest.fixture +def edit_tool(): + return EditTool() + +@pytest.fixture +def tool_collection(bash_tool, computer_tool, edit_tool): + return ToolCollection(bash_tool, computer_tool, edit_tool) + +def test_bash_tool_restart(bash_tool): + result = pytest.run(bash_tool(restart=True)) + assert result.system == "tool has been restarted." + +def test_bash_tool_command(bash_tool): + pytest.run(bash_tool(restart=True)) + result = pytest.run(bash_tool(command="echo 'Hello, World!'")) + assert result.output.strip() == "Hello, World!" + +def test_computer_tool_screenshot(computer_tool): + result = pytest.run(computer_tool(action="screenshot")) + assert result.base64_image is not None + +def test_edit_tool_create(edit_tool): + path = "/tmp/test_file.txt" + result = pytest.run(edit_tool(command="create", path=path, file_text="Hello, World!")) + assert result.output == f"File created successfully at: {path}" + +def test_edit_tool_view(edit_tool): + path = "/tmp/test_file.txt" + pytest.run(edit_tool(command="create", path=path, file_text="Hello, World!")) + result = pytest.run(edit_tool(command="view", path=path)) + assert "Hello, World!" in result.output + +def test_tool_collection_run(tool_collection): + result = pytest.run(tool_collection.run(name="bash", tool_input={"command": "echo 'Hello, World!'"})) + assert result.output.strip() == "Hello, World!" + +def test_tool_collection_invalid(tool_collection): + result = pytest.run(tool_collection.run(name="invalid_tool", tool_input={})) + assert result.error == "Tool invalid_tool is invalid" From 393c35098d164b443efb6d449270da62a5b7b050 Mon Sep 17 00:00:00 2001 From: RaiseYI Date: Fri, 25 Oct 2024 12:47:20 +0800 Subject: [PATCH 2/2] Add section on running tests using `pytest` to `README.md` * **Running Tests** - Update instructions for running tests using `pytest` - Clarify that `pytest` will discover and run all tests in the `tests` directory * **Exiting the Script** - Add a new section on how to quit the script using `Ctrl+C` --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b395190..999e715 100644 --- a/README.md +++ b/README.md @@ -55,9 +55,13 @@ Replace `'Open Safari and look up Anthropic'` with your desired instruction. **Note:** If you do not provide an instruction via the command line, the script will use the default instruction specified in `main.py`. You can edit `main.py` to change this default instruction. +## Exiting the Script + +You can quit the script at any time by pressing `Ctrl+C` in the terminal. + ## Running Tests -To run the tests using `pytest`, follow these steps: +This repository includes automated tests using `pytest`. To run the tests, follow these steps: 1. **Install `pytest` if you haven't already:** @@ -71,11 +75,7 @@ To run the tests using `pytest`, follow these steps: pytest ``` -This will automatically discover and run all the test files in the `tests` directory. - -## Exiting the Script - -You can quit the script at any time by pressing `Ctrl+C` in the terminal. +This will discover and run all the tests in the `tests` directory. ## ⚠ Disclaimer