oslotest/oslotest
Claudiu Belu 0bde35899a Adds mock autospec fixture
In typical unit tests, almost all of the dependencies are mocked or
patched (mock.patch), without any guarantee that the mocked methods
actually exist, or if their signatures are respected (see below). Because
of this, actual issues can easily be overlooked and missed, as the unit
tests are wrongfully passing.

The mock.Mock class accepts a spec as an argument, which only solves half
the problem: it only checks if an attribute exists, based on the given
spec. It does not guarantee that the given attribute is actually a method,
or if its signature is respected. Some unit tests may pass the autospec
argument, but mock doesn't support it at the moment.

mock.patch, mock.patch.object, mock.patch.multiple accept an autospec
argument, but because of a bug, it cannot be used properly.

Adds a fixture which replaces mock.Mock and mock.MagicMock with
subclass which accepts the autospec argument, and on call, it will
check the signature of the called method / function.

Adds a function which replaces mock.mock._patch with a subclass, which
treats the autospec argument properly (consumes the self / cls argument),
and sets autospec=True by default, unless otherwise specified.
WARNING: this function is not a fixture, and in order to benefit from it,
it will have to be called as EARLY as possible, before any test classes
are loaded, otherwise the original mock.mock._patch is used instead.

Needed-By: I3636833962c905faa0f144c7fdc4833037324d31
Needed-By: I4484e63c97bd1cdde3d88855eabe7545784f365e

Closes-Bug: #1735588

Change-Id: I0e4a55fbf4c1d175726ca22b664e240849a99856
2018-01-06 12:21:14 -08:00
..
tests Adds mock autospec fixture 2018-01-06 12:21:14 -08:00
tools Restore missing module for pre-release test script 2015-02-20 12:47:00 -05:00
__init__.py Remove six.moves call 2015-01-15 11:24:07 +01:00
base.py Hack to get back stopall cleanup behavior feature 2016-02-17 06:54:22 -05:00
createfile.py Add CreateFileWithContent fixture 2015-06-12 19:15:18 +00:00
functional.py Deprecate oslotest.functional 2017-08-21 12:01:40 +02:00
log.py Allow TRACE and integer logging levels for 'OS_DEBUG' 2015-07-27 17:58:30 -07:00
mock_fixture.py Adds mock autospec fixture 2018-01-06 12:21:14 -08:00
modules.py A DisableModules fixture that removes modules from path 2016-07-06 15:48:24 +10:00
moxstubout.py Remove six.moves call 2015-01-15 11:24:07 +01:00
output.py Create CaptureOutput fixture 2015-06-12 14:35:28 +00:00
timeout.py Create Timeout wrapper fixture 2015-06-12 14:34:51 +00:00