Skip to content

Commit 3d48ff9

Browse files
authored
Merge pull request #403 from pytest-dev/fix-tests-py311-py312
Fix tests for Python 3.11 and 3.12
2 parents 7f9c131 + 6da5b05 commit 3d48ff9

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

tests/test_pytest_mock.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
# Python 3.8 changed the output formatting (bpo-35500), which has been ported to mock 3.0
2727
NEW_FORMATTING = sys.version_info >= (3, 8)
28+
# Python 3.11.7 changed the output formatting, https://github.com/python/cpython/issues/111019
29+
NEWEST_FORMATTING = sys.version_info >= (3, 11, 7)
2830

2931
if sys.version_info[:2] >= (3, 8):
3032
from unittest.mock import AsyncMock
@@ -240,15 +242,18 @@ def test_repr_with_name(self, mocker: MockerFixture) -> None:
240242

241243
def __test_failure_message(self, mocker: MockerFixture, **kwargs: Any) -> None:
242244
expected_name = kwargs.get("name") or "mock"
243-
if NEW_FORMATTING:
245+
if NEWEST_FORMATTING:
246+
msg = "expected call not found.\nExpected: {0}()\n Actual: not called."
247+
elif NEW_FORMATTING:
244248
msg = "expected call not found.\nExpected: {0}()\nActual: not called."
245249
else:
246250
msg = "Expected call: {0}()\nNot called"
247251
expected_message = msg.format(expected_name)
248252
stub = mocker.stub(**kwargs)
249-
with pytest.raises(AssertionError) as exc_info:
253+
with pytest.raises(
254+
AssertionError, match=re.escape(expected_message)
255+
) as exc_info:
250256
stub.assert_called_with()
251-
assert str(exc_info.value) == expected_message
252257

253258
def test_failure_message_with_no_name(self, mocker: MagicMock) -> None:
254259
self.__test_failure_message(mocker)

0 commit comments

Comments
 (0)