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