From 87c28a7ec6b3f2c50fecfab7558ac9347b185fee Mon Sep 17 00:00:00 2001 From: Stephane Odul Date: Mon, 4 Dec 2023 13:04:00 -0800 Subject: [PATCH] Address 'addDuration' warnings in python 3.12. This is new to python 3.12 and throws warning on every single test. This adds the new method and related property. Longer term we should probably inherit from the official TestResult class as to ensure better forward compatibility. This depends on #274. --- green/config.py | 7 ++----- green/result.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/green/config.py b/green/config.py index e544167..c938758 100644 --- a/green/config.py +++ b/green/config.py @@ -634,17 +634,14 @@ def getConfig(filepath=None): # pragma: no cover if filepaths: global files_loaded files_loaded = filepaths - # Python 3 has parser.read_file(iterator) while Python2 has - # parser.readfp(obj_with_readline) - read_func = getattr(parser, "read_file", getattr(parser, "readfp")) for filepath in filepaths: # Users are expected to put a [green] section # only if they use setup.cfg if filepath.endswith("setup.cfg"): with open(filepath) as f: - read_func(f) + parser.read_file(f) else: - read_func(ConfigFile(filepath)) + parser.read_file(ConfigFile(filepath)) return parser diff --git a/green/result.py b/green/result.py index 8061cbf..65c54a5 100644 --- a/green/result.py +++ b/green/result.py @@ -165,6 +165,8 @@ def __init__(self, stream, colors): self.stderr_errput = OrderedDict() self.stream = stream self.colors = colors + # The collectedDurations list is new in Python 3.12. + self.collectedDurations = [] def recordStdout(self, test, output): """ @@ -218,6 +220,19 @@ def displayStderr(self, test): ) del self.stderr_errput[test] + def addDuration(self, test, elapsed): + """ + Called when a test finished running, regardless of its outcome. + + New in Python 3.12. + + Args: + test: The test case corresponding to the test method. + elapsed: The time represented in seconds, including the + execution of cleanup functions. + """ + self.collectedDurations.append((str(test), elapsed)) + class ProtoTestResult(BaseTestResult): """ @@ -247,6 +262,7 @@ def __init__(self, start_callback=None, finalize_callback=None): def reinitialize(self): self.shouldStop = False + self.collectedDurations = [] self.errors = [] self.expectedFailures = [] self.failures = [] @@ -392,6 +408,7 @@ def __init__(self, args, stream): self.shouldStop = False self.testsRun = 0 # Individual lists + self.collectedDurations = [] self.errors = [] self.expectedFailures = [] self.failures = []