CleanCut / green

Green is a clean, colorful, fast python test runner.
MIT License
785 stars 75 forks source link

Python 3.12 support #272

Closed grigi closed 7 months ago

grigi commented 9 months ago

Hi, I was testing the brand-new-released Py3.12 on common libraries I use, and green is failing to run tests with the following error:

Traceback (most recent call last):
  File "/mnt/old/home/grigi/work/magneto/.pyenv/bin/green", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/mnt/old/home/grigi/work/magneto/.pyenv/lib/python3.12/site-packages/green/cmdline.py", line 89, in main
    return _main(argv, testing)
           ^^^^^^^^^^^^^^^^^^^^
  File "/mnt/old/home/grigi/work/magneto/.pyenv/lib/python3.12/site-packages/green/cmdline.py", line 12, in _main
    args = config.mergeConfig(args, testing)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/old/home/grigi/work/magneto/.pyenv/lib/python3.12/site-packages/green/config.py", line 677, in mergeConfig
    config = getConfig(getattr(args, "config", default_args.config))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/old/home/grigi/work/magneto/.pyenv/lib/python3.12/site-packages/green/config.py", line 648, in getConfig
    read_func = getattr(parser, "read_file", getattr(parser, "readfp"))
                                             ^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'ConfigParser' object has no attribute 'readfp'. Did you mean: 'read'?

It appears to be related to this change: https://docs.python.org/3.12/whatsnew/3.12.html#configparser

Running the tests with python -m unittest works as expected.

sodul commented 7 months ago

I'm able to run tests with python 3.12.0 but I get a new warning.

import unittest

class DurationTestCase(unittest.TestCase):

    def test_pass(self):
        pass

And with Green we get:

> green test_vanilla.py
.

Captured stderr for test_vanilla.DurationTestCase.test_pass
/Users/stephane/.pyenv/versions/3.12.0/lib/python3.12/unittest/case.py:580: RuntimeWarning: TestResult has no addDuration method
  warnings.warn("TestResult has no addDuration method",

Ran 1 test in 0.221s using 10 processes

OK (passes=1)

@CleanCut I can probably take a look over the long weekend but if you are around I would like to discuss a few things:

sodul commented 7 months ago

This is not necessarily what I would PR, but this patch got the warnings to go away with Python 3.12.0:

--- result.py.orig      2023-11-28 12:33:02
+++ result.py   2023-11-28 12:41:44
@@ -172,6 +172,7 @@
         self.stderr_errput = OrderedDict()
         self.stream = stream
         self.colors = colors
+        self.collectedDurations = []

     def recordStdout(self, test, output):
         """
@@ -224,6 +225,17 @@
                 )
             )
             del self.stderr_errput[test]
+
+    def addDuration(self, test, elapsed):
+        """
+        Called when a test finished to run, regardless of its outcome.
+
+        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):
@@ -255,6 +267,7 @@
     def reinitialize(self):
         self.shouldStop = False
         self.errors = []
+        self.collectedDurations = []
         self.expectedFailures = []
         self.failures = []
         self.passing = []
@@ -399,6 +412,7 @@
         self.shouldStop = False
         self.testsRun = 0
         # Individual lists
+        self.collectedDurations = []
         self.errors = []
         self.expectedFailures = []
         self.failures = []

Posting here so others can get unstuck while a new release can get published with a full fix.

CleanCut commented 7 months ago

@sodul Cool! Feel free to open up a PR. 👍

CleanCut commented 7 months ago

Sidenote: Sorry for the delay(s) in my response(s). I'm going through some major life changes at the moment which are consuming all of my time. I'll try to check in more often, and I welcome all the help I can get.