diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-01-24 13:27:04 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-24 13:27:04 (GMT) |
commit | 0ea7309927d1b3efde9be986528b8855f0f4c955 (patch) | |
tree | 8435f6758fed14b13978761c63fd73fc8b866ab8 | |
parent | a46728a570e30e88df253eab17ad6c4372a422da (diff) | |
download | cpython-0ea7309927d1b3efde9be986528b8855f0f4c955.zip cpython-0ea7309927d1b3efde9be986528b8855f0f4c955.tar.gz cpython-0ea7309927d1b3efde9be986528b8855f0f4c955.tar.bz2 |
Improve test coverage for AsyncMock. (GH-17906)
* Add test for nested async decorator patch.
* Add test for side_effect and wraps with a function.
* Add test for side_effect with an exception in the iterable.
(cherry picked from commit 54f743eb315f00b0ff45e115dde7a5d506034153)
Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
-rw-r--r-- | Lib/unittest/test/testmock/testasync.py | 53 |
1 files changed, 49 insertions, 4 deletions
diff --git a/Lib/unittest/test/testmock/testasync.py b/Lib/unittest/test/testmock/testasync.py index be8e311..0bd7ae4 100644 --- a/Lib/unittest/test/testmock/testasync.py +++ b/Lib/unittest/test/testmock/testasync.py @@ -72,9 +72,17 @@ class AsyncPatchDecoratorTest(unittest.TestCase): test_async() def test_async_def_patch(self): - @patch(f"{__name__}.async_func", AsyncMock()) - async def test_async(): + @patch(f"{__name__}.async_func", return_value=1) + @patch(f"{__name__}.async_func_args", return_value=2) + async def test_async(func_args_mock, func_mock): + self.assertEqual(func_args_mock._mock_name, "async_func_args") + self.assertEqual(func_mock._mock_name, "async_func") + self.assertIsInstance(async_func, AsyncMock) + self.assertIsInstance(async_func_args, AsyncMock) + + self.assertEqual(await async_func(), 1) + self.assertEqual(await async_func_args(1, 2, c=3), 2) asyncio.run(test_async()) self.assertTrue(inspect.iscoroutinefunction(async_func)) @@ -370,22 +378,40 @@ class AsyncArguments(unittest.IsolatedAsyncioTestCase): with self.assertRaises(Exception): await mock(5) - async def test_add_side_effect_function(self): + async def test_add_side_effect_coroutine(self): async def addition(var): return var + 1 mock = AsyncMock(side_effect=addition) result = await mock(5) self.assertEqual(result, 6) + async def test_add_side_effect_normal_function(self): + def addition(var): + return var + 1 + mock = AsyncMock(side_effect=addition) + result = await mock(5) + self.assertEqual(result, 6) + async def test_add_side_effect_iterable(self): vals = [1, 2, 3] mock = AsyncMock(side_effect=vals) for item in vals: - self.assertEqual(item, await mock()) + self.assertEqual(await mock(), item) with self.assertRaises(StopAsyncIteration) as e: await mock() + async def test_add_side_effect_exception_iterable(self): + class SampleException(Exception): + pass + + vals = [1, SampleException("foo")] + mock = AsyncMock(side_effect=vals) + self.assertEqual(await mock(), 1) + + with self.assertRaises(SampleException) as e: + await mock() + async def test_return_value_AsyncMock(self): value = AsyncMock(return_value=10) mock = AsyncMock(return_value=value) @@ -432,6 +458,21 @@ class AsyncArguments(unittest.IsolatedAsyncioTestCase): mock.assert_awaited() self.assertTrue(ran) + async def test_wraps_normal_function(self): + value = 1 + + ran = False + def inner(): + nonlocal ran + ran = True + return value + + mock = AsyncMock(wraps=inner) + result = await mock() + self.assertEqual(result, value) + mock.assert_awaited() + self.assertTrue(ran) + class AsyncMagicMethods(unittest.TestCase): def test_async_magic_methods_return_async_mocks(self): m_mock = MagicMock() @@ -854,6 +895,10 @@ class AsyncMockAssert(unittest.TestCase): self.mock.assert_awaited_once() def test_assert_awaited_with(self): + msg = 'Not awaited' + with self.assertRaisesRegex(AssertionError, msg): + self.mock.assert_awaited_with('foo') + asyncio.run(self._runnable_test()) msg = 'expected await not found' with self.assertRaisesRegex(AssertionError, msg): |