Skip to content

Commit 0492dc7

Browse files
authored
[Test Proxy] Improve variables API documentation (Azure#25203)
1 parent 03e60c1 commit 0492dc7

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

doc/dev/test_proxy_migration_guide.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -316,20 +316,25 @@ class TestExample(AzureRecordedTestCase):
316316

317317
@recorded_by_proxy
318318
def test_example(self, **kwargs):
319-
# in live mode, variables is an empty dictionary
320-
# in playback mode, the value of variables is {"table_name": "random-value"}
321-
variables = kwargs.pop("variables")
322-
if self.is_live:
323-
table_name = "random-value"
324-
variables = {"table_name": table_name}
319+
# In live mode, variables is an empty dictionary
320+
# In playback mode, the value of variables is {"table_name": "random-value"}
321+
variables = kwargs.pop("variables", {})
322+
323+
# To fetch variable values, use the `setdefault` method to look for a key ("table_name")
324+
# and set a real value for that key if it's not present ("random-value")
325+
table_name = variables.setdefault("table_name", "random-value")
325326

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

329-
# return the variables at the end of the test
330+
# return the variables at the end of the test to record them
330331
return variables
331332
```
332333

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

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

0 commit comments

Comments
 (0)