Skip to content

Commit 5822ab6

Browse files
[3.9] bpo-45229: Remove test_main in many tests (GH-28405) (GH-28456)
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)
1 parent 41e2a31 commit 5822ab6

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

+211
-472
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
@@ -1525,9 +1525,8 @@ def check_sizeof(test, o, size):
15251525
# Decorator for running a function in a different locale, correctly resetting
15261526
# it afterwards.
15271527

1528+
@contextlib.contextmanager
15281529
def run_with_locale(catstr, *locales):
1529-
def decorator(func):
1530-
def inner(*args, **kwds):
15311530
try:
15321531
import locale
15331532
category = getattr(locale, catstr)
@@ -1546,16 +1545,11 @@ def inner(*args, **kwds):
15461545
except:
15471546
pass
15481547

1549-
# now run the function, resetting the locale on exceptions
15501548
try:
1551-
return func(*args, **kwds)
1549+
yield
15521550
finally:
15531551
if locale and orig_locale:
15541552
locale.setlocale(category, orig_locale)
1555-
inner.__name__ = func.__name__
1556-
inner.__doc__ = func.__doc__
1557-
return inner
1558-
return decorator
15591553

15601554
#=======================================================================
15611555
# Decorator for running a function in a specific timezone, correctly

Lib/test/test_argparse.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5394,13 +5394,11 @@ def test_exit_on_error_with_bad_args(self):
53945394
self.parser.parse_args('--integers a'.split())
53955395

53965396

5397-
def test_main():
5398-
support.run_unittest(__name__)
5397+
def tearDownModule():
53995398
# Remove global references to avoid looking like we have refleaks.
54005399
RFile.seen = {}
54015400
WFile.seen = set()
54025401

54035402

5404-
54055403
if __name__ == '__main__':
5406-
test_main()
5404+
unittest.main()

Lib/test/test_bdb.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,13 +1149,6 @@ def main():
11491149
with TracerRun(self) as tracer:
11501150
tracer.runcall(tfunc_import)
11511151

1152-
def test_main():
1153-
test.support.run_unittest(
1154-
StateTestCase,
1155-
RunTestCase,
1156-
BreakpointTestCase,
1157-
IssuesTestCase,
1158-
)
11591152

11601153
if __name__ == "__main__":
1161-
test_main()
1154+
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,6 @@ def test_real_and_imag(self):
362362
self.assertIs(type(False.real), int)
363363
self.assertIs(type(False.imag), int)
364364

365-
def test_main():
366-
support.run_unittest(BoolTest)
367365

368366
if __name__ == "__main__":
369-
test_main()
367+
unittest.main()

Lib/test/test_bz2.py

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

10041004

1005-
def test_main():
1006-
support.run_unittest(
1007-
BZ2FileTest,
1008-
BZ2CompressorTest,
1009-
BZ2DecompressorTest,
1010-
CompressDecompressTest,
1011-
OpenTest,
1012-
)
1005+
def tearDownModule():
10131006
support.reap_children()
10141007

1008+
10151009
if __name__ == '__main__':
1016-
test_main()
1010+
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()

Lib/test/test_cmd_line.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -841,9 +841,9 @@ def test_sys_flags_not_set(self):
841841
)
842842

843843

844-
def test_main():
845-
support.run_unittest(CmdLineTest, IgnoreEnvironmentTest)
844+
def tearDownModule():
846845
support.reap_children()
847846

847+
848848
if __name__ == "__main__":
849-
test_main()
849+
unittest.main()

Lib/test/test_cmd_line_script.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -737,9 +737,9 @@ def test_nonexisting_script(self):
737737
self.assertNotEqual(proc.returncode, 0)
738738

739739

740-
def test_main():
741-
support.run_unittest(CmdLineTest)
740+
def tearDownModule():
742741
support.reap_children()
743742

743+
744744
if __name__ == '__main__':
745-
test_main()
745+
unittest.main()

Lib/test/test_complex.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,8 +751,6 @@ def test_format(self):
751751
self.assertEqual(format(complex(INF, 1), 'F'), 'INF+1.000000j')
752752
self.assertEqual(format(complex(INF, -1), 'F'), 'INF-1.000000j')
753753

754-
def test_main():
755-
support.run_unittest(ComplexTest)
756754

757755
if __name__ == "__main__":
758-
test_main()
756+
unittest.main()

Lib/test/test_concurrent_futures.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,16 +1500,10 @@ def test_multiple_set_exception(self):
15001500
self.assertEqual(f.exception(), e)
15011501

15021502

1503-
_threads_key = None
1504-
15051503
def setUpModule():
1506-
global _threads_key
1507-
_threads_key = support.threading_setup()
1508-
1509-
1510-
def tearDownModule():
1511-
support.threading_cleanup(*_threads_key)
1512-
multiprocessing.util._cleanup_tests()
1504+
unittest.addModuleCleanup(multiprocessing.util._cleanup_tests)
1505+
thread_info = support.threading_setup()
1506+
unittest.addModuleCleanup(support.threading_cleanup, *thread_info)
15131507

15141508

15151509
if __name__ == "__main__":

Lib/test/test_descr.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4945,8 +4945,11 @@ def test_repr(self):
49454945
self.assertIn('{!r}: {!r}'.format(k, v), r)
49464946

49474947

4948-
class PTypesLongInitTest(unittest.TestCase):
4948+
class AAAPTypesLongInitTest(unittest.TestCase):
49494949
# This is in its own TestCase so that it can be run before any other tests.
4950+
# (Hence the 'AAA' in the test class name: to make it the first
4951+
# item in a list sorted by name, like
4952+
# unittest.TestLoader.getTestCaseNames() does.)
49504953
def test_pytype_long_ready(self):
49514954
# Testing SF bug 551412 ...
49524955

@@ -5684,12 +5687,5 @@ class A(metaclass=M):
56845687
pass
56855688

56865689

5687-
def test_main():
5688-
# Run all local test cases, with PTypesLongInitTest first.
5689-
support.run_unittest(PTypesLongInitTest, OperatorsTest,
5690-
ClassPropertiesAndMethods, DictProxyTests,
5691-
MiscTests, PicklingTests, SharedKeyTests,
5692-
MroTest)
5693-
56945690
if __name__ == "__main__":
5695-
test_main()
5691+
unittest.main()

Lib/test/test_devpoll.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import random
77
import select
88
import unittest
9-
from test.support import run_unittest, cpython_only
9+
from test.support import cpython_only
1010

1111
if not hasattr(select, 'devpoll') :
1212
raise unittest.SkipTest('test works only on Solaris OS family')
@@ -138,8 +138,5 @@ def test_events_mask_overflow_c_limits(self):
138138
self.assertRaises(OverflowError, pollster.modify, 1, USHRT_MAX + 1)
139139

140140

141-
def test_main():
142-
run_unittest(DevPollTests)
143-
144141
if __name__ == '__main__':
145-
test_main()
142+
unittest.main()

Lib/test/test_difflib.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import difflib
2-
from test.support import run_unittest, findfile
2+
from test.support import findfile
33
import unittest
44
import doctest
55
import sys
@@ -547,12 +547,14 @@ def test_longest_match_with_popular_chars(self):
547547
self.assertFalse(self.longer_match_exists(a, b, match.size))
548548

549549

550-
def test_main():
550+
def setUpModule():
551551
difflib.HtmlDiff._default_prefix = 0
552-
Doctests = doctest.DocTestSuite(difflib)
553-
run_unittest(
554-
TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs,
555-
TestOutputFormat, TestBytes, TestJunkAPIs, TestFindLongest, Doctests)
552+
553+
554+
def load_tests(loader, tests, pattern):
555+
tests.addTest(doctest.DocTestSuite(difflib))
556+
return tests
557+
556558

557559
if __name__ == '__main__':
558-
test_main()
560+
unittest.main()

Lib/test/test_distutils.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@
99
import test.support
1010

1111

12-
def test_main():
13-
# used by regrtest
14-
test.support.run_unittest(distutils.tests.test_suite())
15-
test.support.reap_children()
16-
17-
1812
def load_tests(*_):
1913
# used by unittest
2014
return distutils.tests.test_suite()
2115

2216

17+
def tearDownModule():
18+
test.support.reap_children()
19+
20+
2321
if __name__ == "__main__":
24-
test_main()
22+
unittest.main()

Lib/test/test_dtrace.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import types
77
import unittest
88

9-
from test.support import findfile, run_unittest
9+
from test.support import findfile
1010

1111

1212
def abspath(filename):
@@ -97,7 +97,7 @@ class SystemTapBackend(TraceBackend):
9797
COMMAND = ["stap", "-g"]
9898

9999

100-
class TraceTests(unittest.TestCase):
100+
class TraceTests:
101101
# unittest.TestCase options
102102
maxDiff = None
103103

@@ -149,30 +149,25 @@ def test_line(self):
149149
self.run_case("line")
150150

151151

152-
class DTraceNormalTests(TraceTests):
152+
class DTraceNormalTests(TraceTests, unittest.TestCase):
153153
backend = DTraceBackend()
154154
optimize_python = 0
155155

156156

157-
class DTraceOptimizedTests(TraceTests):
157+
class DTraceOptimizedTests(TraceTests, unittest.TestCase):
158158
backend = DTraceBackend()
159159
optimize_python = 2
160160

161161

162-
class SystemTapNormalTests(TraceTests):
162+
class SystemTapNormalTests(TraceTests, unittest.TestCase):
163163
backend = SystemTapBackend()
164164
optimize_python = 0
165165

166166

167-
class SystemTapOptimizedTests(TraceTests):
167+
class SystemTapOptimizedTests(TraceTests, unittest.TestCase):
168168
backend = SystemTapBackend()
169169
optimize_python = 2
170170

171171

172-
def test_main():
173-
run_unittest(DTraceNormalTests, DTraceOptimizedTests, SystemTapNormalTests,
174-
SystemTapOptimizedTests)
175-
176-
177172
if __name__ == '__main__':
178173
test_main()

Lib/test/test_fcntl.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import sys
77
import unittest
88
from multiprocessing import Process
9-
from test.support import (verbose, TESTFN, unlink, run_unittest, import_module,
9+
from test.support import (verbose, TESTFN, unlink, import_module,
1010
cpython_only)
1111

1212
# Skip test if no fcntl module.
@@ -188,8 +188,6 @@ def test_fcntl_f_getpath(self):
188188
res = fcntl.fcntl(self.f.fileno(), fcntl.F_GETPATH, bytes(len(expected)))
189189
self.assertEqual(expected, res)
190190

191-
def test_main():
192-
run_unittest(TestFcntl)
193191

194192
if __name__ == '__main__':
195-
test_main()
193+
unittest.main()

0 commit comments

Comments
 (0)