Skip to content

Commit

Permalink
Address 'addDuration' warnings in python 3.12.
Browse files Browse the repository at this point in the history
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 CleanCut#274.
  • Loading branch information
sodul committed Dec 4, 2023
1 parent ff8d950 commit 87c28a7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
7 changes: 2 additions & 5 deletions green/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions green/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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 = []
Expand Down

0 comments on commit 87c28a7

Please sign in to comment.