Skip to content

[3.9] bpo-45229: Remove test_main in many tests (GH-28405) #28456

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

Merged
merged 2 commits into from
Sep 20, 2021
Merged
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
7 changes: 2 additions & 5 deletions Lib/lib2to3/tests/data/py2_test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# regression test, the filterwarnings() call has been added to
# regrtest.py.

from test.test_support import run_unittest, check_syntax_error
from test.test_support import check_syntax_error
import unittest
import sys
# testing import *
Expand Down Expand Up @@ -967,8 +967,5 @@ def _checkeval(msg, ret):
self.assertEqual((6 < 4 if 0 else 2), 2)


def test_main():
run_unittest(TokenTests, GrammarTests)

if __name__ == '__main__':
test_main()
unittest.main()
7 changes: 2 additions & 5 deletions Lib/lib2to3/tests/data/py3_test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# regression test, the filterwarnings() call has been added to
# regrtest.py.

from test.support import run_unittest, check_syntax_error
from test.support import check_syntax_error
import unittest
import sys
# testing import *
Expand Down Expand Up @@ -952,8 +952,5 @@ def _checkeval(msg, ret):
self.assertEqual((6 < 4 if 0 else 2), 2)


def test_main():
run_unittest(TokenTests, GrammarTests)

if __name__ == '__main__':
test_main()
unittest.main()
10 changes: 2 additions & 8 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1525,9 +1525,8 @@ def check_sizeof(test, o, size):
# Decorator for running a function in a different locale, correctly resetting
# it afterwards.

@contextlib.contextmanager
def run_with_locale(catstr, *locales):
def decorator(func):
def inner(*args, **kwds):
try:
import locale
category = getattr(locale, catstr)
Expand All @@ -1546,16 +1545,11 @@ def inner(*args, **kwds):
except:
pass

# now run the function, resetting the locale on exceptions
try:
return func(*args, **kwds)
yield
finally:
if locale and orig_locale:
locale.setlocale(category, orig_locale)
inner.__name__ = func.__name__
inner.__doc__ = func.__doc__
return inner
return decorator

#=======================================================================
# Decorator for running a function in a specific timezone, correctly
Expand Down
6 changes: 2 additions & 4 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5394,13 +5394,11 @@ def test_exit_on_error_with_bad_args(self):
self.parser.parse_args('--integers a'.split())


def test_main():
support.run_unittest(__name__)
def tearDownModule():
# Remove global references to avoid looking like we have refleaks.
RFile.seen = {}
WFile.seen = set()



if __name__ == '__main__':
test_main()
unittest.main()
9 changes: 1 addition & 8 deletions Lib/test/test_bdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,13 +1149,6 @@ def main():
with TracerRun(self) as tracer:
tracer.runcall(tfunc_import)

def test_main():
test.support.run_unittest(
StateTestCase,
RunTestCase,
BreakpointTestCase,
IssuesTestCase,
)

if __name__ == "__main__":
test_main()
unittest.main()
5 changes: 1 addition & 4 deletions Lib/test/test_bigaddrspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ def test_repeat(self):
x = None


def test_main():
support.run_unittest(BytesTest, StrTest)

if __name__ == '__main__':
if len(sys.argv) > 1:
support.set_memlimit(sys.argv[1])
test_main()
unittest.main()
5 changes: 1 addition & 4 deletions Lib/test/test_bigmem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,11 +1247,8 @@ def test_sort(self, size):
self.assertEqual(l[:10], [1] * 10)
self.assertEqual(l[-10:], [5] * 10)

def test_main():
support.run_unittest(StrTest, BytesTest, BytearrayTest,
TupleTest, ListTest)

if __name__ == '__main__':
if len(sys.argv) > 1:
support.set_memlimit(sys.argv[1])
test_main()
unittest.main()
4 changes: 1 addition & 3 deletions Lib/test/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,6 @@ def test_real_and_imag(self):
self.assertIs(type(False.real), int)
self.assertIs(type(False.imag), int)

def test_main():
support.run_unittest(BoolTest)

if __name__ == "__main__":
test_main()
unittest.main()
12 changes: 3 additions & 9 deletions Lib/test/test_bz2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,15 +1002,9 @@ def test_newline(self):
self.assertEqual(f.readlines(), [text])


def test_main():
support.run_unittest(
BZ2FileTest,
BZ2CompressorTest,
BZ2DecompressorTest,
CompressDecompressTest,
OpenTest,
)
def tearDownModule():
support.reap_children()


if __name__ == '__main__':
test_main()
unittest.main()
9 changes: 3 additions & 6 deletions Lib/test/test_c_locale_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,9 @@ def test_PYTHONCOERCECLOCALE_set_to_one(self):
self.assertEqual(cmd.stdout.rstrip(), loc)


def test_main():
support.run_unittest(
LocaleConfigurationTests,
LocaleCoercionTests
)
def tearDownModule():
support.reap_children()


if __name__ == "__main__":
test_main()
unittest.main()
6 changes: 3 additions & 3 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,9 +841,9 @@ def test_sys_flags_not_set(self):
)


def test_main():
support.run_unittest(CmdLineTest, IgnoreEnvironmentTest)
def tearDownModule():
support.reap_children()


if __name__ == "__main__":
test_main()
unittest.main()
6 changes: 3 additions & 3 deletions Lib/test/test_cmd_line_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,9 @@ def test_nonexisting_script(self):
self.assertNotEqual(proc.returncode, 0)


def test_main():
support.run_unittest(CmdLineTest)
def tearDownModule():
support.reap_children()


if __name__ == '__main__':
test_main()
unittest.main()
4 changes: 1 addition & 3 deletions Lib/test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,6 @@ def test_format(self):
self.assertEqual(format(complex(INF, 1), 'F'), 'INF+1.000000j')
self.assertEqual(format(complex(INF, -1), 'F'), 'INF-1.000000j')

def test_main():
support.run_unittest(ComplexTest)

if __name__ == "__main__":
test_main()
unittest.main()
12 changes: 3 additions & 9 deletions Lib/test/test_concurrent_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1500,16 +1500,10 @@ def test_multiple_set_exception(self):
self.assertEqual(f.exception(), e)


_threads_key = None

def setUpModule():
global _threads_key
_threads_key = support.threading_setup()


def tearDownModule():
support.threading_cleanup(*_threads_key)
multiprocessing.util._cleanup_tests()
unittest.addModuleCleanup(multiprocessing.util._cleanup_tests)
thread_info = support.threading_setup()
unittest.addModuleCleanup(support.threading_cleanup, *thread_info)


if __name__ == "__main__":
Expand Down
14 changes: 5 additions & 9 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4945,8 +4945,11 @@ def test_repr(self):
self.assertIn('{!r}: {!r}'.format(k, v), r)


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

Expand Down Expand Up @@ -5684,12 +5687,5 @@ class A(metaclass=M):
pass


def test_main():
# Run all local test cases, with PTypesLongInitTest first.
support.run_unittest(PTypesLongInitTest, OperatorsTest,
ClassPropertiesAndMethods, DictProxyTests,
MiscTests, PicklingTests, SharedKeyTests,
MroTest)

if __name__ == "__main__":
test_main()
unittest.main()
7 changes: 2 additions & 5 deletions Lib/test/test_devpoll.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import random
import select
import unittest
from test.support import run_unittest, cpython_only
from test.support import cpython_only

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


def test_main():
run_unittest(DevPollTests)

if __name__ == '__main__':
test_main()
unittest.main()
16 changes: 9 additions & 7 deletions Lib/test/test_difflib.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import difflib
from test.support import run_unittest, findfile
from test.support import findfile
import unittest
import doctest
import sys
Expand Down Expand Up @@ -547,12 +547,14 @@ def test_longest_match_with_popular_chars(self):
self.assertFalse(self.longer_match_exists(a, b, match.size))


def test_main():
def setUpModule():
difflib.HtmlDiff._default_prefix = 0
Doctests = doctest.DocTestSuite(difflib)
run_unittest(
TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs,
TestOutputFormat, TestBytes, TestJunkAPIs, TestFindLongest, Doctests)


def load_tests(loader, tests, pattern):
tests.addTest(doctest.DocTestSuite(difflib))
return tests


if __name__ == '__main__':
test_main()
unittest.main()
12 changes: 5 additions & 7 deletions Lib/test/test_distutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
import test.support


def test_main():
# used by regrtest
test.support.run_unittest(distutils.tests.test_suite())
test.support.reap_children()


def load_tests(*_):
# used by unittest
return distutils.tests.test_suite()


def tearDownModule():
test.support.reap_children()


if __name__ == "__main__":
test_main()
unittest.main()
17 changes: 6 additions & 11 deletions Lib/test/test_dtrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import types
import unittest

from test.support import findfile, run_unittest
from test.support import findfile


def abspath(filename):
Expand Down Expand Up @@ -97,7 +97,7 @@ class SystemTapBackend(TraceBackend):
COMMAND = ["stap", "-g"]


class TraceTests(unittest.TestCase):
class TraceTests:
# unittest.TestCase options
maxDiff = None

Expand Down Expand Up @@ -149,30 +149,25 @@ def test_line(self):
self.run_case("line")


class DTraceNormalTests(TraceTests):
class DTraceNormalTests(TraceTests, unittest.TestCase):
backend = DTraceBackend()
optimize_python = 0


class DTraceOptimizedTests(TraceTests):
class DTraceOptimizedTests(TraceTests, unittest.TestCase):
backend = DTraceBackend()
optimize_python = 2


class SystemTapNormalTests(TraceTests):
class SystemTapNormalTests(TraceTests, unittest.TestCase):
backend = SystemTapBackend()
optimize_python = 0


class SystemTapOptimizedTests(TraceTests):
class SystemTapOptimizedTests(TraceTests, unittest.TestCase):
backend = SystemTapBackend()
optimize_python = 2


def test_main():
run_unittest(DTraceNormalTests, DTraceOptimizedTests, SystemTapNormalTests,
SystemTapOptimizedTests)


if __name__ == '__main__':
test_main()
6 changes: 2 additions & 4 deletions Lib/test/test_fcntl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
import unittest
from multiprocessing import Process
from test.support import (verbose, TESTFN, unlink, run_unittest, import_module,
from test.support import (verbose, TESTFN, unlink, import_module,
cpython_only)

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

def test_main():
run_unittest(TestFcntl)

if __name__ == '__main__':
test_main()
unittest.main()
Loading