def setup(self): setup_class = _get_xunit_func(self.obj, "setup_class") if setup_class is not None: setup_class = getimfunc(setup_class) setup_class(self.obj) fin_class = getattr(self.obj, "teardown_class", None) if fin_class is not None: fin_class = getimfunc(fin_class) self.addfinalizer(lambda: fin_class(self.obj))
def collect(self): from unittest import TestLoader cls = self.obj if not getattr(cls, "__test__", True): return self.session._fixturemanager.parsefactories(self, unittest=True) loader = TestLoader() module = self.getparent(Module).obj foundsomething = False for name in loader.getTestCaseNames(self.obj): x = getattr(self.obj, name) if not getattr(x, "__test__", True): continue funcobj = getimfunc(x) transfer_markers(funcobj, cls, module) yield TestCaseFunction(name, parent=self, callobj=funcobj) foundsomething = True if not foundsomething: runtest = getattr(self.obj, "runTest", None) if runtest is not None: ut = sys.modules.get("twisted.trial.unittest", None) if ut is None or runtest != ut.TestCase.runTest: yield TestCaseFunction("runTest", parent=self)
def collect(self): from unittest import TestLoader cls = self.obj if not getattr(cls, "__test__", True): return skipped = getattr(cls, "__unittest_skip__", False) if not skipped: self._inject_setup_teardown_fixtures(cls) self._inject_setup_class_fixture() self.session._fixturemanager.parsefactories(self, unittest=True) loader = TestLoader() foundsomething = False for name in loader.getTestCaseNames(self.obj): x = getattr(self.obj, name) if not getattr(x, "__test__", True): continue funcobj = getimfunc(x) yield TestCaseFunction(name, parent=self, callobj=funcobj) foundsomething = True if not foundsomething: runtest = getattr(self.obj, "runTest", None) if runtest is not None: ut = sys.modules.get("twisted.trial.unittest", None) if ut is None or runtest != ut.TestCase.runTest: yield TestCaseFunction("runTest", parent=self)
def pytest_fixture_setup(fixturedef, request): # since we use DataProvider at collection time and BaseProvider in fixtures and tests, # we need to instantiate BaseProvider and replace DataProvider obj with it right before first # provider fixture request. # There were several other ways to do that. However, those bumped into different # scope mismatch issues. if fixturedef.argname == 'provider': kwargs = {} for argname in fixturedef.argnames: fixdef = request._get_active_fixturedef(argname) result, arg_cache_key, exc = fixdef.cached_result request._check_scope(argname, request.scope, fixdef.scope) kwargs[argname] = result fixturefunc = fixturedef.func if request.instance is not None: fixturefunc = getimfunc(fixturedef.func) if fixturefunc != fixturedef.func: fixturefunc = fixturefunc.__get__(request.instance) my_cache_key = request.param_index try: provider_data = call_fixture_func(fixturefunc, request, kwargs) except TEST_OUTCOME: fixturedef.cached_result = (None, my_cache_key, sys.exc_info()) raise from cfme.utils.providers import get_crud provider = get_crud(provider_data.key) fixturedef.cached_result = (provider, my_cache_key, None) request.param = provider yield provider else: yield
def pytest_fixture_setup(fixturedef, request): """ Execution of fixture setup. """ kwargs = {} for argname in fixturedef.argnames: fixdef = request._get_active_fixturedef(argname) result, arg_cache_key, exc = fixdef.cached_result request._check_scope(argname, request.scope, fixdef.scope) kwargs[argname] = result fixturefunc = fixturedef.func if fixturedef.unittest: if request.instance is not None: # bind the unbound method to the TestCase instance fixturefunc = fixturedef.func.__get__(request.instance) else: # the fixture function needs to be bound to the actual # request.instance so that code working with "fixturedef" behaves # as expected. if request.instance is not None: fixturefunc = getimfunc(fixturedef.func) if fixturefunc != fixturedef.func: fixturefunc = fixturefunc.__get__(request.instance) my_cache_key = request.param_index try: result = call_fixture_func(fixturefunc, request, kwargs) except Exception: fixturedef.cached_result = (None, my_cache_key, sys.exc_info()) raise fixturedef.cached_result = (result, my_cache_key, None) return result
def collect(self) -> Iterable[Union[Item, Collector]]: from unittest import TestLoader cls = self.obj if not getattr(cls, "__test__", True): return skipped = _is_skipped(cls) if not skipped: self._inject_setup_teardown_fixtures(cls) self._inject_setup_class_fixture() self.session._fixturemanager.parsefactories(self, unittest=True) loader = TestLoader() foundsomething = False for name in loader.getTestCaseNames(self.obj): x = getattr(self.obj, name) if not getattr(x, "__test__", True): continue funcobj = getimfunc(x) yield TestCaseFunction.from_parent(self, name=name, callobj=funcobj) foundsomething = True if not foundsomething: runtest = getattr(self.obj, "runTest", None) if runtest is not None: ut = sys.modules.get("twisted.trial.unittest", None) # Type ignored because `ut` is an opaque module. if ut is None or runtest != ut.TestCase.runTest: # type: ignore yield TestCaseFunction.from_parent(self, name="runTest")
def pytest_fixture_setup(fixturedef, request): # since we use DataProvider at collection time and BaseProvider in fixtures and tests, # we need to instantiate BaseProvider and replace DataProvider obj with it right before first # provider fixture request. # There were several other ways to do that. However, those bumped into different # scope mismatch issues. # As the object may not be the root object and may have a parent, we need to walk to that # the object to see if we can find the attribute on it or any of its parents if hasattr(_walk_to_obj_parent(request).function, 'provider'): marks = _walk_to_obj_parent(request).function.provider._marks for mark in marks: if mark.kwargs.get('fixture_name', 'provider') == fixturedef.argname: kwargs = {} for argname in fixturedef.argnames: fixdef = request._get_active_fixturedef(argname) result, arg_cache_key, exc = fixdef.cached_result request._check_scope(argname, request.scope, fixdef.scope) kwargs[argname] = result fixturefunc = fixturedef.func if request.instance is not None: fixturefunc = getimfunc(fixturedef.func) if fixturefunc != fixturedef.func: fixturefunc = fixturefunc.__get__(request.instance) my_cache_key = request.param_index try: provider_data = call_fixture_func(fixturefunc, request, kwargs) except TEST_OUTCOME: fixturedef.cached_result = (None, my_cache_key, sys.exc_info()) raise from cfme.utils.providers import get_crud provider = get_crud(provider_data.key) fixturedef.cached_result = (provider, my_cache_key, None) request.param = provider yield provider break else: yield else: yield
def resolve_fixture_function(fixturedef, request): """Gets the actual callable that can be called to obtain the fixture value, dealing with unittest-specific instances and bound methods. """ fixturefunc = fixturedef.func if fixturedef.unittest: if request.instance is not None: # bind the unbound method to the TestCase instance fixturefunc = fixturedef.func.__get__(request.instance) else: # the fixture function needs to be bound to the actual # request.instance so that code working with "fixturedef" behaves # as expected. if request.instance is not None: fixturefunc = getimfunc(fixturedef.func) if fixturefunc != fixturedef.func: fixturefunc = fixturefunc.__get__(request.instance) return fixturefunc
def pytest_fixture_setup(fixturedef, request): # since we use DataProvider at collection time and BaseProvider in fixtures and tests, # we need to instantiate BaseProvider and replace DataProvider obj with it right before first # provider fixture request. # There were several other ways to do that. However, those bumped into different # scope mismatch issues. # As the object may not be the root object and may have a parent, we need to walk to that # the object to see if we can find the attribute on it or any of its parents parent = _walk_to_obj_parent(request) # node has all the markers from full scope # default it to empty dict so loop below shorts and yields at the end item_marks = ProviderEnvironmentMarker.get_closest_kwarg_markers(parent.node) or {} for fixture_name, mark in item_marks.items(): if fixture_name == fixturedef.argname: kwargs = {} for argname in fixturedef.argnames: fixdef = request._get_active_fixturedef(argname) result, arg_cache_key, exc = fixdef.cached_result request._check_scope(argname, request.scope, fixdef.scope) kwargs[argname] = result fixturefunc = fixturedef.func if request.instance is not None: fixturefunc = getimfunc(fixturedef.func) if fixturefunc != fixturedef.func: fixturefunc = fixturefunc.__get__(request.instance) # Use the DataProvider instance as the cache key. my_cache_key = request.param try: provider_data = call_fixture_func(fixturefunc, request, kwargs) except TEST_OUTCOME: fixturedef.cached_result = (None, my_cache_key, sys.exc_info()) raise from cfme.utils.providers import get_crud provider = get_crud(provider_data.key) request.param = provider yield provider # Store the cached_result after we have yielded to other pytest_fixture_setup methods. fixturedef.cached_result = (provider, my_cache_key, None) break else: yield
def collect(self): cls = self.obj if not getattr(cls, "__test__", True): return skipped = getattr(cls, "__unittest_skip__", False) if not skipped: self._inject_setup_teardown_fixtures(cls) self._inject_setup_class_fixture() self.session._fixturemanager.parsefactories(self, unittest=True) for name in self.obj.scenarios: x = getattr(self.obj, name) if not getattr(x, "__test__", True): continue funcobj = getimfunc(x) function = TestCaseFunction(name, parent=self, callobj=funcobj) self.obj.functions.append(function) yield function foundsomething = True
def function(self): "underlying python 'function' object" return getimfunc(self.obj)