aboutsummaryrefslogtreecommitdiff
path: root/absl/testing/tests
diff options
context:
space:
mode:
authorRichard Levasseur <rlevasseur@google.com>2020-05-12 10:58:49 -0700
committerCopybara-Service <copybara-worker@google.com>2020-05-12 10:59:08 -0700
commit1d4e09ad39bd0163d1413769bda1fea7c621fe48 (patch)
tree0338ed75606d91f74b86b5013d531037b5b834aa /absl/testing/tests
parent97ab612537feab6b56f345b0e1025d35712f8926 (diff)
downloadabsl-py-1d4e09ad39bd0163d1413769bda1fea7c621fe48.tar.gz
Make parameterized tests show runtime test name that is copy/paste friendly.
This solves two problems: 1. Makes the printed name copy/paste friendly (e.g. "FooTest.test_foo" instead of "test_foo (__main__.FooTest)" 2. Prints the addressable test name (e.g. "test_foo1") instead of the original test name ("test_foo"). This makes it possible to copy/paste the name and run that single test (which is otherwise near impossible, since the suffixed name is never mentioned anywhere). The repr of parameterized values is still appended to aid knowing which case the test is covering. Also more strictly enforces that generated tests are coming from the parameterized decorators. PiperOrigin-RevId: 311160728 Change-Id: Ica8afd0998362cd8a8a1d1abdca02bc8a51c38d6
Diffstat (limited to 'absl/testing/tests')
-rwxr-xr-xabsl/testing/tests/parameterized_test.py134
-rwxr-xr-xabsl/testing/tests/xml_reporter_test.py5
2 files changed, 50 insertions, 89 deletions
diff --git a/absl/testing/tests/parameterized_test.py b/absl/testing/tests/parameterized_test.py
index 85f8e9b..5f2dcc0 100755
--- a/absl/testing/tests/parameterized_test.py
+++ b/absl/testing/tests/parameterized_test.py
@@ -336,23 +336,10 @@ class ParameterizedTestsTest(absltest.TestCase):
class UniqueDescriptiveNamesTest(parameterized.TestCase):
- class JustBeingMean(object):
-
- def __repr__(self):
- return '13) (2'
-
@parameterized.parameters(13, 13)
def test_normal(self, number):
del number
- @parameterized.parameters(13, 13, JustBeingMean())
- def test_double_conflict(self, number):
- del number
-
- @parameterized.parameters(13, JustBeingMean(), 13, 13)
- def test_triple_conflict(self, number):
- del number
-
class MultiGeneratorsTestCase(parameterized.TestCase):
@parameterized.parameters((i for i in (1, 2, 3)), (i for i in (3, 2, 1)))
@@ -442,11 +429,13 @@ class ParameterizedTestsTest(absltest.TestCase):
def test_short_description(self):
ts = unittest.makeSuite(self.GoodAdditionParams)
- short_desc = list(ts)[0].shortDescription().split('\n')
- self.assertEqual(
- 'test_addition(1, 2, 3)', short_desc[1])
- self.assertTrue(
- short_desc[0].startswith('test_addition(1, 2, 3)'))
+ short_desc = list(ts)[0].shortDescription()
+
+ location = unittest.util.strclass(self.GoodAdditionParams).replace(
+ '__main__.', '')
+ expected = ('{}.test_addition0 (1, 2, 3)\n'.format(location) +
+ 'test_addition(1, 2, 3)')
+ self.assertEqual(expected, short_desc)
def test_short_description_addresses_removed(self):
ts = unittest.makeSuite(self.ArgumentsWithAddresses)
@@ -461,14 +450,22 @@ class ParameterizedTestsTest(absltest.TestCase):
ts = unittest.makeSuite(self.ArgumentsWithAddresses)
self.assertEqual(
(unittest.util.strclass(self.ArgumentsWithAddresses) +
- '.test_something(<object>)'),
+ '.test_something0 (<object>)'),
list(ts)[0].id())
ts = unittest.makeSuite(self.GoodAdditionParams)
self.assertEqual(
(unittest.util.strclass(self.GoodAdditionParams) +
- '.test_addition(1, 2, 3)'),
+ '.test_addition0 (1, 2, 3)'),
list(ts)[0].id())
+ def test_str(self):
+ ts = unittest.makeSuite(self.GoodAdditionParams)
+ test = list(ts)[0]
+
+ expected = 'test_addition0 (1, 2, 3) ({})'.format(
+ unittest.util.strclass(self.GoodAdditionParams))
+ self.assertEqual(expected, str(test))
+
def test_dict_parameters(self):
ts = unittest.makeSuite(self.DictionaryArguments)
res = unittest.TestResult()
@@ -481,24 +478,21 @@ class ParameterizedTestsTest(absltest.TestCase):
self.assertEqual(4, ts.countTestCases())
short_descs = [x.shortDescription() for x in list(ts)]
full_class_name = unittest.util.strclass(self.NoParameterizedTests)
+ full_class_name = full_class_name.replace('__main__.', '')
self.assertSameElements(
[
- 'testGenerator (%s)' % (full_class_name,),
- 'test_generator (%s)' % (full_class_name,),
- 'testNormal (%s)' % (full_class_name,),
- 'test_normal (%s)' % (full_class_name,),
+ '{}.testGenerator'.format(full_class_name),
+ '{}.test_generator'.format(full_class_name),
+ '{}.testNormal'.format(full_class_name),
+ '{}.test_normal'.format(full_class_name),
],
short_descs)
- def test_generator_tests(self):
- with self.assertRaises(AssertionError):
-
- # This fails because the generated test methods share the same test id.
- class GeneratorTests(parameterized.TestCase):
+ def test_generator_tests_disallowed(self):
+ with self.assertRaisesRegex(RuntimeError, 'generated.*without'):
+ class GeneratorTests(parameterized.TestCase): # pylint: disable=unused-variable
test_generator_method = (lambda self: None for _ in range(10))
- del GeneratorTests
-
def test_named_parameters_run(self):
ts = unittest.makeSuite(self.NamedTests)
self.assertEqual(9, ts.countTestCases())
@@ -576,47 +570,20 @@ class ParameterizedTestsTest(absltest.TestCase):
def test_named_parameters_short_description(self):
ts = sorted(unittest.makeSuite(self.NamedTests),
key=lambda t: t.id())
- short_desc = ts[0].shortDescription().split('\n')
- self.assertEqual(
- 'test_dict_single_interesting(case=0)', short_desc[1])
- self.assertTrue(
- short_desc[0].startswith('test_dict_single_interesting'))
-
- short_desc = ts[1].shortDescription().split('\n')
- self.assertEqual(
- 'test_dict_something_boring(case=1)', short_desc[1])
- self.assertTrue(
- short_desc[0].startswith('test_dict_something_boring'))
-
- short_desc = ts[2].shortDescription().split('\n')
- self.assertEqual(
- 'test_dict_something_interesting(case=0)', short_desc[1])
- self.assertTrue(
- short_desc[0].startswith('test_dict_something_interesting'))
-
- short_desc = ts[3].shortDescription().split('\n')
- self.assertEqual(
- 'test_mixed_something_boring(1)', short_desc[1])
- self.assertTrue(
- short_desc[0].startswith('test_mixed_something_boring'))
-
- short_desc = ts[4].shortDescription().split('\n')
- self.assertEqual(
- 'test_mixed_something_interesting(case=0)', short_desc[1])
- self.assertTrue(
- short_desc[0].startswith('test_mixed_something_interesting'))
-
- short_desc = ts[6].shortDescription().split('\n')
- self.assertEqual(
- 'test_something_boring(1)', short_desc[1])
- self.assertTrue(
- short_desc[0].startswith('test_something_boring'))
-
- short_desc = ts[7].shortDescription().split('\n')
- self.assertEqual(
- 'test_something_interesting(0)', short_desc[1])
- self.assertTrue(
- short_desc[0].startswith('test_something_interesting'))
+ actual = {t._testMethodName: t.shortDescription() for t in ts}
+ expected = {
+ 'test_dict_single_interesting': 'case=0',
+ 'test_dict_something_boring': 'case=1',
+ 'test_dict_something_interesting': 'case=0',
+ 'test_mixed_something_boring': '1',
+ 'test_mixed_something_interesting': 'case=0',
+ 'test_something_boring': '1',
+ 'test_something_interesting': '0',
+ }
+ for test_name, param_repr in expected.items():
+ short_desc = actual[test_name].split('\n')
+ self.assertIn(test_name, short_desc[0])
+ self.assertEqual('{}({})'.format(test_name, param_repr), short_desc[1])
def test_load_tuple_named_test(self):
loader = unittest.TestLoader()
@@ -861,19 +828,12 @@ class ParameterizedTestsTest(absltest.TestCase):
res = RecordSuccessTestsResult()
ts.run(res)
self.assertTrue(res.wasSuccessful())
- self.assertEqual(9, res.testsRun)
+ self.assertEqual(2, res.testsRun)
test_ids = [test.id() for test in res.successful_tests]
full_class_name = unittest.util.strclass(self.UniqueDescriptiveNamesTest)
expected_test_ids = [
- full_class_name + '.test_normal(13)',
- full_class_name + '.test_normal(13) (2)',
- full_class_name + '.test_double_conflict(13)',
- full_class_name + '.test_double_conflict(13) (2)',
- full_class_name + '.test_double_conflict(13) (2) (2)',
- full_class_name + '.test_triple_conflict(13)',
- full_class_name + '.test_triple_conflict(13) (2)',
- full_class_name + '.test_triple_conflict(13) (2) (2)',
- full_class_name + '.test_triple_conflict(13) (3)',
+ full_class_name + '.test_normal0 (13)',
+ full_class_name + '.test_normal1 (13)',
]
self.assertTrue(test_ids)
self.assertItemsEqual(expected_test_ids, test_ids)
@@ -892,13 +852,13 @@ class ParameterizedTestsTest(absltest.TestCase):
self.assertEqual(8, res.testsRun)
self.assertTrue(res.wasSuccessful(), msg=str(res.failures))
- def test_subclass_inherits_superclass_test_method_ids(self):
+ def test_subclass_inherits_superclass_test_params_reprs(self):
self.assertEqual(
- {'test_name0': "test_name('foo')", 'test_name1': "test_name('bar')"},
- self.SuperclassTestCase._test_method_ids)
+ {'test_name0': "('foo')", 'test_name1': "('bar')"},
+ self.SuperclassTestCase._test_params_reprs)
self.assertEqual(
- {'test_name0': "test_name('foo')", 'test_name1': "test_name('bar')"},
- self.SubclassTestCase._test_method_ids)
+ {'test_name0': "('foo')", 'test_name1': "('bar')"},
+ self.SubclassTestCase._test_params_reprs)
def _decorate_with_side_effects(func, self):
diff --git a/absl/testing/tests/xml_reporter_test.py b/absl/testing/tests/xml_reporter_test.py
index f4a6c2d..a7c3f9d 100755
--- a/absl/testing/tests/xml_reporter_test.py
+++ b/absl/testing/tests/xml_reporter_test.py
@@ -135,7 +135,8 @@ class TextAndXMLTestResultTest(absltest.TestCase):
'foo', 0, timer)
def _assert_match(self, regex, output):
- self.assertRegex(output, regex)
+ fail_msg = 'Expected regex:\n{}\nTo match:\n{}'.format(regex, output)
+ self.assertRegex(output, regex, fail_msg)
def _assert_valid_xml(self, xml_output):
try:
@@ -786,7 +787,7 @@ class TextAndXMLTestResultTest(absltest.TestCase):
'errors': 0,
'run_time': run_time,
'start_time': re.escape(self._iso_timestamp(start_time),),
- 'test_name': re.escape('test_prefix(&apos;a&#x20;(b.c)&apos;)'),
+ 'test_name': re.escape('test_prefix0&#x20;(&apos;a&#x20;(b.c)&apos;)'),
'classname': classname,
'status': 'run',
'result': 'completed',