简体   繁体   English

mock.patch 和多处理

[英]mock.patch and multiprocessing


I'm struggling to use mock.patch in a multiprocessing environment while without multiprocessing mock.patch works fine.我正在努力在多处理环境中使用mock.patch ,而没有 multiprocessing mock.patch工作正常。 Filename: test_mp.py文件名: test_mp.py

import multiprocessing
import mock

def inner():
    return sub()

def sub():
    return "abc"

def test_local():
    assert inner()=="abc"

def test_mp():    
    with multiprocessing.Pool() as pool:
        assert pool.apply(inner,args=[])=='abc'

def test_mock():    
    with mock.patch('test_mp.sub', return_value='xxx') as xx:
        assert inner()=="xxx"
        xx.assert_called_once()

def test_mp_mock():    
    with multiprocessing.Pool() as pool:
        with mock.patch('test_mp.sub', return_value='xyz') as xx:
            assert pool.apply(inner,args=[])=='xyz'
            xx.assert_called_once()

  • Test test_local , test_mock and test_mock finishes successfully测试test_localtest_mocktest_mock成功完成
  • However test_mp_mock fails with但是test_mp_mock失败了
=================================== FAILURES ===================================
_________________________________ test_mp_mock _________________________________

    def test_mp_mock():
        with multiprocessing.Pool() as pool:
            with mock.patch('test_mp.sub', return_value='xyz') as xx:
>               assert pool.apply(inner,args=[])=='xyz'
E               AssertionError: assert 'abc' == 'xyz'
E                 - abc
E                 + xyz

projects/mfhealth/test_mp.py:25: AssertionError

Update:更新:

Based on https://medium.com/uckey/how-mock-patch-decorator-works-in-python-37acd8b78ae and with help of sharedmock https://github.com/elritsch/python-sharedmock I was able to get further, but still not complete.基于https://medium.com/ukey/how-mock-patch-decorator-works-in-python-37acd8b78ae并借助sharedmock https://github.com/elritsch/python-sharedmock我能够进一步,但仍不完整。

I extended test_mp.py for我将test_mp.py扩展为

from sharedmock.mock import SharedMock

def inner2(sm):
    with mock.patch('test_mp.sub', sm) as xx:
        return inner()


def test_mp_smock():
    with multiprocessing.Pool() as pool:
        sm=SharedMock()
        sm.return_value="xyz"
        with mock.patch('test_mp.sub', sm) as xx:
            assert pool.apply(inner2,args=[sm])=='xyz'
            assert xx.call_count == 1

def test_mp_mock2():
    with multiprocessing.Pool() as pool:
        sm=mock.Mock()
        sm.return_value="xyz"
        print(f"before patch {sub}, {locals()}")
        with mock.patch('test_mp.sub', sm) as xx:
            print(f"after patch {sub}")
            assert pool.apply(inner2,args=[sm])=='xyz'
            assert xx.call_count == 1

With the following results:结果如下:

  • test_mp_smock finishes successfully. test_mp_smock成功完成。
  • test_mp_mock2 fails with _pickle.PicklingError: Can't pickle <class 'mock.mock.Mock'>: it's not the same object as mock.mock.Mock test_mp_mock2失败, _pickle.PicklingError: Can't pickle <class 'mock.mock.Mock'>: it's not the same object as mock.mock.Mock

The main disadvantage of test_mp_smock is that a new method inner2 must be introduced to activate patching via mock.patch . test_mp_smock的主要缺点是必须引入一个新方法inner2来通过mock.patch激活补丁。 Any idea of how to propagate patching from test_mp_smock to code under test without introducing a wrapper method inner2 because I cannot override.关于如何在不引入包装方法inner2的情况下将补丁从test_mp_smock传播到被测代码的任何想法,因为我无法覆盖。

pool.apply(inner,args=[])

Finally, I got it working.最后,我让它工作了。

SharedMock is not that flexible like Mock , eg. SharedMock不像Mock那样灵活,例如。 missing assert_called_once_with , ..., but you can set returned value or check the number of calls or its arguments.缺少assert_called_once_with ,...,但您可以设置返回值或检查调用次数或其 arguments。

import multiprocessing
import mock
from sharedmock.mock import SharedMock


def inner():
    return sub(x="xxx")

def sub(x=""):
    return f"abc"

def fun_under_test():
    with multiprocessing.Pool() as pool:
        assert pool.apply(inner,args=[])=='xyz'

def test_final():
    sm=SharedMock()
    sm.return_value="xyz"
    with mock.patch('test_mp.sub', sm) as xx:
        fun_under_test()
        assert xx.call_count == 1 #number of calls of sub function
        assert xx.mock_calls[0][2]['x']=="xxx" # value of parameters ie sub(x="xxx")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM