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

gh-105310: add decorator to skip rerunning tests #105311

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2457,3 +2457,34 @@ def adjust_int_max_str_digits(max_digits):

#For recursion tests, easily exceeds default recursion limit
EXCEEDS_RECURSION_LIMIT = 5000

# A decorator to skip tests that are not rerunnable.
def skip_rerun(reason):
"""
This decorator skips the decorated test case
if it has already been run.
"""
def decorator(test_case):

original_setUpClass = test_case.setUpClass
original_tearDownClass = test_case.tearDownClass

test_case._has_run = False

@classmethod
def setUpClass(cls):
if cls._has_run:
raise unittest.SkipTest(reason)
original_setUpClass()

@classmethod
def tearDownClass(cls):
original_tearDownClass()
cls._has_run = True

test_case.setUpClass = setUpClass
test_case.tearDownClass = tearDownClass

return test_case

return decorator
26 changes: 26 additions & 0 deletions Lib/test/test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from test.support import script_helper
from test.support import socket_helper
from test.support import warnings_helper
from test.support import skip_rerun

TESTFN = os_helper.TESTFN

Expand Down Expand Up @@ -684,6 +685,31 @@ def test_has_strftime_extensions(self):
else:
self.assertTrue(support.has_strftime_extensions)

def test_skip_rerun(self):
@skip_rerun("This test case does not support rerunning")
class TestCase(unittest.TestCase):
counter = 0

def test(self):
self.assertEqual(self.counter, 0)
self.__class__.counter += 1
self.assertEqual(self.counter, 1)

test_case = TestCase("test")

output = io.StringIO()
unittest.TextTestRunner(output).run(unittest.TestSuite([test_case]))
output.seek(0)
self.assertRegex(output.read(), "Ran 1 test")

output = io.StringIO()
unittest.TextTestRunner(output).run(unittest.TestSuite([test_case]))
output.seek(0)
self.assertRegex(output.read(), "Ran 0 tests")

self.assertEqual(TestCase.counter, 1)


# XXX -follows a list of untested API
# make_legacy_pyc
# is_resource_enabled
Expand Down