Skip to content

Commit 73b6f76

Browse files
adding class method for awaiting async tests (#14738)
1 parent b86ec1e commit 73b6f76

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

tools/azure-sdk-tools/devtools_testutils/azure_testcase.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Licensed under the MIT License. See License.txt in the project root for
44
# license information.
55
#--------------------------------------------------------------------------
6+
import functools
67
import inspect
78
import os.path
89
import sys
@@ -20,6 +21,7 @@
2021
AuthenticationMetadataFilter, OAuthRequestResponsesFilter
2122
)
2223
from azure_devtools.scenario_tests.config import TestConfig
24+
from azure_devtools.scenario_tests.utilities import trim_kwargs_from_test_function
2325

2426
from .config import TEST_SETTING_FILENAME
2527
from . import mgmt_settings_fake as fake_settings
@@ -262,3 +264,21 @@ def get_preparer_resource_name(self, prefix):
262264
If prefix is a blank string, use the fully qualified test name instead.
263265
This is what legacy tests do for resource groups."""
264266
return self.get_resource_name(prefix or self.qualified_test_name.replace('.', '_'))
267+
268+
@staticmethod
269+
def await_prepared_test(test_fn):
270+
"""Synchronous wrapper for async test methods. Used to avoid making changes
271+
upstream to AbstractPreparer (which doesn't await the functions it wraps)
272+
"""
273+
274+
if sys.version_info < (3, 5):
275+
raise ImportError("Async wrapper is not needed for Python 2.7 code.")
276+
277+
import asyncio
278+
@functools.wraps(test_fn)
279+
def run(test_class_instance, *args, **kwargs):
280+
trim_kwargs_from_test_function(test_fn, kwargs)
281+
loop = asyncio.get_event_loop()
282+
return loop.run_until_complete(test_fn(test_class_instance, **kwargs))
283+
284+
return run

0 commit comments

Comments
 (0)