Skip to content

Commit f6b4e0c

Browse files
authored
Add central sleep patching fixtures (#35677)
* Add requestable central sleep patch fixtures * Document sleep patching in tests.md
1 parent 25ac3de commit f6b4e0c

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

doc/dev/tests.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,24 @@ value of `"appId"`, and `AZURE_CLIENT_SECRET` to the value of `"password"`.
248248
The test proxy has to be available in order for tests to work; this is done automatically with a `pytest` fixture.
249249

250250
Create a `conftest.py` file within your package's test directory (`sdk/{service}/{package}/tests`), and inside it add a
251-
session-level fixture that accepts `devtools_testutils.test_proxy` as a parameter (and has `autouse` set to `True`):
251+
session-level fixture that accepts the `test_proxy` fixture as a parameter (and has `autouse` set to `True`):
252252

253253
```python
254254
import pytest
255-
from devtools_testutils import test_proxy
256255

257256
# autouse=True will trigger this fixture on each pytest run, even if it's not explicitly used by a test method
257+
# test_proxy auto-starts the test proxy
258+
# patch_sleep and patch_async_sleep streamline tests by disabling wait times during LRO polling
258259
@pytest.fixture(scope="session", autouse=True)
259-
def start_proxy(test_proxy):
260+
def start_proxy(test_proxy, patch_sleep, patch_async_sleep):
260261
return
261262
```
262263

264+
As shown in the example, it's recommended to also request the `patch_sleep` and `patch_async_sleep` fixtures unless
265+
your tests have a unique need to use `time.sleep` or `asyncio.sleep` during playback tests (this is unusual). All of
266+
these fixtures are imported by the central [`/sdk/conftest.py`][central_conftest], so `pytest` will automatically
267+
resolve the references.
268+
263269
For more details about how this fixture starts up the test proxy, or the test proxy itself, refer to the
264270
[test proxy migration guide][test_proxy_startup].
265271

@@ -576,6 +582,7 @@ For information about more advanced testing scenarios, refer to the [advanced te
576582
[azure_cli_service_principal]: https://docs.microsoft.com/cli/azure/ad/sp?view=azure-cli-latest#az-ad-sp-create-for-rbac
577583
[azure_portal]: https://portal.azure.com/
578584
[azure_recorded_test_case]: https://github.com/Azure/azure-sdk-for-python/blob/7e66e3877519a15c1d4304eb69abf0a2281773/tools/azure-sdk-tools/devtools_testutils/azure_recorded_testcase.py#L44
585+
[central_conftest]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/conftest.py
579586
[env_var_docs]: https://github.com/Azure/azure-sdk-for-python/tree/main/tools/azure-sdk-tools/devtools_testutils#use-the-environmentvariableloader
580587
[env_var_loader]: https://github.com/Azure/azure-sdk-for-python/blob/main/tools/azure-sdk-tools/devtools_testutils/envvariable_loader.py
581588
[generate_sas]: https://github.com/Azure/azure-sdk-for-python/blob/bf4749babb363e2dc972775f4408036e31f361b4/tools/azure-sdk-tools/devtools_testutils/azure_recorded_testcase.py#L196

sdk/conftest.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,35 @@ def reduce_logging_volume(caplog, pytestconfig):
8585
if not (pytestconfig.getoption("log_level") or pytestconfig.getoption("log_cli_level")):
8686
caplog.set_level(30)
8787
yield
88+
89+
90+
@pytest.fixture(scope="session")
91+
def patch_async_sleep():
92+
from unittest import mock
93+
from devtools_testutils import is_live
94+
95+
async def immediate_return(_):
96+
return
97+
98+
if not is_live():
99+
with mock.patch("asyncio.sleep", immediate_return):
100+
yield
101+
102+
else:
103+
yield
104+
105+
106+
@pytest.fixture(scope="session")
107+
def patch_sleep():
108+
from unittest import mock
109+
from devtools_testutils import is_live
110+
111+
def immediate_return(_):
112+
return
113+
114+
if not is_live():
115+
with mock.patch("time.sleep", immediate_return):
116+
yield
117+
118+
else:
119+
yield

0 commit comments

Comments
 (0)