-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add crewai test feature (#984)
* feat: add crewai test feature * fix: remove unused import * feat: update docstirng * fix: tests
- Loading branch information
1 parent
349753a
commit 6930656
Showing
7 changed files
with
201 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import subprocess | ||
|
||
import click | ||
|
||
|
||
def test_crew(n_iterations: int, model: str) -> None: | ||
""" | ||
Test the crew by running a command in the Poetry environment. | ||
Args: | ||
n_iterations (int): The number of iterations to test the crew. | ||
model (str): The model to test the crew with. | ||
""" | ||
command = ["poetry", "run", "test", str(n_iterations), model] | ||
|
||
try: | ||
if n_iterations <= 0: | ||
raise ValueError("The number of iterations must be a positive integer.") | ||
|
||
result = subprocess.run(command, capture_output=False, text=True, check=True) | ||
|
||
if result.stderr: | ||
click.echo(result.stderr, err=True) | ||
|
||
except subprocess.CalledProcessError as e: | ||
click.echo(f"An error occurred while testing the crew: {e}", err=True) | ||
click.echo(e.output, err=True) | ||
|
||
except Exception as e: | ||
click.echo(f"An unexpected error occurred: {e}", err=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import subprocess | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from crewai.cli import test_crew | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"n_iterations,model", | ||
[ | ||
(1, "gpt-4o"), | ||
(5, "gpt-3.5-turbo"), | ||
(10, "gpt-4"), | ||
], | ||
) | ||
@mock.patch("crewai.cli.test_crew.subprocess.run") | ||
def test_crew_success(mock_subprocess_run, n_iterations, model): | ||
"""Test the crew function for successful execution.""" | ||
mock_subprocess_run.return_value = subprocess.CompletedProcess( | ||
args=f"poetry run test {n_iterations} {model}", returncode=0 | ||
) | ||
result = test_crew.test_crew(n_iterations, model) | ||
|
||
mock_subprocess_run.assert_called_once_with( | ||
["poetry", "run", "test", str(n_iterations), model], | ||
capture_output=False, | ||
text=True, | ||
check=True, | ||
) | ||
assert result is None | ||
|
||
|
||
@mock.patch("crewai.cli.test_crew.click") | ||
def test_test_crew_zero_iterations(click): | ||
test_crew.test_crew(0, "gpt-4o") | ||
click.echo.assert_called_once_with( | ||
"An unexpected error occurred: The number of iterations must be a positive integer.", | ||
err=True, | ||
) | ||
|
||
|
||
@mock.patch("crewai.cli.test_crew.click") | ||
def test_test_crew_negative_iterations(click): | ||
test_crew.test_crew(-2, "gpt-4o") | ||
click.echo.assert_called_once_with( | ||
"An unexpected error occurred: The number of iterations must be a positive integer.", | ||
err=True, | ||
) | ||
|
||
|
||
@mock.patch("crewai.cli.test_crew.click") | ||
@mock.patch("crewai.cli.test_crew.subprocess.run") | ||
def test_test_crew_called_process_error(mock_subprocess_run, click): | ||
n_iterations = 5 | ||
mock_subprocess_run.side_effect = subprocess.CalledProcessError( | ||
returncode=1, | ||
cmd=["poetry", "run", "test", str(n_iterations), "gpt-4o"], | ||
output="Error", | ||
stderr="Some error occurred", | ||
) | ||
test_crew.test_crew(n_iterations, "gpt-4o") | ||
|
||
mock_subprocess_run.assert_called_once_with( | ||
["poetry", "run", "test", "5", "gpt-4o"], | ||
capture_output=False, | ||
text=True, | ||
check=True, | ||
) | ||
click.echo.assert_has_calls( | ||
[ | ||
mock.call.echo( | ||
"An error occurred while testing the crew: Command '['poetry', 'run', 'test', '5', 'gpt-4o']' returned non-zero exit status 1.", | ||
err=True, | ||
), | ||
mock.call.echo("Error", err=True), | ||
] | ||
) | ||
|
||
|
||
@mock.patch("crewai.cli.test_crew.click") | ||
@mock.patch("crewai.cli.test_crew.subprocess.run") | ||
def test_test_crew_unexpected_exception(mock_subprocess_run, click): | ||
# Arrange | ||
n_iterations = 5 | ||
mock_subprocess_run.side_effect = Exception("Unexpected error") | ||
test_crew.test_crew(n_iterations, "gpt-4o") | ||
|
||
mock_subprocess_run.assert_called_once_with( | ||
["poetry", "run", "test", "5", "gpt-4o"], | ||
capture_output=False, | ||
text=True, | ||
check=True, | ||
) | ||
click.echo.assert_called_once_with( | ||
"An unexpected error occurred: Unexpected error", err=True | ||
) |