Skip to content

Commit e5f96d9

Browse files
yileicopybara-github
authored andcommitted
Add a test case for parameterized async tests using IsolatedAsyncioTestCase.
PiperOrigin-RevId: 561801233
1 parent 8e3ad2e commit e5f96d9

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

absl/testing/tests/parameterized_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,35 @@ def test_subclass_inherits_superclass_test_params_reprs(self):
998998
)
999999

10001000

1001+
# IsolatedAsyncioTestCase is only available in Python 3.8+.
1002+
if sys.version_info[:2] >= (3, 8):
1003+
1004+
async def mult(x: float, y: float) -> float:
1005+
return x * y
1006+
1007+
class AsyncTest(unittest.IsolatedAsyncioTestCase, parameterized.TestCase):
1008+
1009+
def setUp(self):
1010+
super().setUp()
1011+
self.verify_ran = False
1012+
1013+
def tearDown(self):
1014+
super().tearDown()
1015+
1016+
# We need the additional check here because originally the test function
1017+
# would run, but a coroutine results from the execution and is never
1018+
# awaited, so it looked like a successful test run when in fact the
1019+
# internal test logic never executed. If you remove the check for
1020+
# coroutine and run_until_complete, then set the parameters to fail they
1021+
# never will.
1022+
self.assertTrue(self.verify_ran)
1023+
1024+
@parameterized.parameters((1, 2, 2), (2, 2, 4), (3, 2, 6))
1025+
async def test_multiply_expected_matches_actual(self, x, y, expected):
1026+
self.assertEqual(await mult(x, y), expected)
1027+
self.verify_ran = True
1028+
1029+
10011030
def _decorate_with_side_effects(func, self):
10021031
self.sideeffect = True
10031032
func(self)

0 commit comments

Comments
 (0)