Skip to content

Commit

Permalink
bpo-45229: Remove test_main in many tests (GH-28405)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
serhiy-storchaka authored Sep 19, 2021
1 parent 664448d commit 40348ac
Show file tree
Hide file tree
Showing 61 changed files with 209 additions and 485 deletions.
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 @@ -695,9 +695,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 @@ -716,16 +715,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
7 changes: 2 additions & 5 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

from io import StringIO

from test import support
from test.support import os_helper
from unittest import mock
class StdIOBuffer(StringIO):
Expand Down Expand Up @@ -5416,13 +5415,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()
10 changes: 1 addition & 9 deletions Lib/test/test_bdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import linecache
from contextlib import contextmanager
from itertools import islice, repeat
import test.support
from test.support import import_helper
from test.support import os_helper

Expand Down Expand Up @@ -1193,13 +1192,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()
5 changes: 1 addition & 4 deletions Lib/test/test_bool.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Test properties of bool promised by PEP 285

import unittest
from test import support
from test.support import os_helper

import os
Expand Down Expand Up @@ -370,8 +369,6 @@ def f(x):
f(x)
self.assertGreaterEqual(x.count, 1)

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 @@ -1005,15 +1005,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()
7 changes: 4 additions & 3 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,9 +864,10 @@ def test_tokenizer_error_with_stdin(self):
def test_decoding_error_at_the_end_of_the_line(self):
self.check_string(br"'\u1f'")

def test_main():
support.run_unittest(CmdLineTest, IgnoreEnvironmentTest, SyntaxErrorTests)

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 @@ -738,9 +738,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 @@ -789,8 +789,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 @@ -1515,16 +1515,10 @@ def test_multiple_set_exception(self):
self.assertEqual(f.exception(), e)


_threads_key = None

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


def tearDownModule():
threading_helper.threading_cleanup(*_threads_key)
multiprocessing.util._cleanup_tests()
unittest.addModuleCleanup(multiprocessing.util._cleanup_tests)
thread_info = threading_helper.threading_setup()
unittest.addModuleCleanup(threading_helper.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 @@ -4996,8 +4996,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 @@ -5735,12 +5738,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 @@ -15,16 +15,14 @@
import distutils.tests


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


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


def tearDownModule():
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()
Loading

0 comments on commit 40348ac

Please sign in to comment.