From f5064c6295f683745cbf65f1c5543bdf024900e2 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 5 Aug 2014 22:15:12 +0200 Subject: [PATCH] test: Use unittest's subTest. https://docs.python.org/3.4/library/unittest.html#distinguishing-test-iterations-using-subtests --- qutebrowser/test/config/test_conftypes.py | 77 +++++++++++++++-------- qutebrowser/test/utils/test_misc.py | 17 +++-- qutebrowser/test/utils/test_qt.py | 14 +++-- qutebrowser/test/utils/test_readline.py | 5 +- qutebrowser/test/utils/test_url.py | 16 +++-- qutebrowser/test/utils/test_webelem.py | 6 +- 6 files changed, 86 insertions(+), 49 deletions(-) diff --git a/qutebrowser/test/config/test_conftypes.py b/qutebrowser/test/config/test_conftypes.py index e8745079d..b1a479a4a 100644 --- a/qutebrowser/test/config/test_conftypes.py +++ b/qutebrowser/test/config/test_conftypes.py @@ -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.""" diff --git a/qutebrowser/test/utils/test_misc.py b/qutebrowser/test/utils/test_misc.py index 6e7e7ac2d..41483d9cb 100644 --- a/qutebrowser/test/utils/test_misc.py +++ b/qutebrowser/test/utils/test_misc.py @@ -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__': diff --git a/qutebrowser/test/utils/test_qt.py b/qutebrowser/test/utils/test_qt.py index 5ca66615a..3c1b88ee8 100644 --- a/qutebrowser/test/utils/test_qt.py +++ b/qutebrowser/test/utils/test_qt.py @@ -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): diff --git a/qutebrowser/test/utils/test_readline.py b/qutebrowser/test/utils/test_readline.py index 2666a29bf..93a1c353d 100644 --- a/qutebrowser/test/utils/test_readline.py +++ b/qutebrowser/test/utils/test_readline.py @@ -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): diff --git a/qutebrowser/test/utils/test_url.py b/qutebrowser/test/utils/test_url.py index 7e2ff6c54..860c68d16 100644 --- a/qutebrowser/test/utils/test_url.py +++ b/qutebrowser/test/utils/test_url.py @@ -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__': diff --git a/qutebrowser/test/utils/test_webelem.py b/qutebrowser/test/utils/test_webelem.py index bc44c947a..502790702 100644 --- a/qutebrowser/test/utils/test_webelem.py +++ b/qutebrowser/test/utils/test_webelem.py @@ -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):