[openstack-dev] [cinder] How to make a mock effactive for all method of a testclass

Brant Knudson blk at acm.org
Wed Sep 23 13:46:40 UTC 2015


On Wed, Sep 23, 2015 at 3:06 AM, liuxinguo <liuxinguo at huawei.com> wrote:

> Hi,
>
>
>
> In a.py we have a function:
>
> def *_change_file_mode*(filepath):
>
> utils.execute(*'chmod'*, *'600'*, filepath, run_as_root=True)
>
>
>
> In test_xxx.py, there is a testclass:
>
> clas*s xxxxDriverTestCase(test.TestCase):*
>
> *def test_a(self)*
>
> *    …*
>
> *    Call a. _change_file_mode*
>
> *…*
>
>
>
> *def test_b(self)*
>
> *    …*
>
> *    Call a. _change_file_mode*
>
> *…*
>
>
>
> I have tried to mock like mock out function *_change_file_mode *like this:
>
> *@mock.patch.object*(a, *'_change_file_mode', *return_value=None)
>
> clas*s xxxxDriverTestCase(test.TestCase):*
>
> *def test_a(self)*
>
> *    …*
>
> *    Call a. _change_file_mode*
>
> *…*
>
>
>
> *def test_b(self)*
>
> *    …*
>
> *    Call a. _change_file_mode*
>
> *…*
>
>
>
> But the mock takes no effort, the real function _change_file_mode is still
> executed.
>
> So how to make a mock effactive for all method of a testclass?
>
> Thanks for any input!
>
>
>

Use oslotest's mockpatch.PatchObject fixture:
http://docs.openstack.org/developer/oslotest/api.html#oslotest.mockpatch.PatchObject


> Wilson Liu
>
> __________________________________________________________________________
> OpenStack Development Mailing List (not for usage questions)
> Unsubscribe: OpenStack-dev-request at lists.openstack.org?subject:unsubscribe
> http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstack.org/pipermail/openstack-dev/attachments/20150923/45e9dd82/attachment.html>


More information about the OpenStack-dev mailing list