fix(testing): pdb post mortem (#28156)

This commit is contained in:
David Arnold 2024-10-17 20:52:25 +02:00 committed by GitHub
parent 60df96ce08
commit 2f9fd308e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 23 deletions

View file

@ -115,6 +115,7 @@ def main(
verbosity=2 if testing_module_logger.getEffectiveLevel() < logging.INFO else 1,
tb_locals=testing_module_logger.getEffectiveLevel() <= logging.INFO,
cfg=test_config,
buffer=not bool(pdb_on_exceptions),
)
if doctype or doctype_list_path:

View file

@ -34,6 +34,12 @@ class TestResult(unittest.TextTestResult):
self._old_stdout = []
self._old_stderr = []
def _setupStdout(self):
pass
def _restoreStdout(self):
pass
def startTestRun(self):
if not sys.warnoptions:
import warnings
@ -44,16 +50,18 @@ class TestResult(unittest.TextTestResult):
warnings.filterwarnings("module", category=FrappeDeprecationWarning)
# capture class & module setup & teardown in order to show it above the first test of the class
self._old_stderr.append(sys.stderr)
self._old_stdout.append(sys.stdout)
self._module_or_class_stdout_capture = io.StringIO()
self._module_or_class_stderr_capture = io.StringIO()
sys.stdout = self._module_or_class_stdout_capture
sys.stderr = self._module_or_class_stderr_capture
if self.buffer:
self._old_stderr.append(sys.stderr)
self._old_stdout.append(sys.stdout)
self._module_or_class_stdout_capture = io.StringIO()
self._module_or_class_stderr_capture = io.StringIO()
sys.stdout = self._module_or_class_stdout_capture
sys.stderr = self._module_or_class_stderr_capture
def stopTestRun(self):
sys.stdout = self._old_stdout.pop()
sys.stderr = self._old_stderr.pop()
if self.buffer:
sys.stdout = self._old_stdout.pop()
sys.stderr = self._old_stderr.pop()
def startTest(self, test):
self.tb_locals = True
@ -87,23 +95,25 @@ class TestResult(unittest.TextTestResult):
logger.info(f"records created: {', '.join(records)}")
self.stream.flush()
self._old_stderr.append(sys.stderr)
self._old_stdout.append(sys.stdout)
self._test_stdout_capture = io.StringIO()
self._test_stderr_capture = io.StringIO()
sys.stdout = self._test_stdout_capture
sys.stderr = self._test_stderr_capture
if self.buffer:
self._old_stderr.append(sys.stderr)
self._old_stdout.append(sys.stdout)
self._test_stdout_capture = io.StringIO()
self._test_stderr_capture = io.StringIO()
sys.stdout = self._test_stdout_capture
sys.stderr = self._test_stderr_capture
def stopTest(self, test):
super().stopTest(test)
sys.stdout = self._old_stderr.pop()
sys.stderr = self._old_stdout.pop()
for line in self._test_stdout_capture.getvalue().splitlines():
self.stream.write(f"{line}\n")
self.stream.flush()
for line in self._test_stderr_capture.getvalue().splitlines():
self.stream.write(f"{line}\n")
self.stream.flush()
if self.buffer:
sys.stdout = self._old_stderr.pop()
sys.stderr = self._old_stdout.pop()
for line in self._test_stdout_capture.getvalue().splitlines():
self.stream.write(f"{line}\n")
self.stream.flush()
for line in self._test_stderr_capture.getvalue().splitlines():
self.stream.write(f"{line}\n")
self.stream.flush()
def getTestMethodName(self, test):
return test._testMethodName if hasattr(test, "_testMethodName") else str(test)

View file

@ -52,7 +52,7 @@ class TestRunner(unittest.TextTestRunner):
descriptions=True,
verbosity=1,
failfast=False,
buffer=False,
buffer=True,
resultclass=None,
warnings="module",
*,