Skip to content

Commit 5e680fc

Browse files
authored
Remove run_unittest usage (#2047)
* Remove run_unittest usage * Try to fix test__warnings
1 parent c1e86ed commit 5e680fc

5 files changed

Lines changed: 96 additions & 109 deletions

File tree

src/core/IronPython/Lib/iptest/ipunittest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ def test_inputs_dir(self):
192192

193193
def run_test(name):
194194
if name == '__main__':
195-
from test import support
196-
support.run_unittest(name)
195+
unittest.main(verbosity=2)
197196

198197
def _flatten_suite(suite):
199198
tests = []

tests/suite/modules/misc/test__warnings.py

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,57 @@
66
CPython's _warnings module. http://docs.python.org/library/warnings.html
77
'''
88

9-
import unittest
109
import _warnings
11-
from iptest import IronPythonTestCase, run_test, stderr_trapper as stderr_trapper
10+
from iptest import IronPythonTestCase, stderr_trapper, run_test
1211

1312
#--GLOBALS---------------------------------------------------------------------
14-
EXPECTED = [] # expected output (ignoring filename, lineno, and line)
1513
WARN_TYPES = [Warning, UserWarning, PendingDeprecationWarning, SyntaxWarning,
1614
RuntimeWarning, FutureWarning, ImportWarning, UnicodeWarning,
1715
BytesWarning]
1816

19-
def cleanup():
20-
'''Clean up after possible incomplete test runs.'''
21-
global EXPECTED
22-
EXPECTED = []
23-
24-
25-
def expect(warn_type, message):
26-
'''Helper for test output'''
27-
for filter in _warnings.filters:
28-
if filter[0] == "ignore" and issubclass(warn_type, filter[2]):
29-
return
30-
EXPECTED.append(": " + warn_type.__name__ + ": " + message + "\n")
31-
32-
3317
class _WarningsTest(IronPythonTestCase):
3418
#TODO: @skip("multiple_execute")
3519
def test_sanity(self):
36-
global EXPECTED
37-
try:
38-
with stderr_trapper() as output:
39-
# generate test output
40-
_warnings.warn("Warning Message!")
41-
expect(UserWarning, "Warning Message!")
42-
for warn_type in WARN_TYPES:
43-
_warnings.warn(warn_type("Type-overriding message!"), UnicodeWarning)
44-
expect(warn_type, "Type-overriding message!")
45-
_warnings.warn("Another Warning Message!", warn_type)
46-
expect(warn_type, "Another Warning Message!")
47-
_warnings.warn_explicit("Explicit Warning!", warn_type, "nonexistent_file.py", 12)
48-
expect(warn_type, "Explicit Warning!")
49-
_warnings.warn_explicit("Explicit Warning!", warn_type, "test_python26.py", 34)
50-
expect(warn_type, "Explicit Warning!")
51-
_warnings.warn_explicit("Explicit Warning!", warn_type, "nonexistent_file.py", 56, "module.py")
52-
expect(warn_type, "Explicit Warning!")
53-
_warnings.warn_explicit("Explicit Warning!", warn_type, "test_python26.py", 78, "module.py")
54-
expect(warn_type, "Explicit Warning!")
55-
56-
temp_messages = output.messages
57-
58-
#No point in going further if the number of lines is not what we expect
59-
nlines = len([x for x in temp_messages if not x.startswith(" ")])
60-
self.assertEqual(nlines, len(EXPECTED))
61-
62-
# match lines
63-
for line in temp_messages:
64-
if line.startswith(" "):
65-
continue
66-
temp = EXPECTED.pop(0).rstrip()
67-
self.assertTrue(line.endswith(temp), str(line) + " does not end with " + temp)
68-
69-
finally:
70-
# remove generated files
71-
cleanup()
20+
EXPECTED = [] # expected output (ignoring filename, lineno, and line)
21+
22+
def expect(warn_type, message):
23+
'''Helper for test output'''
24+
# unittest will reset the warning filters so the base filters do nothing
25+
#for filter in _warnings.filters:
26+
# if filter[0] == "ignore" and issubclass(warn_type, filter[2]):
27+
# return
28+
EXPECTED.append(": " + warn_type.__name__ + ": " + message + "\n")
29+
30+
with stderr_trapper() as output:
31+
# generate test output
32+
_warnings.warn("Warning Message!")
33+
expect(UserWarning, "Warning Message!")
34+
for warn_type in WARN_TYPES:
35+
_warnings.warn(warn_type("Type-overriding message!"), UnicodeWarning)
36+
expect(warn_type, "Type-overriding message!")
37+
_warnings.warn("Another Warning Message!", warn_type)
38+
expect(warn_type, "Another Warning Message!")
39+
_warnings.warn_explicit("Explicit Warning!", warn_type, "nonexistent_file.py", 12)
40+
expect(warn_type, "Explicit Warning!")
41+
_warnings.warn_explicit("Explicit Warning!", warn_type, "test_python26.py", 34)
42+
expect(warn_type, "Explicit Warning!")
43+
_warnings.warn_explicit("Explicit Warning!", warn_type, "nonexistent_file.py", 56, "module.py")
44+
expect(warn_type, "Explicit Warning!")
45+
_warnings.warn_explicit("Explicit Warning!", warn_type, "test_python26.py", 78, "module.py")
46+
expect(warn_type, "Explicit Warning!")
47+
48+
temp_messages = output.messages
49+
50+
#No point in going further if the number of lines is not what we expect
51+
nlines = len([x for x in temp_messages if not x.startswith(" ")])
52+
self.assertEqual(nlines, len(EXPECTED))
53+
54+
# match lines
55+
for line in temp_messages:
56+
if line.startswith(" "):
57+
continue
58+
temp = EXPECTED.pop(0).rstrip()
59+
self.assertTrue(line.endswith(temp), str(line) + " does not end with " + temp)
7260

7361
def test_ipy2_gh23(self):
7462
def test():

tests/suite/test_cliclass.py

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
import sys
66
import unittest
7-
from iptest import IronPythonTestCase, is_cli, is_debug, is_mono, is_net70, is_net80, is_net100, is_netcoreapp, is_netcoreapp21, is_posix, is_arm64, big, run_test, skipUnlessIronPython
7+
from iptest import IronPythonTestCase, is_cli, is_debug, is_mono, is_net70, is_net80, is_net100, is_netcoreapp, is_posix, is_arm64, big, run_test, skipUnlessIronPython
8+
from iptest.ipunittest import load_ironpython_test
89

910
if is_cli:
1011
import clr
1112
import System
1213

14+
load_ironpython_test()
15+
1316
@skipUnlessIronPython()
1417
class CliClassTestCase(IronPythonTestCase):
1518

@@ -20,11 +23,6 @@ def assertNotWarns(self, warning, callable, *args, **kwds):
2023
result = callable(*args, **kwds)
2124
self.assertFalse(any(item.category == warning for item in warning_list))
2225

23-
def setUp(self):
24-
super(CliClassTestCase, self).setUp()
25-
26-
self.load_iron_python_test()
27-
2826
def test_inheritance(self):
2927
import System
3028
class MyList(System.Collections.Generic.List[int]):
@@ -1087,7 +1085,6 @@ class x(System.Version): pass
10871085
self.assertRaisesPartialMessage(TypeError, 'System.Single', f)
10881086
self.assertRaisesPartialMessage(TypeError, 'System.Version', g)
10891087

1090-
@unittest.skipIf(is_netcoreapp21, "TODO: figure out")
10911088
def test_disposable(self):
10921089
"""classes implementing IDisposable should automatically support the with statement"""
10931090
from IronPythonTest import DisposableTest
@@ -1890,44 +1887,45 @@ class MyXamlRootObject(XamlTestObject):
18901887
finally:
18911888
os.unlink(fname)
18921889

1893-
@unittest.skipIf(is_netcoreapp21, "TODO: figure out")
1894-
def test_extension_methods(self):
1895-
import clr, os
1896-
if is_netcoreapp:
1897-
clr.AddReference('System.Linq')
1898-
else:
1899-
clr.AddReference('System.Core')
1890+
def get_extension_method_test_cases():
1891+
import os
19001892

19011893
test_cases = [
19021894
"""
19031895
# add reference via type
1904-
import clr
1905-
from System.Linq import Enumerable
1896+
@skipUnlessIronPython()
19061897
class TheTestCase(IronPythonTestCase):
19071898
def test_reference_via_type(self):
1899+
import clr
1900+
from System.Linq import Enumerable
1901+
19081902
self.assertNotIn('Where', dir([]))
19091903
clr.ImportExtensions(Enumerable)
19101904
self.assertIn('Where', dir([]))
19111905
self.assertEqual(list([2,3,4].Where(lambda x: x == 2)), [2])
19121906
""",
19131907
"""
19141908
# add reference via namespace
1915-
import clr
1916-
import System
1909+
@skipUnlessIronPython()
19171910
class TheTestCase(IronPythonTestCase):
19181911
def test_reference_via_namespace(self):
1912+
import clr
1913+
import System
1914+
19191915
self.assertNotIn('Where', dir([]))
19201916
clr.ImportExtensions(System.Linq)
19211917
self.assertIn('Where', dir([]))
19221918
self.assertEqual(list([2,3,4].Where(lambda x: x == 2)), [2])
19231919
""",
19241920
"""
19251921
# add reference via namespace, add new namespace w/ more specific type
1926-
import clr
1927-
import System
1928-
from IronPythonTest.ExtensionMethodTest import LinqCollision
1922+
@skipUnlessIronPython()
19291923
class TheTestCase(IronPythonTestCase):
19301924
def test_namespace_reference(self):
1925+
import clr
1926+
import System
1927+
from IronPythonTest.ExtensionMethodTest import LinqCollision
1928+
19311929
self.assertNotIn('Where', dir([]))
19321930
clr.ImportExtensions(System.Linq)
19331931
self.assertIn('Where', dir([]))
@@ -1937,7 +1935,6 @@ def test_namespace_reference(self):
19371935
""",
19381936

19391937
"""
1940-
import clr
19411938
class UserType(object): pass
19421939
class UserTypeWithValue(object):
19431940
def __init__(self):
@@ -1949,13 +1946,15 @@ class UserTypeWithSlotsWithValue(object):
19491946
def __init__(self):
19501947
self.BaseClass = 100
19511948
1949+
@skipUnlessIronPython()
19521950
class TheTestCase(IronPythonTestCase):
19531951
def test_user_type(self):
1952+
import clr
1953+
19541954
self.assertRaises(AttributeError, lambda : UserType().BaseClass)
19551955
self.assertRaises(AttributeError, lambda : UserTypeWithSlots().BaseClass)
19561956
self.assertEqual(UserTypeWithValue().BaseClass, 200)
19571957
1958-
import clr
19591958
from IronPythonTest.ExtensionMethodTest import ClassRelationship
19601959
clr.ImportExtensions(ClassRelationship)
19611960
@@ -1978,13 +1977,14 @@ def test_user_type(self):
19781977
self.assertEqual(UserTypeWithValue().BaseClass, 200)
19791978
""",
19801979
"""
1981-
import clr
1982-
import System
1983-
from IronPythonTest.ExtensionMethodTest import ClassRelationship
1984-
clr.ImportExtensions(ClassRelationship)
1985-
1980+
@skipUnlessIronPython()
19861981
class TheTestCase(IronPythonTestCase):
19871982
def test_class_relationship(self):
1983+
import clr
1984+
import System
1985+
from IronPythonTest.ExtensionMethodTest import ClassRelationship
1986+
clr.ImportExtensions(ClassRelationship)
1987+
19881988
self.assertEqual([].Interface(), 23)
19891989
self.assertEqual([].GenericInterface(), 23)
19901990
self.assertEqual([].GenericInterfaceAndMethod(), 23)
@@ -1998,21 +1998,22 @@ def test_class_relationship(self):
19981998
self.assertEqual(object().GenericMethod(), 23)
19991999
""",
20002000
"""
2001-
import clr
2002-
import System
2003-
from System import Linq
2004-
2005-
clr.ImportExtensions(Linq)
2006-
20072001
class Product(object):
20082002
def __init__(self, cat, id, qtyOnHand ):
20092003
self.Cat = cat
20102004
self.ID = id
20112005
self.QtyOnHand = qtyOnHand
20122006
self.Q = self.QtyOnHand
20132007
2008+
@skipUnlessIronPython()
20142009
class TheTestCase(IronPythonTestCase):
20152010
def test_extension_method(self):
2011+
import clr
2012+
import System
2013+
from System import Linq
2014+
2015+
clr.ImportExtensions(Linq)
2016+
20162017
products = [Product(prod[0], prod[1], prod[2]) for prod in
20172018
(('DrillRod', 'DR123', 45), ('Flange', 'F423', 12), ('Gizmo', 'G9872', 214), ('Sprocket', 'S534', 42))]
20182019
@@ -2058,20 +2059,22 @@ def test_extension_method(self):
20582059
try:
20592060
old_path = [x for x in sys.path]
20602061
sys.path.append('.')
2061-
with open(fname, 'w+') as f:
2062+
with open(fname, 'w') as f:
20622063
f.write('''
2063-
from test import support
2064-
from iptest import IronPythonTestCase
2064+
from iptest import IronPythonTestCase, skipUnlessIronPython
20652065
''')
20662066
f.write(test_case)
2067-
f.write('''
2068-
2069-
support.run_unittest(TheTestCase)''')
20702067

20712068
__import__(temp_module)
2069+
yield sys.modules[temp_module].TheTestCase
20722070
del sys.modules[temp_module]
20732071
finally:
20742072
os.unlink(fname)
20752073
sys.path = [x for x in old_path]
20762074

2075+
def load_tests(loader, tests, pattern):
2076+
for test_case in get_extension_method_test_cases():
2077+
tests.addTests(loader.loadTestsFromTestCase(test_case))
2078+
return tests
2079+
20772080
run_test(__name__)

tests/suite/test_pow.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import test.support, unittest
1+
# Licensed to the .NET Foundation under one or more agreements.
2+
# The .NET Foundation licenses this file to you under the Apache 2.0 License.
3+
# See the LICENSE file in the project root for more information.
4+
5+
import unittest
26

37
class PowTest(unittest.TestCase):
48
# taken from test_complex.py
5-
# needed for negative real values with a fractional exponent
9+
# needed for negative real values with a fractional exponent
610
def assertAlmostEqual(self, a, b):
711
if isinstance(a, complex):
812
if isinstance(b, complex):
@@ -55,8 +59,5 @@ def test_pow_negvaluefractionalexponent(self):
5559
self.assertAlmostEqual(pow(-1, 1/3), 0.5 + 0.8660254037844386j)
5660
self.assertAlmostEqual((-1)**(1/3), 0.5 + 0.8660254037844386j)
5761

58-
def test_main():
59-
test.support.run_unittest(PowTest)
60-
6162
if __name__ == "__main__":
62-
test_main()
63+
unittest.main(verbosity=2)

tests/suite/test_unpack.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# See the LICENSE file in the project root for more information.
44

55
import sys
6-
import test.support
76
import unittest
87

98
class UnpackTest(unittest.TestCase):
@@ -22,7 +21,7 @@ def test_unpack_into_exprlist_2(self):
2221
self.assertEqual(a, [0, 1, 2, 3])
2322
self.assertEqual(b, 4)
2423
self.assertEqual(c, 5)
25-
24+
2625
def test_unpack_into_exprlist_3(self):
2726
a, *b, c = range(6)
2827
self.assertEqual(a, 0)
@@ -46,7 +45,7 @@ def test_unpack_into_exprlist_5(self):
4645
self.assertEqual(f, [1, 2, 3])
4746
self.assertEqual(g, 4)
4847
self.assertEqual(h, 5)
49-
48+
5049
def test_unpack_into_list_1(self):
5150
[*a] = range(2)
5251
self.assertEqual(a, [0, 1])
@@ -75,7 +74,7 @@ def test_unpack_into_for_target_2(self):
7574
self.assertEqual(b, index)
7675
self.assertEqual(a, [index])
7776
index = index + 1
78-
77+
7978
def test_unpack_into_for_target_3(self):
8079
index = 0
8180
expected_a = [1, 4]
@@ -236,8 +235,5 @@ def test_unpack_sequence(self):
236235
with self.assertRaises(TypeError):
237236
exec("{*1}")
238237

239-
def test_main():
240-
test.support.run_unittest(UnpackTest)
241-
242238
if __name__ == "__main__":
243-
test_main()
239+
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)