Skip to content

Commit

Permalink
adding class method for awaiting async tests (#14738)
Browse files Browse the repository at this point in the history
  • Loading branch information
seankane-msft authored Oct 23, 2020
1 parent b86ec1e commit 73b6f76
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tools/azure-sdk-tools/devtools_testutils/azure_testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#--------------------------------------------------------------------------
import functools
import inspect
import os.path
import sys
Expand All @@ -20,6 +21,7 @@
AuthenticationMetadataFilter, OAuthRequestResponsesFilter
)
from azure_devtools.scenario_tests.config import TestConfig
from azure_devtools.scenario_tests.utilities import trim_kwargs_from_test_function

from .config import TEST_SETTING_FILENAME
from . import mgmt_settings_fake as fake_settings
Expand Down Expand Up @@ -262,3 +264,21 @@ def get_preparer_resource_name(self, prefix):
If prefix is a blank string, use the fully qualified test name instead.
This is what legacy tests do for resource groups."""
return self.get_resource_name(prefix or self.qualified_test_name.replace('.', '_'))

@staticmethod
def await_prepared_test(test_fn):
"""Synchronous wrapper for async test methods. Used to avoid making changes
upstream to AbstractPreparer (which doesn't await the functions it wraps)
"""

if sys.version_info < (3, 5):
raise ImportError("Async wrapper is not needed for Python 2.7 code.")

import asyncio
@functools.wraps(test_fn)
def run(test_class_instance, *args, **kwargs):
trim_kwargs_from_test_function(test_fn, kwargs)
loop = asyncio.get_event_loop()
return loop.run_until_complete(test_fn(test_class_instance, **kwargs))

return run

0 comments on commit 73b6f76

Please sign in to comment.