def runtest(self): fm = self.session._fixturemanager self._fixtureinfo = fm.getfixtureinfo(node=self, func=self.test_fn, cls=None) try: # Pytest deprecated direct construction of this, but there doesn't appear to # be an obvious non-deprecated way to produce `pytest.Item`s (i.e. tests) # which fullfill fixtures depended on by this plugin. fixture_request = fixtures.FixtureRequest(self, _ispytest=True) except TypeError: # For backwards compatibility, attempt to make the `fixture_request` in the interface # shape pre pytest's addition of this warning-producing parameter. fixture_request = fixtures.FixtureRequest(self) except Exception: # Just to avoid a NameError in an unforeseen error constructing the `fixture_request`. raise NotImplementedError( "Failed to fill the fixtures. " "This is almost certainly a pytest version incompatibility, please submit a bug report!" ) fixture_request._fillfixtures() params = { arg: self.funcargs[arg] for arg in self._fixtureinfo.argnames } self.test_fn(**params)
def _setup_fixtures(xdoctest_item): """ Used by XDoctestTextfile and XDoctestItem to setup fixture information. """ def func(): pass xdoctest_item.funcargs = {} fm = xdoctest_item.session._fixturemanager xdoctest_item._fixtureinfo = fm.getfixtureinfo(node=xdoctest_item, func=func, cls=None, funcargs=False) # Note: FixtureRequest may change in the future, we are using # private functionality. Hopefully it wont break, but we should # check to see if there is a better way to do this # https://github.com/pytest-dev/pytest/discussions/8512#discussioncomment-563347 if _PYTEST_IS_GE_620: # The "_ispytest" arg was added in 3.6.1 fixture_request = fixtures.FixtureRequest(xdoctest_item, _ispytest=True) else: fixture_request = fixtures.FixtureRequest(xdoctest_item) fixture_request._fillfixtures() return fixture_request
def initialise_fixture_attrs(self): # pylint: disable=protected-access,attribute-defined-outside-init self.funcargs = {} fixtureinfo = self.session._fixturemanager.getfixtureinfo( self, self.obj, type(self), funcargs=False) self._fixtureinfo = fixtureinfo self.fixturenames = fixtureinfo.names_closure self._request = fixtures.FixtureRequest(self)
def request_fixtures(self, names): # pytest fixtures dance: fm = self.session._fixturemanager names_closure, arg2fixturedefs = fm.getfixtureclosure(names, self) fixtureinfo = FuncFixtureInfo(names, names_closure, arg2fixturedefs) self._fixtureinfo = fixtureinfo self.funcargs = {} self._request = fixtures.FixtureRequest(self)
def _initrequest(self): self.funcargs = {} if self._isyieldedfunction(): assert not hasattr(self, "callspec"), ( "yielded functions (deprecated) cannot have funcargs") else: if hasattr(self, "callspec"): callspec = self.callspec assert not callspec.funcargs self._genid = callspec.id if hasattr(callspec, "param"): self.param = callspec.param self._request = fixtures.FixtureRequest(self)
def runtest(self) -> None: fm = getattr(self.session, "_fixturemanager") fixture_info = fm.getfixtureinfo( node=self, func=self.function, cls=None, ) fixture_request = fixtures.FixtureRequest(self) fixture_request._fillfixtures() # noqa params = {arg: self.funcargs[arg] for arg in fixture_info.argnames} self.function(**params)
def initialise_fixture_attrs(self): # pylint: disable=protected-access,attribute-defined-outside-init self.funcargs = {} # _get_direct_parametrize_args checks parametrize arguments in Python # functions, but we don't care about that in Tavern. self.session._fixturemanager._get_direct_parametrize_args = lambda _: [ ] fixtureinfo = self.session._fixturemanager.getfixtureinfo( self, self.obj, type(self), funcargs=False) self._fixtureinfo = fixtureinfo self.fixturenames = fixtureinfo.names_closure self._request = fixtures.FixtureRequest(self)
def _setup_fixtures(xdoctest_item): """ Used by XDoctestTextfile and XDoctestItem to setup fixture information. """ def func(): pass xdoctest_item.funcargs = {} fm = xdoctest_item.session._fixturemanager xdoctest_item._fixtureinfo = fm.getfixtureinfo(node=xdoctest_item, func=func, cls=None, funcargs=False) fixture_request = fixtures.FixtureRequest(xdoctest_item) fixture_request._fillfixtures() return fixture_request
def runtest(self): fm = self.session._fixturemanager self._fixtureinfo = fm.getfixtureinfo(node=self, func=self.test_fn, cls=None) fixture_request = fixtures.FixtureRequest(self) fixture_request._fillfixtures() params = { arg: self.funcargs[arg] for arg in self._fixtureinfo.argnames } self.test_fn(**params)
def request_fixtures(self, names): # pytest fixtures dance: fm = self.session._fixturemanager closure = fm.getfixtureclosure(names, self) try: initialnames, names_closure, arg2fixturedefs = closure except ValueError: # pragma: no cover # pytest < 3.7 names_closure, arg2fixturedefs = closure fixtureinfo = FuncFixtureInfo(names, names_closure, arg2fixturedefs) else: # pyest >= 3.7 fixtureinfo = FuncFixtureInfo(names, initialnames, names_closure, arg2fixturedefs) self._fixtureinfo = fixtureinfo self.funcargs = {} self._request = fixtures.FixtureRequest(self)
def runtest(self): "Called by pytest to run the actual test" # Build the tmpdir fixture request self.funcargs = {} request = fixtures.FixtureRequest(self) request._fillfixtures() # Switch to this function self.funcargs["tmpdir"].chdir() if self.test_cmd.endswith(".py"): # We are running a python script. Run it in-process for speed # Save the old command line arguments prior_argv = sys.argv # TBX RULE: Tests rely on old relative-import behaviour prior_path = list(sys.path) dir_path = py.path.local(self.test_cmd).dirname try: sys.argv = self.full_cmd sys.path.insert(0, dir_path) runpy.run_path(self.test_cmd, run_name="__main__") except SystemExit as e: if e.code != 0: raise LibTBXTestException("Script exited with non-zero error code") finally: sys.argv = prior_argv sys.path = prior_path else: print("Procrunning ", self.test_cmd) # Not a python script. Assume that we can run as an external program result = procrunner.run( self.full_cmd, print_stdout=False, print_stderr=False ) self.add_report_section("call", "stdout", result["stdout"]) self.add_report_section("call", "stderr", result["stderr"]) if result["stderr"] or result["exitcode"] != 0: raise LibTBXTestException("Script exited with non-zero error code")