Skip to content

Commit e4b5e3b

Browse files
[3.10] bpo-45229: Remove test_main in many tests (GH-28405)
Instead of explicitly enumerate test classes for run_unittest() use the unittest ability to discover tests. This also makes these tests discoverable and runnable with unittest. load_tests() can be used for dynamic generating tests and adding doctests. setUpModule(), tearDownModule() and addModuleCleanup() can be used for running code before and after all module tests.. (cherry picked from commit 40348ac) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 753f7af commit e4b5e3b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+208
-486
lines changed

Lib/lib2to3/tests/data/py2_test_grammar.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# regression test, the filterwarnings() call has been added to
99
# regrtest.py.
1010

11-
from test.test_support import run_unittest, check_syntax_error
11+
from test.test_support import check_syntax_error
1212
import unittest
1313
import sys
1414
# testing import *
@@ -967,8 +967,5 @@ def _checkeval(msg, ret):
967967
self.assertEqual((6 < 4 if 0 else 2), 2)
968968

969969

970-
def test_main():
971-
run_unittest(TokenTests, GrammarTests)
972-
973970
if __name__ == '__main__':
974-
test_main()
971+
unittest.main()

Lib/lib2to3/tests/data/py3_test_grammar.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# regression test, the filterwarnings() call has been added to
99
# regrtest.py.
1010

11-
from test.support import run_unittest, check_syntax_error
11+
from test.support import check_syntax_error
1212
import unittest
1313
import sys
1414
# testing import *
@@ -952,8 +952,5 @@ def _checkeval(msg, ret):
952952
self.assertEqual((6 < 4 if 0 else 2), 2)
953953

954954

955-
def test_main():
956-
run_unittest(TokenTests, GrammarTests)
957-
958955
if __name__ == '__main__':
959-
test_main()
956+
unittest.main()

Lib/test/support/__init__.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -687,9 +687,8 @@ def check_sizeof(test, o, size):
687687
# Decorator for running a function in a different locale, correctly resetting
688688
# it afterwards.
689689

690+
@contextlib.contextmanager
690691
def run_with_locale(catstr, *locales):
691-
def decorator(func):
692-
def inner(*args, **kwds):
693692
try:
694693
import locale
695694
category = getattr(locale, catstr)
@@ -708,16 +707,11 @@ def inner(*args, **kwds):
708707
except:
709708
pass
710709

711-
# now run the function, resetting the locale on exceptions
712710
try:
713-
return func(*args, **kwds)
711+
yield
714712
finally:
715713
if locale and orig_locale:
716714
locale.setlocale(category, orig_locale)
717-
inner.__name__ = func.__name__
718-
inner.__doc__ = func.__doc__
719-
return inner
720-
return decorator
721715

722716
#=======================================================================
723717
# Decorator for running a function in a specific timezone, correctly

Lib/test/test_argparse.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
from io import StringIO
1414

15-
from test import support
1615
from test.support import os_helper
1716
from unittest import mock
1817
class StdIOBuffer(StringIO):
@@ -5397,13 +5396,11 @@ def test_exit_on_error_with_bad_args(self):
53975396
self.parser.parse_args('--integers a'.split())
53985397

53995398

5400-
def test_main():
5401-
support.run_unittest(__name__)
5399+
def tearDownModule():
54025400
# Remove global references to avoid looking like we have refleaks.
54035401
RFile.seen = {}
54045402
WFile.seen = set()
54055403

54065404

5407-
54085405
if __name__ == '__main__':
5409-
test_main()
5406+
unittest.main()

Lib/test/test_bdb.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import linecache
5858
from contextlib import contextmanager
5959
from itertools import islice, repeat
60-
import test.support
6160
from test.support import import_helper
6261
from test.support import os_helper
6362

@@ -1193,13 +1192,6 @@ def main():
11931192
with TracerRun(self) as tracer:
11941193
tracer.runcall(tfunc_import)
11951194

1196-
def test_main():
1197-
test.support.run_unittest(
1198-
StateTestCase,
1199-
RunTestCase,
1200-
BreakpointTestCase,
1201-
IssuesTestCase,
1202-
)
12031195

12041196
if __name__ == "__main__":
1205-
test_main()
1197+
unittest.main()

Lib/test/test_bigaddrspace.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ def test_repeat(self):
9292
x = None
9393

9494

95-
def test_main():
96-
support.run_unittest(BytesTest, StrTest)
97-
9895
if __name__ == '__main__':
9996
if len(sys.argv) > 1:
10097
support.set_memlimit(sys.argv[1])
101-
test_main()
98+
unittest.main()

Lib/test/test_bigmem.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,11 +1247,8 @@ def test_sort(self, size):
12471247
self.assertEqual(l[:10], [1] * 10)
12481248
self.assertEqual(l[-10:], [5] * 10)
12491249

1250-
def test_main():
1251-
support.run_unittest(StrTest, BytesTest, BytearrayTest,
1252-
TupleTest, ListTest)
12531250

12541251
if __name__ == '__main__':
12551252
if len(sys.argv) > 1:
12561253
support.set_memlimit(sys.argv[1])
1257-
test_main()
1254+
unittest.main()

Lib/test/test_bool.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Test properties of bool promised by PEP 285
22

33
import unittest
4-
from test import support
54
from test.support import os_helper
65

76
import os
@@ -370,8 +369,6 @@ def f(x):
370369
f(x)
371370
self.assertGreaterEqual(x.count, 1)
372371

373-
def test_main():
374-
support.run_unittest(BoolTest)
375372

376373
if __name__ == "__main__":
377-
test_main()
374+
unittest.main()

Lib/test/test_bz2.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,15 +1005,9 @@ def test_newline(self):
10051005
self.assertEqual(f.readlines(), [text])
10061006

10071007

1008-
def test_main():
1009-
support.run_unittest(
1010-
BZ2FileTest,
1011-
BZ2CompressorTest,
1012-
BZ2DecompressorTest,
1013-
CompressDecompressTest,
1014-
OpenTest,
1015-
)
1008+
def tearDownModule():
10161009
support.reap_children()
10171010

1011+
10181012
if __name__ == '__main__':
1019-
test_main()
1013+
unittest.main()

Lib/test/test_c_locale_coercion.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,12 +427,9 @@ def test_PYTHONCOERCECLOCALE_set_to_one(self):
427427
self.assertEqual(cmd.stdout.rstrip(), loc)
428428

429429

430-
def test_main():
431-
support.run_unittest(
432-
LocaleConfigurationTests,
433-
LocaleCoercionTests
434-
)
430+
def tearDownModule():
435431
support.reap_children()
436432

433+
437434
if __name__ == "__main__":
438-
test_main()
435+
unittest.main()

0 commit comments

Comments
 (0)