|
3 | 3 | import re
|
4 | 4 | import unittest
|
5 | 5 |
|
6 |
| -from unittest.mock import (ANY, call, AsyncMock, patch, MagicMock, |
| 6 | +from unittest.mock import (ANY, call, AsyncMock, patch, MagicMock, Mock, |
7 | 7 | create_autospec, sentinel, _CallList)
|
8 | 8 |
|
9 | 9 |
|
@@ -228,33 +228,50 @@ async def test_async():
|
228 | 228 |
|
229 | 229 |
|
230 | 230 | class AsyncSpecTest(unittest.TestCase):
|
231 |
| - def test_spec_as_async_positional_magicmock(self): |
232 |
| - mock = MagicMock(async_func) |
233 |
| - self.assertIsInstance(mock, MagicMock) |
234 |
| - m = mock() |
235 |
| - self.assertTrue(inspect.isawaitable(m)) |
236 |
| - asyncio.run(m) |
| 231 | + def test_spec_normal_methods_on_class(self): |
| 232 | + def inner_test(mock_type): |
| 233 | + mock = mock_type(AsyncClass) |
| 234 | + self.assertIsInstance(mock.async_method, AsyncMock) |
| 235 | + self.assertIsInstance(mock.normal_method, MagicMock) |
237 | 236 |
|
238 |
| - def test_spec_as_async_kw_magicmock(self): |
239 |
| - mock = MagicMock(spec=async_func) |
240 |
| - self.assertIsInstance(mock, MagicMock) |
241 |
| - m = mock() |
242 |
| - self.assertTrue(inspect.isawaitable(m)) |
243 |
| - asyncio.run(m) |
| 237 | + for mock_type in [AsyncMock, MagicMock]: |
| 238 | + with self.subTest(f"test method types with {mock_type}"): |
| 239 | + inner_test(mock_type) |
244 | 240 |
|
245 |
| - def test_spec_as_async_kw_AsyncMock(self): |
246 |
| - mock = AsyncMock(spec=async_func) |
247 |
| - self.assertIsInstance(mock, AsyncMock) |
248 |
| - m = mock() |
249 |
| - self.assertTrue(inspect.isawaitable(m)) |
250 |
| - asyncio.run(m) |
| 241 | + def test_spec_normal_methods_on_class_with_mock(self): |
| 242 | + mock = Mock(AsyncClass) |
| 243 | + self.assertIsInstance(mock.async_method, AsyncMock) |
| 244 | + self.assertIsInstance(mock.normal_method, Mock) |
251 | 245 |
|
252 |
| - def test_spec_as_async_positional_AsyncMock(self): |
253 |
| - mock = AsyncMock(async_func) |
254 |
| - self.assertIsInstance(mock, AsyncMock) |
255 |
| - m = mock() |
256 |
| - self.assertTrue(inspect.isawaitable(m)) |
257 |
| - asyncio.run(m) |
| 246 | + def test_spec_mock_type_kw(self): |
| 247 | + def inner_test(mock_type): |
| 248 | + async_mock = mock_type(spec=async_func) |
| 249 | + self.assertIsInstance(async_mock, mock_type) |
| 250 | + with self.assertWarns(RuntimeWarning): |
| 251 | + # Will raise a warning because never awaited |
| 252 | + self.assertTrue(inspect.isawaitable(async_mock())) |
| 253 | + |
| 254 | + sync_mock = mock_type(spec=normal_func) |
| 255 | + self.assertIsInstance(sync_mock, mock_type) |
| 256 | + |
| 257 | + for mock_type in [AsyncMock, MagicMock, Mock]: |
| 258 | + with self.subTest(f"test spec kwarg with {mock_type}"): |
| 259 | + inner_test(mock_type) |
| 260 | + |
| 261 | + def test_spec_mock_type_positional(self): |
| 262 | + def inner_test(mock_type): |
| 263 | + async_mock = mock_type(async_func) |
| 264 | + self.assertIsInstance(async_mock, mock_type) |
| 265 | + with self.assertWarns(RuntimeWarning): |
| 266 | + # Will raise a warning because never awaited |
| 267 | + self.assertTrue(inspect.isawaitable(async_mock())) |
| 268 | + |
| 269 | + sync_mock = mock_type(normal_func) |
| 270 | + self.assertIsInstance(sync_mock, mock_type) |
| 271 | + |
| 272 | + for mock_type in [AsyncMock, MagicMock, Mock]: |
| 273 | + with self.subTest(f"test spec positional with {mock_type}"): |
| 274 | + inner_test(mock_type) |
258 | 275 |
|
259 | 276 | def test_spec_as_normal_kw_AsyncMock(self):
|
260 | 277 | mock = AsyncMock(spec=normal_func)
|
|
0 commit comments