Skip to content

Conversation

@ueshin
Copy link
Contributor

@ueshin ueshin commented Jul 9, 2025

Fixes unittest to return exit code 5 when setUpClass raises an exception.

import unittest

class BugTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        raise ValueError("Simulated setup failure")

    def test_example(self):
        self.assertTrue(True)
  • before
$ ./python.exe -m unittest bug_test.py
E
======================================================================
ERROR: setUpClass (bug_test.BugTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/.../cpython/bug_test.py", line 6, in setUpClass
    raise ValueError("Simulated setup failure")
ValueError: Simulated setup failure

----------------------------------------------------------------------
Ran 0 tests in 0.000s

FAILED (errors=1)
$ echo $?
5
  • after
$ ./python.exe -m unittest bug_test.py
E
======================================================================
ERROR: setUpClass (bug_test.BugTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/.../cpython/bug_test.py", line 6, in setUpClass
    raise ValueError("Simulated setup failure")
ValueError: Simulated setup failure

----------------------------------------------------------------------
Ran 0 tests in 0.000s

FAILED (errors=1)
$ echo $?
1

@python-cla-bot
Copy link

python-cla-bot bot commented Jul 9, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Jul 9, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link
Member

@gaogaotiantian gaogaotiantian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I think it's good, just a few minor suggestions.

self.assertEqual(cm.exception.code, 1)
out = stream.getvalue()
self.assertIn('\nERROR: setUpClass (test.test_unittest.test_program.'
'Test_TestProgram.SetUpClassFailure)\n', out)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the error message you are trying to catch here is a bit too specific. Part of the strings is related to how the testing infra is designed, we should not test those. I think the actual test case related info is SetUpClassFailure, setUpClass and probably ERROR, let's just write two assertIn for those info.

self.assertEqual(result.testsRun, 2)
self.assertEqual(len(result.errors), 0)

def test_setup_class_raising_error(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this test case related to this issue? Seems like you are trying to test that an exception in setUpClass will stop the test? If it's not directly related, I think it's better to have the commit to include only related tests, so it's easier for us to follow the commit history.

@bedevere-app
Copy link

bedevere-app bot commented Nov 14, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@ueshin
Copy link
Contributor Author

ueshin commented Nov 14, 2025

I have made the requested changes; please review again.

@bedevere-app
Copy link

bedevere-app bot commented Nov 14, 2025

Thanks for making the requested changes!

@gaogaotiantian: please review the changes made to this pull request.

testLoader=self.TestLoader(self.SetUpClassFailure))
self.assertEqual(cm.exception.code, 1)
out = stream.getvalue()
self.assertIn("ERROR", out)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's check "ERROR: setUpClass" and "SetUpClassFailure" here. Otherwise I think it's good!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Thanks!

@gaogaotiantian gaogaotiantian added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Nov 15, 2025
@gaogaotiantian gaogaotiantian merged commit 53d65c8 into python:main Nov 15, 2025
49 of 50 checks passed
@miss-islington-app
Copy link

Thanks @ueshin for the PR, and @gaogaotiantian for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 15, 2025
…aises an exception (pythonGH-136487)

(cherry picked from commit 53d65c8)

Co-authored-by: Takuya UESHIN <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Nov 15, 2025

GH-141576 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Nov 15, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 15, 2025
…aises an exception (pythonGH-136487)

(cherry picked from commit 53d65c8)

Co-authored-by: Takuya UESHIN <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Nov 15, 2025

GH-141577 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Nov 15, 2025
gaogaotiantian pushed a commit that referenced this pull request Nov 15, 2025
…raises an exception (GH-136487) (#141577)

gh-136442: Fix unittest to return exit code 5 when setUpClass raises an exception (GH-136487)
(cherry picked from commit 53d65c8)

Co-authored-by: Takuya UESHIN <[email protected]>
gaogaotiantian pushed a commit that referenced this pull request Nov 15, 2025
…raises an exception (GH-136487) (#141576)

gh-136442: Fix unittest to return exit code 5 when setUpClass raises an exception (GH-136487)
(cherry picked from commit 53d65c8)

Co-authored-by: Takuya UESHIN <[email protected]>
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants