Skip to content

Commit

Permalink
[Test Proxy] Improve variables API documentation (Azure#25203)
Browse files Browse the repository at this point in the history
  • Loading branch information
mccoyp authored Jul 15, 2022
1 parent 03e60c1 commit 0492dc7
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions doc/dev/test_proxy_migration_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,20 +316,25 @@ class TestExample(AzureRecordedTestCase):

@recorded_by_proxy
def test_example(self, **kwargs):
# in live mode, variables is an empty dictionary
# in playback mode, the value of variables is {"table_name": "random-value"}
variables = kwargs.pop("variables")
if self.is_live:
table_name = "random-value"
variables = {"table_name": table_name}
# In live mode, variables is an empty dictionary
# In playback mode, the value of variables is {"table_name": "random-value"}
variables = kwargs.pop("variables", {})

# To fetch variable values, use the `setdefault` method to look for a key ("table_name")
# and set a real value for that key if it's not present ("random-value")
table_name = variables.setdefault("table_name", "random-value")

# use variables["table_name"] when using the table name throughout the test
...

# return the variables at the end of the test
# return the variables at the end of the test to record them
return variables
```

> **Note:** `variables` will be passed as a named argument to any test that accepts `kwargs` by the test proxy. In
> environments that don't use the test proxy, though -- like live test pipelines -- `variables` won't be provided.
> To avoid a KeyError, providing an empty dictionary as the default value to `kwargs.pop` is recommended.
## Migrate management-plane tests

For management-plane packages, test classes should inherit from [AzureMgmtRecordedTestCase][mgmt_recorded_test_case]
Expand Down

0 comments on commit 0492dc7

Please sign in to comment.