def runpytest_inprocess(self, *args, **kwargs): """Return result of running pytest in-process, providing a similar interface to what self.runpytest() provides. """ syspathinsert = kwargs.pop("syspathinsert", False) if syspathinsert: self.syspathinsert() now = time.time() capture = MultiCapture(Capture=SysCapture) capture.start_capturing() try: try: reprec = self.inline_run(*args, **kwargs) except SystemExit as e: class reprec: ret = e.args[0] except Exception: traceback.print_exc() class reprec: ret = 3 finally: out, err = capture.readouterr() capture.stop_capturing() sys.stdout.write(out) sys.stderr.write(err) res = RunResult(reprec.ret, out.split("\n"), err.split("\n"), time.time() - now) res.reprec = reprec return res
def record_stdout( disable_recording: bool, record_stdout_markers: List[Mark], record_mode: str, request: SubRequest, ): marker = request.node.get_closest_marker("record_stdout") if disable_recording: yield None elif marker: # SETUP TEST DETAILS module_dir = request.node.fspath.dirname module_name = request.node.fspath.purebasename test_name = request.node.name # FORMAT MARKER'S KEYWORD ARGUMENTS formatted_kwargs = record_stdout_format_kwargs( test_name=test_name, record_mode=record_mode, record_stdout_markers=record_stdout_markers, ) # SETUP RECORDER path_template = PathTemplate( module_dir=module_dir, module_name=module_name, test_name=formatted_kwargs["record_name"], ) recorder = Recorder(path_template=path_template, record_mode=formatted_kwargs["record_mode"]) # CAPTURE STDOUT capture = request.config.getoption("--capture") if capture == "no": global_capturing = MultiCapture(in_=SysCapture(0), out=SysCapture(1), err=SysCapture(2)) global_capturing.start_capturing() yield recorder.capture( captured=global_capturing.readouterr().out, strip=formatted_kwargs["strip"], ) global_capturing.stop_capturing() else: capsys = request.getfixturevalue("capsys") yield recorder.capture(captured=capsys.readouterr().out, strip=formatted_kwargs["strip"]) # SAVE/CHECK RECORD if formatted_kwargs["save_record"]: recorder.persist() recorder.assert_equal() recorder.assert_in_list(in_list=formatted_kwargs["assert_in_list"]) else: recorder.assert_in_list(in_list=formatted_kwargs["assert_in_list"]) else: yield None
def runpytest_inprocess(self, *args, **kwargs): """ Return result of running pytest in-process, providing a similar interface to what self.runpytest() provides. """ if kwargs.get("syspathinsert"): self.syspathinsert() now = time.time() capture = MultiCapture(Capture=SysCapture) capture.start_capturing() try: try: reprec = self.inline_run(*args, **kwargs) except SystemExit as e: class reprec: ret = e.args[0] except Exception: traceback.print_exc() class reprec: ret = 3 finally: out, err = capture.readouterr() capture.stop_capturing() sys.stdout.write(out) sys.stderr.write(err) res = RunResult(reprec.ret, out.split("\n"), err.split("\n"), time.time()-now) res.reprec = reprec return res
def _invoke(self, cli_object, args=None, binary_capture=False): exc_info = None exception = None exit_code = 0 # Temporarily redirect sys.stdin to /dev/null to ensure that # Popen doesn't attempt to read pytest's dummy stdin. old_stdin = sys.stdin with open(os.devnull) as devnull: sys.stdin = devnull capture_kind = FDCaptureBinary if binary_capture else FDCapture capture = MultiCapture(out=capture_kind(1), err=capture_kind(2), in_=None) capture.start_capturing() try: cli_object.main(args=args or (), prog_name=cli_object.name) except SystemExit as e: if e.code != 0: exception = e exc_info = sys.exc_info() exit_code = e.code if not isinstance(exit_code, int): sys.stdout.write("Program exit code was not an integer: ") sys.stdout.write(str(exit_code)) sys.stdout.write("\n") exit_code = 1 except Exception as e: # pylint: disable=broad-except exception = e exit_code = -1 exc_info = sys.exc_info() finally: sys.stdout.flush() sys.stdin = old_stdin out, err = capture.readouterr() capture.stop_capturing() return Result(exit_code=exit_code, exception=exception, exc_info=exc_info, output=out, stderr=err)
def invoke(self, cli, args=None, color=False, **extra): exc_info = None exception = None exit_code = 0 # Temporarily redirect sys.stdin to /dev/null to ensure that # Popen doesn't attempt to read pytest's dummy stdin. old_stdin = sys.stdin with open(os.devnull) as devnull: sys.stdin = devnull capture = MultiCapture(in_=None, out=FDCapture(1), err=FDCapture(2)) capture.start_capturing() try: cli.main(args=args or (), prog_name=cli.name, **extra) except SystemExit as e: if e.code != 0: exception = e exc_info = sys.exc_info() exit_code = e.code if not isinstance(exit_code, int): sys.stdout.write('Program exit code was not an integer: ') sys.stdout.write(str(exit_code)) sys.stdout.write('\n') exit_code = 1 except Exception as e: exception = e exit_code = -1 exc_info = sys.exc_info() finally: sys.stdout.flush() sys.stdin = old_stdin out, err = capture.readouterr() capture.stop_capturing() return Result(exit_code=exit_code, exception=exception, exc_info=exc_info, output=out, stderr=err)