def _gen_items( self, operation: APIOperation, data_generation_method: DataGenerationMethod ) -> Generator[SchemathesisFunction, None, None]: """Generate all tests for the given API operation. Could produce more than one test item if parametrization is applied via ``pytest.mark.parametrize`` or ``pytest_generate_tests``. This implementation is based on the original one in pytest, but with slight adjustments to produce tests out of hypothesis ones. """ name = self._get_test_name(operation, data_generation_method) funcobj = create_test( operation=operation, test=self.test_function, _given_args=self.given_args, _given_kwargs=self.given_kwargs, data_generation_method=data_generation_method, ) cls = self._get_class_parent() definition: FunctionDefinition = create(FunctionDefinition, name=self.name, parent=self.parent, callobj=funcobj) fixturemanager = self.session._fixturemanager fixtureinfo = fixturemanager.getfixtureinfo(definition, funcobj, cls) metafunc = self._parametrize(cls, definition, fixtureinfo) if not metafunc._calls: yield create( SchemathesisFunction, name=name, parent=self.parent, callobj=funcobj, fixtureinfo=fixtureinfo, test_func=self.test_function, originalname=self.name, data_generation_method=data_generation_method, ) else: fixtures.add_funcarg_pseudo_fixture_def(self.parent, metafunc, fixturemanager) fixtureinfo.prune_dependency_tree() for callspec in metafunc._calls: subname = f"{name}[{callspec.id}]" yield create( SchemathesisFunction, name=subname, parent=self.parent, callspec=callspec, callobj=funcobj, fixtureinfo=fixtureinfo, keywords={callspec.id: True}, originalname=name, test_func=self.test_function, data_generation_method=data_generation_method, )
def _genfunctions(self, name, funcobj): module = self.getparent(Module).obj clscol = self.getparent(Class) cls = clscol and clscol.obj or None transfer_markers(funcobj, cls, module) fm = self.session._fixturemanager fixtureinfo = fm.getfixtureinfo(self, funcobj, cls) metafunc = Metafunc(funcobj, fixtureinfo, self.config, cls=cls, module=module) methods = [] if hasattr(module, "pytest_generate_tests"): methods.append(module.pytest_generate_tests) if hasattr(cls, "pytest_generate_tests"): methods.append(cls().pytest_generate_tests) if methods: self.ihook.pytest_generate_tests.call_extra(methods, dict(metafunc=metafunc)) else: self.ihook.pytest_generate_tests(metafunc=metafunc) Function = self._getcustomclass("Function") if not metafunc._calls: yield Function(name, parent=self, fixtureinfo=fixtureinfo) else: # add funcargs() as fixturedefs to fixtureinfo.arg2fixturedefs fixtures.add_funcarg_pseudo_fixture_def(self, metafunc, fm) for callspec in metafunc._calls: subname = "%s[%s]" % (name, callspec.id) yield Function(name=subname, parent=self, callspec=callspec, callobj=funcobj, fixtureinfo=fixtureinfo, keywords={callspec.id: True}, originalname=name, )
def _genfunctions(self, name, funcobj): fm = self.session._fixturemanager cls = None definition = FunctionDefinition(self.nodeid, parent=self, callobj=funcobj) fixtureinfo = fm.getfixtureinfo(definition, funcobj, cls) metafunc = Metafunc(definition, fixtureinfo, self.config, cls=cls, module=None) self.ihook.pytest_generate_tests(metafunc=metafunc) if not metafunc._calls: yield YAMLItem(name, parent=self) else: # add funcargs() as fixturedefs to fixtureinfo.arg2fixturedefs fixtures.add_funcarg_pseudo_fixture_def(self, metafunc, fm) fixtureinfo.prune_dependency_tree() for callspec in metafunc._calls: subname = "%s[%s]" % (name, callspec.id) yield YAMLItem(subname, parent=self, callspec=callspec, keywords={callspec.id: True}, originalname=name)
def _gen_items(self, endpoint: Endpoint) -> Generator[Function, None, None]: """Generate all items for the given endpoint. Could produce more than one test item if parametrization is applied via ``pytest.mark.parametrize`` or ``pytest_generate_tests``. This implementation is based on the original one in pytest, but with slight adjustments to produce tests out of hypothesis ones. """ name = self._get_test_name(endpoint) funcobj = self._make_test(endpoint) cls = self._get_class_parent() if USE_FROM_PARENT: create_definition = FunctionDefinition.from_parent else: create_definition = FunctionDefinition definition = create_definition(name=self.name, parent=self.parent, callobj=funcobj) fixturemanager = self.session._fixturemanager fixtureinfo = fixturemanager.getfixtureinfo(definition, funcobj, cls) metafunc = self._parametrize(cls, definition, fixtureinfo) if USE_FROM_PARENT: create_function = SchemathesisFunction.from_parent else: create_function = SchemathesisFunction if not metafunc._calls: yield create_function(name=name, parent=self.parent, callobj=funcobj, fixtureinfo=fixtureinfo) else: fixtures.add_funcarg_pseudo_fixture_def(self.parent, metafunc, fixturemanager) fixtureinfo.prune_dependency_tree() for callspec in metafunc._calls: subname = "{}[{}]".format(name, callspec.id) yield create_function( name=subname, parent=self.parent, callspec=callspec, callobj=funcobj, fixtureinfo=fixtureinfo, keywords={callspec.id: True}, originalname=name, )
def _genfunctions(self, name, funcobj): module = self.getparent(Module).obj clscol = self.getparent(Class) cls = clscol and clscol.obj or None fm = self.session._fixturemanager definition = FunctionDefinition(name=name, parent=self, callobj=funcobj) fixtureinfo = fm.getfixtureinfo(definition, funcobj, cls) metafunc = Metafunc( definition, fixtureinfo, self.config, cls=cls, module=module ) methods = [] if hasattr(module, "pytest_generate_tests"): methods.append(module.pytest_generate_tests) if hasattr(cls, "pytest_generate_tests"): methods.append(cls().pytest_generate_tests) if methods: self.ihook.pytest_generate_tests.call_extra( methods, dict(metafunc=metafunc) ) else: self.ihook.pytest_generate_tests(metafunc=metafunc) if not metafunc._calls: yield Function(name, parent=self, fixtureinfo=fixtureinfo) else: # add funcargs() as fixturedefs to fixtureinfo.arg2fixturedefs fixtures.add_funcarg_pseudo_fixture_def(self, metafunc, fm) # add_funcarg_pseudo_fixture_def may have shadowed some fixtures # with direct parametrization, so make sure we update what the # function really needs. fixtureinfo.prune_dependency_tree() for callspec in metafunc._calls: subname = "%s[%s]" % (name, callspec.id) yield Function( name=subname, parent=self, callspec=callspec, callobj=funcobj, fixtureinfo=fixtureinfo, keywords={callspec.id: True}, originalname=name, )
def _gen_items( self, result: Result[APIOperation, InvalidSchema], data_generation_method: DataGenerationMethod ) -> Generator[SchemathesisFunction, None, None]: """Generate all tests for the given API operation. Could produce more than one test item if parametrization is applied via ``pytest.mark.parametrize`` or ``pytest_generate_tests``. This implementation is based on the original one in pytest, but with slight adjustments to produce tests out of hypothesis ones. """ if isinstance(result, Ok): operation = result.ok() if self.is_invalid_test: funcobj = self.test_function else: funcobj = create_test( operation=operation, test=self.test_function, _given_kwargs=self.given_kwargs, data_generation_method=data_generation_method, ) name = self._get_test_name(operation, data_generation_method) else: error = result.err() funcobj = error.as_failing_test_function() name = self.name # `full_path` is always available in this case if error.method: name += f"[{error.method.upper()} {error.full_path}]" else: name += f"[{error.full_path}]" name += f"[{data_generation_method.as_short_name()}]" cls = self._get_class_parent() definition: FunctionDefinition = create(FunctionDefinition, name=self.name, parent=self.parent, callobj=funcobj) fixturemanager = self.session._fixturemanager fixtureinfo = fixturemanager.getfixtureinfo(definition, funcobj, cls) metafunc = self._parametrize(cls, definition, fixtureinfo) if not metafunc._calls: yield create( SchemathesisFunction, name=name, parent=self.parent, callobj=funcobj, fixtureinfo=fixtureinfo, test_func=self.test_function, originalname=self.name, data_generation_method=data_generation_method, ) else: fixtures.add_funcarg_pseudo_fixture_def(self.parent, metafunc, fixturemanager) fixtureinfo.prune_dependency_tree() for callspec in metafunc._calls: subname = f"{name}[{callspec.id}]" yield create( SchemathesisFunction, name=subname, parent=self.parent, callspec=callspec, callobj=funcobj, fixtureinfo=fixtureinfo, keywords={callspec.id: True}, originalname=name, test_func=self.test_function, data_generation_method=data_generation_method, )