Add test_mock_new()

This commit is contained in:
Ralf W. Grosse-Kunstleve 2023-07-31 10:55:54 -07:00
parent c89561f77a
commit a2f95e1d94

View File

@ -1,3 +1,5 @@
from unittest import mock
import pytest
import env
@ -203,6 +205,18 @@ def test_inheritance_init(msg):
assert msg(exc_info.value) == expected
@pytest.mark.parametrize(
"mock_return_value", [None, (1, 2, 3), m.Pet("Polly", "parrot"), m.Dog("Molly")]
)
def test_mock_new(mock_return_value):
with mock.patch.object(
m.Pet, "__new__", return_value=mock_return_value
) as mock_new:
obj = m.Pet("Noname", "Nospecies")
assert obj is mock_return_value
mock_new.assert_called_once_with(m.Pet, "Noname", "Nospecies")
def test_automatic_upcasting():
assert type(m.return_class_1()).__name__ == "DerivedClass1"
assert type(m.return_class_2()).__name__ == "DerivedClass2"