Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zope.testrunner fails one test with Python 3.12, succeeding with 3.11 #157

Closed
doko42 opened this issue Dec 5, 2023 · 8 comments · Fixed by #159
Closed

zope.testrunner fails one test with Python 3.12, succeeding with 3.11 #157

doko42 opened this issue Dec 5, 2023 · 8 comments · Fixed by #159

Comments

@doko42
Copy link

doko42 commented Dec 5, 2023

seen when testing with the python 3.12 branch, the test succeed with 3.11.7:

Failure in test /home/packages/zope/zope.testrunner/.pybuild/cpython3_3.12/build/zope/testrunner/
tests/testrunner-colors.rst
Failed doctest test for testrunner-colors.rst
File "/home/packages/zope/zope.testrunner/.pybuild/cpython3_3.12/build/zope/testrunner/tests/te
strunner-colors.rst", line 0


File "/home/packages/zope/zope.testrunner/.pybuild/cpython3_3.12/build/zope/testrunner/tests/testrunner-colors.rst", line 165, in testrunner-colors.rst
Failed example:
_ = testrunner.run_internal(defaults_skip)
Exception raised:
Traceback (most recent call last):
File "/usr/lib/python3.12/doctest.py", line 1359, in __run
exec(compile(example.source, filename, "single",
File "<doctest testrunner-colors.rst[14]>", line 1, in
_ = testrunner.run_internal(defaults_skip)
File "/home/packages/zope/zope.testrunner/.pybuild/cpython3_3.12/build/zope/testrunner/init.py", line 55, in run_internal
runner.run()
File "/home/packages/zope/zope.testrunner/.pybuild/cpython3_3.12/build/zope/testrunner/runner.py", line 191, in run
self.run_tests()
File "/home/packages/zope/zope.testrunner/.pybuild/cpython3_3.12/build/zope/testrunner/runner.py", line 292, in run_tests
self.ran += run_layer(self.options, layer_name, layer, tests,
File "/home/packages/zope/zope.testrunner/.pybuild/cpython3_3.12/build/zope/testrunner/runner.py", line 473, in run_layer
return run_tests(options, tests, layer_name, failures, errors, skipped,
File "/home/packages/zope/zope.testrunner/.pybuild/cpython3_3.12/build/zope/testrunner/runner.py", line 399, in run_tests
test(result)
File "/usr/lib/python3.12/unittest/case.py", line 692, in call
return self.run(*args, **kwds)
File "/usr/lib/python3.12/unittest/case.py", line 662, in run
result.stopTest(self)
File "/home/packages/zope/zope.testrunner/.pybuild/cpython3_3.12/build/zope/testrunner/runner.py", line 1023, in stopTest
test.dict.update(self._test_state)
AttributeError: 'TestResult' object has no attribute '_test_state'

@jugmac00
Copy link
Member

jugmac00 commented Dec 5, 2023

The test suite passed a week ago on Python 3.12 which suggests either a dependency or the test environment has changed.

@mgedmin
Copy link
Member

mgedmin commented Dec 6, 2023

The AttributeError: 'TestResult' object has no attribute '_test_state' error in stopTest() means that stopTest() was called without startTest() being called first. This can happen if any of the statements at the beginning of the try: block in unittest.TestCase.run() raise an exception in https://github.com/python/cpython/blob/399a3f2e1e428710fa4dc5c54841e179dc8e1028/Lib/unittest/case.py#L609-L620, which then causes the finally: to call our stopTest().

When I fix this (and a similar AttributeError about _threads) in stopTest(), I get to see the real failure:

File "/home/mg/src/zopefoundation/zope.testrunner/src/zope/testrunner/tests/testrunner-colors.rst", line 165, in testrunner-colors.rst
Failed example:
    _ = testrunner.run_internal(defaults_skip)
Exception raised:
    Traceback (most recent call last):
      File "/home/mg/opt/python312/lib/python3.12/doctest.py", line 1357, in __run
        exec(compile(example.source, filename, "single",
      File "<doctest testrunner-colors.rst[14]>", line 1, in <module>
        _ = testrunner.run_internal(defaults_skip)
      File "/home/mg/src/zopefoundation/zope.testrunner/src/zope/testrunner/__init__.py", line 55, in run_internal
        runner.run()
      File "/home/mg/src/zopefoundation/zope.testrunner/src/zope/testrunner/runner.py", line 191, in run
        self.run_tests()
      File "/home/mg/src/zopefoundation/zope.testrunner/src/zope/testrunner/runner.py", line 292, in run_tests
        self.ran += run_layer(self.options, layer_name, layer, tests,
      File "/home/mg/src/zopefoundation/zope.testrunner/src/zope/testrunner/runner.py", line 473, in run_layer
        return run_tests(options, tests, layer_name, failures, errors, skipped,
      File "/home/mg/src/zopefoundation/zope.testrunner/src/zope/testrunner/runner.py", line 399, in run_tests
        test(result)
      File "/home/mg/opt/python312/lib/python3.12/unittest/case.py", line 692, in __call__
        return self.run(*args, **kwds)
      File "/home/mg/opt/python312/lib/python3.12/unittest/case.py", line 616, in run
        _addSkip(result, self, skip_why)
      File "/home/mg/opt/python312/lib/python3.12/unittest/case.py", line 89, in _addSkip
        addSkip(test_case, reason)
      File "/home/mg/src/zopefoundation/zope.testrunner/src/zope/testrunner/runner.py", line 955, in addSkip
        self.options.output.test_skipped(test, reason)
      File "/home/mg/src/zopefoundation/zope.testrunner/src/zope/testrunner/formatter.py", line 573, in test_skipped
        self.test_width += len(s) + 1
    AttributeError: 'ColorfulOutputFormatter' object has no attribute 'test_width'. Did you mean: 'last_width'?

@mgedmin
Copy link
Member

mgedmin commented Dec 6, 2023

Same cause: the code for handing skipped tests is now called without start_test() getting called first.

This is a consequence of python/cpython@551aa6a

@mgedmin
Copy link
Member

mgedmin commented Dec 6, 2023

I started working on a fix but quickly got stuck: #158.

@icemac
Copy link
Member

icemac commented Dec 15, 2023

@d-maurer Do you maybe have an idea here, how to solve this issue?

@d-maurer
Copy link
Contributor

d-maurer commented Dec 15, 2023 via email

@d-maurer
Copy link
Contributor

@d-maurer Do you maybe have an idea here, how to solve this issue?

I agree with the analysis of @mgedmin.

Apparently, python/cpython@551aa6a
no longer calls startTest for (most) skipped tests in order to get the exit code right if all tests have been skipped.
This case is so isolated that in my view it is not worth to cause significant trouble. Therefore, I see it a potential approach to use a monkey patch to retain the old "call startTest for skipped tests, too" -- until zope.testrunner only supports versions above 3.12. Maybe, we control such a monkey patch with a new option to support the new counting for skipped tests.

Alternatively, #158 is an alternative. As @mgedmin reports, it still lets many tests fail because the counting of skipped tests has changed. My impression: in most cases, those differences are not relevant for the test and should not cause its failure. The failures result from doctests tending to check also things which are not relevant. In this case, a normalized might be able to hide those differences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants