[New-bugs-announce] [issue46690] create_autospec() doesn't respect configure_mock style kwargs

James Marchant report at bugs.python.org
Wed Feb 9 06:54:00 EST 2022

New submission from James Marchant <marchant.jm at gmail.com>:

When using `create_autospec()` to create a mock object, it doesn't respect values passed through in the style described for passing mock configurations in the Mock constructor (https://docs.python.org/3.8/library/unittest.mock.html#unittest.mock.Mock.configure_mock). Instead, they seem to get discarded somewhere here (https://github.com/python/cpython/blob/77bab59c8a1f04922bb975cc4f11e5323d1d379d/Lib/unittest/mock.py#L2693-L2741).

Here's a simple test case:
from unittest.mock import create_autospec

class Test:
    def test_method(self):

autospec_mock = create_autospec(Test, instance=True, **{"test_method.side_effect": ValueError})

# Should throw a ValueError exception

# Assign manually
autospec_mock.test_method.side_effect = ValueError
# Throws as expected

components: Tests
messages: 412898
nosy: marchant.jm
priority: normal
severity: normal
status: open
title: create_autospec() doesn't respect configure_mock style kwargs
type: behavior
versions: Python 3.10, Python 3.8, Python 3.9

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list