-
Notifications
You must be signed in to change notification settings - Fork 3.2k
code clean up and added readme #44525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
howieleung
wants to merge
6
commits into
main
Choose a base branch
from
howie/sample-recording-5
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
65a738c
code clean up and added readme
howieleung a4afb39
restore patched_open_fn
howieleung 4e0636f
fix bugs and resolved comment
howieleung 3d6966f
clean up
howieleung 2fd02c0
update test endpoint
howieleung 98cca73
update
howieleung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,110 @@ | ||
| ## Recorded sample tests | ||
|
|
||
| Use recorded tests to validate samples with `SyncSampleExecutor` and `AsyncSampleExecutor`. Tests run the sample code, fail on exceptions, and can optionally validate captured `print` output through LLM instructions. | ||
|
|
||
| ### Prerequisites | ||
| - In `.env`, set the variables required by your samples plus `AZURE_TEST_RUN_LIVE=true` and `AZURE_SKIP_LIVE_RECORDING=false` when capturing recordings. CI playback typically uses `AZURE_TEST_RUN_LIVE=false` and `AZURE_SKIP_LIVE_RECORDING=true`. | ||
| - Provide sanitized defaults via `servicePreparer` so recordings do not leak secrets. | ||
|
|
||
| ### Sync example | ||
| ```python | ||
| import pytest | ||
| import os | ||
| from devtools_testutils import recorded_by_proxy, AzureRecordedTestCase, RecordedTransport | ||
| from test_base import servicePreparer | ||
| from sample_executor import SyncSampleExecutor, get_sample_paths, SamplePathPasser | ||
| from test_samples_helpers import agent_tools_instructions, get_sample_environment_variables_map | ||
|
|
||
| class TestSamples(AzureRecordedTestCase): | ||
| @servicePreparer() | ||
| @pytest.mark.parametrize( | ||
| "sample_path", | ||
| get_sample_paths( | ||
| "agents/tools", | ||
| samples_to_skip=[ | ||
| "sample_agent_bing_custom_search.py", | ||
| "sample_agent_bing_grounding.py", | ||
| "sample_agent_browser_automation.py", | ||
| "sample_agent_fabric.py", | ||
| "sample_agent_mcp_with_project_connection.py", | ||
| "sample_agent_openapi_with_project_connection.py", | ||
| "sample_agent_to_agent.py", | ||
| ], | ||
| ), | ||
| ) | ||
| @SamplePathPasser() | ||
| @recorded_by_proxy(RecordedTransport.AZURE_CORE, RecordedTransport.HTTPX) | ||
| def test_agent_tools_samples(self, sample_path: str, **kwargs) -> None: | ||
| env_var_mapping = get_sample_environment_variables_map() | ||
| executor = SyncSampleExecutor(self, sample_path, env_var_mapping, **kwargs) | ||
| executor.execute() | ||
| executor.validate_print_calls_by_llm( | ||
| instructions=agent_tools_instructions, | ||
| project_endpoint=kwargs["azure_ai_projects_tests_project_endpoint"], | ||
| ) | ||
| ``` | ||
|
|
||
| ### Async example | ||
| ```python | ||
| import pytest | ||
| from devtools_testutils.aio import recorded_by_proxy_async | ||
| import os | ||
| from devtools_testutils import AzureRecordedTestCase, RecordedTransport | ||
| from test_base import servicePreparer | ||
| from sample_executor import AsyncSampleExecutor, get_async_sample_paths, SamplePathPasser | ||
| from test_samples_helpers import agent_tools_instructions, get_sample_environment_variables_map | ||
|
|
||
| class TestSamplesAsync(AzureRecordedTestCase): | ||
|
|
||
| @servicePreparer() | ||
| @pytest.mark.parametrize( | ||
| "sample_path", | ||
| get_async_sample_paths( | ||
| "agents/tools", | ||
| samples_to_skip=[ | ||
| "sample_agent_mcp_with_project_connection_async.py", | ||
| ], | ||
| ), | ||
| ) | ||
| @SamplePathPasser() | ||
| @recorded_by_proxy_async(RecordedTransport.AZURE_CORE, RecordedTransport.HTTPX) | ||
| async def test_agent_tools_samples_async(self, sample_path: str, **kwargs) -> None: | ||
| env_var_mapping = get_sample_environment_variables_map() | ||
| executor = AsyncSampleExecutor(self, sample_path, env_var_mapping, **kwargs) | ||
| await executor.execute_async() | ||
| await executor.validate_print_calls_by_llm_async( | ||
| instructions=agent_tools_instructions, | ||
| project_endpoint=kwargs["azure_ai_projects_tests_project_endpoint"], | ||
| ) | ||
| ``` | ||
|
|
||
| ### Key pieces | ||
| - `@servicePreparer()`: Supplies sanitized environment variables for playback (often via `EnvironmentVariableLoader`). Use an empty string as the prefix if your variables do not share one. | ||
| - Example: | ||
| ```python | ||
| import functools | ||
| from devtools_testutils import EnvironmentVariableLoader | ||
|
|
||
| servicePreparer = functools.partial( | ||
| EnvironmentVariableLoader, | ||
| "azure_ai_projects_tests", | ||
| azure_ai_projects_tests_project_endpoint="https://sanitized-account-name.services.ai.azure.com/api/projects/sanitized-project-name", | ||
| azure_ai_projects_tests_model_deployment_name="gpt-4o", | ||
| # add other sanitized vars here | ||
| ) | ||
| ``` | ||
| - `@pytest.mark.parametrize`: Drives one test per sample file. Use `samples_to_test` or `samples_to_skip` with `get_sample_paths` / `get_async_sample_paths`. | ||
| - `@SamplePathPasser`: Forwards the sample path to the recorder decorators. | ||
| - `recorded_by_proxy` / `recorded_by_proxy_async`: Wrap tests for recording/playback. Include `RecordedTransport.HTTPX` when samples use httpx in addition to the default `RecordedTransport.AZURE_CORE`. | ||
| - `get_sample_environment_variables_map`: Map test env vars to the names expected by samples. Pass `{}` if you already export the sample variables directly. Example: | ||
| ```python | ||
| def get_sample_environment_variables_map(operation_group: str | None = None) -> dict[str, str]: | ||
| return { | ||
| "AZURE_AI_PROJECT_ENDPOINT": "azure_ai_projects_tests_project_endpoint", | ||
| "AZURE_AI_MODEL_DEPLOYMENT_NAME": "azure_ai_projects_tests_model_deployment_name", | ||
| # add other mappings as needed | ||
| } | ||
| ``` | ||
| - `execute` / `execute_async`: Run the sample; any exception fails the test. | ||
| - `validate_print_calls_by_llm` / `validate_print_calls_by_llm_async`: Optionally validate captured print output with LLM instructions and an explicit `project_endpoint` (and optional `model`). | ||
| - `kwargs` in the test function: A dictionary with environment variables in key and value pairs. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.