test: Use unittest's subTest.
https://docs.python.org/3.4/library/unittest.html#distinguishing-test-iterations-using-subtests
This commit is contained in:
parent
4d051bea73
commit
f5064c6295
@ -305,7 +305,8 @@ class BoolTests(unittest.TestCase):
|
||||
"""Test transform with all values."""
|
||||
for out, inputs in self.TESTS.items():
|
||||
for inp in inputs:
|
||||
self.assertEqual(self.t.transform(inp), out, inp)
|
||||
with self.subTest(inp=inp, out=out):
|
||||
self.assertEqual(self.t.transform(inp), out, inp)
|
||||
|
||||
def test_transform_empty(self):
|
||||
"""Test transform with none_ok = False and an empty value."""
|
||||
@ -315,13 +316,15 @@ class BoolTests(unittest.TestCase):
|
||||
"""Test validate with valid values."""
|
||||
for vallist in self.TESTS.values():
|
||||
for val in vallist:
|
||||
self.t.validate(val)
|
||||
with self.subTest(val=val):
|
||||
self.t.validate(val)
|
||||
|
||||
def test_validate_invalid(self):
|
||||
"""Test validate with invalid values."""
|
||||
for val in self.INVALID:
|
||||
with self.assertRaises(conftypes.ValidationError):
|
||||
self.t.validate(val)
|
||||
with self.subTest(val=val):
|
||||
with self.assertRaises(conftypes.ValidationError):
|
||||
self.t.validate(val)
|
||||
|
||||
def test_validate_empty(self):
|
||||
"""Test validate with empty string and none_ok = False."""
|
||||
@ -931,18 +934,21 @@ class ColorSystemTests(unittest.TestCase):
|
||||
def test_validate_valid(self):
|
||||
"""Test validate with valid values."""
|
||||
for val in self.TESTS:
|
||||
self.t.validate(val)
|
||||
with self.subTest(val=val):
|
||||
self.t.validate(val)
|
||||
|
||||
def test_validate_invalid(self):
|
||||
"""Test validate with invalid values."""
|
||||
for val in self.INVALID:
|
||||
with self.assertRaises(conftypes.ValidationError, msg=val):
|
||||
self.t.validate(val)
|
||||
with self.subTest(val=val):
|
||||
with self.assertRaises(conftypes.ValidationError, msg=val):
|
||||
self.t.validate(val)
|
||||
|
||||
def test_transform(self):
|
||||
"""Test transform."""
|
||||
for k, v in self.TESTS.items():
|
||||
self.assertEqual(self.t.transform(k), v, k)
|
||||
with self.subTest(v=v):
|
||||
self.assertEqual(self.t.transform(k), v, k)
|
||||
|
||||
def test_transform_empty(self):
|
||||
"""Test transform with an empty value."""
|
||||
@ -973,18 +979,21 @@ class QtColorTests(unittest.TestCase):
|
||||
def test_validate_valid(self):
|
||||
"""Test validate with valid values."""
|
||||
for v in self.VALID:
|
||||
self.t.validate(v)
|
||||
with self.subTest(v=v):
|
||||
self.t.validate(v)
|
||||
|
||||
def test_validate_invalid(self):
|
||||
"""Test validate with invalid values."""
|
||||
for val in self.INVALID + self.INVALID_QT:
|
||||
with self.assertRaises(conftypes.ValidationError, msg=val):
|
||||
self.t.validate(val)
|
||||
with self.subTest(val=val):
|
||||
with self.assertRaises(conftypes.ValidationError, msg=val):
|
||||
self.t.validate(val)
|
||||
|
||||
def test_transform(self):
|
||||
"""Test transform."""
|
||||
for v in self.VALID:
|
||||
self.assertEqual(self.t.transform(v), QColor(v), v)
|
||||
with self.subTest(v=v):
|
||||
self.assertEqual(self.t.transform(v), QColor(v), v)
|
||||
|
||||
def test_transform_empty(self):
|
||||
"""Test transform with an empty value."""
|
||||
@ -1008,7 +1017,8 @@ class CssColorTests(QtColorTests):
|
||||
def test_transform(self):
|
||||
"""Make sure transform doesn't alter the value."""
|
||||
for v in self.VALID:
|
||||
self.assertEqual(self.t.transform(v), v, v)
|
||||
with self.subTest(v=v):
|
||||
self.assertEqual(self.t.transform(v), v, v)
|
||||
|
||||
|
||||
class QssColorTests(QtColorTests):
|
||||
@ -1039,7 +1049,8 @@ class QssColorTests(QtColorTests):
|
||||
def test_transform(self):
|
||||
"""Make sure transform doesn't alter the value."""
|
||||
for v in self.VALID:
|
||||
self.assertEqual(self.t.transform(v), v, v)
|
||||
with self.subTest(v=v):
|
||||
self.assertEqual(self.t.transform(v), v, v)
|
||||
|
||||
|
||||
class FontTests(unittest.TestCase):
|
||||
@ -1104,27 +1115,36 @@ class FontTests(unittest.TestCase):
|
||||
def test_validate_valid(self):
|
||||
"""Test validate with valid values."""
|
||||
for val in self.TESTS:
|
||||
self.t.validate(val)
|
||||
self.t2.validate(val)
|
||||
with self.subTest(val=val):
|
||||
with self.subTest(t="t1"):
|
||||
self.t.validate(val)
|
||||
with self.subTest(t="t2"):
|
||||
self.t2.validate(val)
|
||||
|
||||
# FIXME
|
||||
@unittest.expectedFailure
|
||||
def test_validate_invalid(self):
|
||||
"""Test validate with invalid values."""
|
||||
for val in self.INVALID:
|
||||
with self.assertRaises(conftypes.ValidationError, msg=val):
|
||||
self.t.validate(val)
|
||||
with self.assertRaises(conftypes.ValidationError, msg=val):
|
||||
self.t2.validate(val)
|
||||
with self.subTest(val=val):
|
||||
with self.subTest(t="t1"):
|
||||
with self.assertRaises(conftypes.ValidationError, msg=val):
|
||||
self.t.validate(val)
|
||||
with self.subTest(t="t2"):
|
||||
with self.assertRaises(conftypes.ValidationError, msg=val):
|
||||
self.t2.validate(val)
|
||||
|
||||
# FIXME
|
||||
@unittest.expectedFailure
|
||||
def test_transform(self):
|
||||
"""Test transform."""
|
||||
for string, desc in self.TESTS.items():
|
||||
self.assertEqual(self.t.transform(string), string, string)
|
||||
self.assertEqual(Font(self.t2.transform(string)),
|
||||
Font.fromdesc(desc), string)
|
||||
with self.subTest(val=val):
|
||||
with self.subTest(t="t1"):
|
||||
self.assertEqual(self.t.transform(string), string, string)
|
||||
with self.subTest(t="t2"):
|
||||
self.assertEqual(Font(self.t2.transform(string)),
|
||||
Font.fromdesc(desc), string)
|
||||
|
||||
def test_transform_empty(self):
|
||||
"""Test transform with an empty value."""
|
||||
@ -1767,19 +1787,22 @@ class AutoSearchTests(unittest.TestCase):
|
||||
"""Test validate with valid values."""
|
||||
for vallist in self.TESTS.values():
|
||||
for val in vallist:
|
||||
self.t.validate(val)
|
||||
with self.subTest(val=val):
|
||||
self.t.validate(val)
|
||||
|
||||
def test_validate_invalid(self):
|
||||
"""Test validate with invalid values."""
|
||||
for val in self.INVALID:
|
||||
with self.assertRaises(conftypes.ValidationError):
|
||||
self.t.validate(val)
|
||||
with self.subTest(val=val):
|
||||
with self.assertRaises(conftypes.ValidationError):
|
||||
self.t.validate(val)
|
||||
|
||||
def test_transform(self):
|
||||
"""Test transform with all values."""
|
||||
for out, inputs in self.TESTS.items():
|
||||
for inp in inputs:
|
||||
self.assertEqual(self.t.transform(inp), out, inp)
|
||||
with self.subTest(inp=inp):
|
||||
self.assertEqual(self.t.transform(inp), out, inp)
|
||||
|
||||
def test_transform_empty(self):
|
||||
"""Test transform with none_ok = False and an empty value."""
|
||||
|
@ -384,7 +384,8 @@ class FormatSecondsTests(unittest.TestCase):
|
||||
def test_format_seconds(self):
|
||||
"""Test format_seconds with several tests."""
|
||||
for seconds, out in self.TESTS:
|
||||
self.assertEqual(utils.format_seconds(seconds), out, seconds)
|
||||
with self.subTest(seconds=seconds):
|
||||
self.assertEqual(utils.format_seconds(seconds), out)
|
||||
|
||||
|
||||
class FormatSizeTests(unittest.TestCase):
|
||||
@ -410,19 +411,22 @@ class FormatSizeTests(unittest.TestCase):
|
||||
def test_format_size(self):
|
||||
"""Test format_size with several tests."""
|
||||
for size, out in self.TESTS:
|
||||
self.assertEqual(utils.format_size(size), out, size)
|
||||
with self.subTest(size=size):
|
||||
self.assertEqual(utils.format_size(size), out)
|
||||
|
||||
def test_suffix(self):
|
||||
"""Test the suffix option."""
|
||||
for size, out in self.TESTS:
|
||||
self.assertEqual(utils.format_size(size, suffix='B'), out + 'B',
|
||||
size)
|
||||
with self.subTest(size=size):
|
||||
self.assertEqual(utils.format_size(size, suffix='B'),
|
||||
out + 'B')
|
||||
|
||||
def test_base(self):
|
||||
"""Test with an alternative base."""
|
||||
kilo_tests = [(999, '999.00'), (1000, '1.00k'), (1010, '1.01k')]
|
||||
for size, out in kilo_tests:
|
||||
self.assertEqual(utils.format_size(size, base=1000), out, size)
|
||||
with self.subTest(size=size):
|
||||
self.assertEqual(utils.format_size(size, base=1000), out)
|
||||
|
||||
|
||||
class KeyToStringTests(unittest.TestCase):
|
||||
@ -502,7 +506,8 @@ class NormalizeTests(unittest.TestCase):
|
||||
('Windows++', 'meta++'),
|
||||
)
|
||||
for orig, repl in strings:
|
||||
self.assertEqual(utils.normalize_keystr(orig), repl, orig)
|
||||
with self.subTest(orig=orig):
|
||||
self.assertEqual(utils.normalize_keystr(orig), repl)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -62,22 +62,24 @@ class CheckOverflowTests(unittest.TestCase):
|
||||
"""Test values which are inside bounds."""
|
||||
for ctype, vals in self.GOOD_VALUES.items():
|
||||
for val in vals:
|
||||
qt.check_overflow(val, ctype)
|
||||
with self.subTest(ctype=ctype, val=val):
|
||||
qt.check_overflow(val, ctype)
|
||||
|
||||
def test_bad_values_fatal(self):
|
||||
"""Test values which are outside bounds with fatal=True."""
|
||||
for ctype, vals in self.BAD_VALUES.items():
|
||||
for (val, _) in vals:
|
||||
with self.assertRaises(OverflowError, msg=ctype):
|
||||
qt.check_overflow(val, ctype)
|
||||
with self.subTest(ctype=ctype, val=val):
|
||||
with self.assertRaises(OverflowError):
|
||||
qt.check_overflow(val, ctype)
|
||||
|
||||
def test_bad_values_nonfatal(self):
|
||||
"""Test values which are outside bounds with fatal=False."""
|
||||
for ctype, vals in self.BAD_VALUES.items():
|
||||
for (val, replacement) in vals:
|
||||
newval = qt.check_overflow(val, ctype, fatal=False)
|
||||
self.assertEqual(newval, replacement,
|
||||
"{}: {}".format(ctype, val))
|
||||
with self.subTest(ctype=ctype, val=val):
|
||||
newval = qt.check_overflow(val, ctype, fatal=False)
|
||||
self.assertEqual(newval, replacement)
|
||||
|
||||
|
||||
class GetQtArgsTests(unittest.TestCase):
|
||||
|
@ -40,8 +40,9 @@ class NoneWidgetTests(unittest.TestCase):
|
||||
def test_none(self):
|
||||
"""Test if there are no exceptions when the widget is None."""
|
||||
for name, method in inspect.getmembers(self.bridge, inspect.ismethod):
|
||||
if name.startswith('rl_'):
|
||||
method()
|
||||
with self.subTest(name=name):
|
||||
if name.startswith('rl_'):
|
||||
method()
|
||||
|
||||
|
||||
class ReadlineBridgeTest(unittest.TestCase):
|
||||
|
@ -61,14 +61,16 @@ class SpecialURLTests(unittest.TestCase):
|
||||
def test_special_urls(self):
|
||||
"""Test special URLs."""
|
||||
for url in self.SPECIAL_URLS:
|
||||
u = QUrl(url)
|
||||
self.assertTrue(urlutils.is_special_url(u))
|
||||
with self.subTest(url=url):
|
||||
u = QUrl(url)
|
||||
self.assertTrue(urlutils.is_special_url(u))
|
||||
|
||||
def test_normal_urls(self):
|
||||
"""Test non-special URLs."""
|
||||
for url in self.NORMAL_URLS:
|
||||
u = QUrl(url)
|
||||
self.assertFalse(urlutils.is_special_url(u))
|
||||
with self.subTest(url=url):
|
||||
u = QUrl(url)
|
||||
self.assertFalse(urlutils.is_special_url(u))
|
||||
|
||||
|
||||
class SearchUrlTests(unittest.TestCase):
|
||||
@ -135,12 +137,14 @@ class IsUrlNaiveTests(unittest.TestCase):
|
||||
def test_urls(self):
|
||||
"""Test things which are URLs."""
|
||||
for url in self.URLS:
|
||||
self.assertTrue(urlutils._is_url_naive(url), url)
|
||||
with self.subTest(url=url):
|
||||
self.assertTrue(urlutils._is_url_naive(url), url)
|
||||
|
||||
def test_not_urls(self):
|
||||
"""Test things which are not URLs."""
|
||||
for url in self.NOT_URLS:
|
||||
self.assertFalse(urlutils._is_url_naive(url), url)
|
||||
with self.subTest(url=url):
|
||||
self.assertFalse(urlutils._is_url_naive(url), url)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -242,7 +242,8 @@ class JavascriptEscapeTests(unittest.TestCase):
|
||||
def test_fake_escape(self):
|
||||
"""Test javascript escaping."""
|
||||
for before, after in self.STRINGS:
|
||||
self.assertEqual(webelem.javascript_escape(before), after)
|
||||
with self.subTest(before=before):
|
||||
self.assertEqual(webelem.javascript_escape(before), after)
|
||||
|
||||
|
||||
class GetChildFramesTests(unittest.TestCase):
|
||||
@ -293,7 +294,8 @@ class GetChildFramesTests(unittest.TestCase):
|
||||
self.assertEqual(len(children), 7)
|
||||
self.assertIs(children[0], root)
|
||||
for frame in [root] + first + second:
|
||||
frame.childFrames.assert_called_once_with()
|
||||
with self.subTest(frame=frame):
|
||||
frame.childFrames.assert_called_once_with()
|
||||
|
||||
|
||||
class IsEditableTests(unittest.TestCase):
|
||||
|
Loading…
Reference in New Issue
Block a user