[TIP] Mock and multiprocessing on Windows
Stefan Scherfke
stefan at sofa-rockers.org
Tue Sep 25 01:23:31 PDT 2012
Hi,
[tl;dr] Below is a snippet that works on Linux/Mac but fails on Windows. Is there a way to fix it?
The example below fails on Windows, because it uses spawn() to start subprocesses. The python
state is being pickled and sent to the new process. However, pickling mock objects doesn’t
work, so the example fails.
Is there another way to mock something in a subprocess on Windows?
Below is a minimal example that prints
<class 'mock.MagicMock'>
eggs
on Mac/Linux and:
<class 'function'>
spam
on Windows:
# sub.py
def spam():
return 'spam'
def main():
print(type(spam))
print(spam())
# EOF
# main.py
import multiprocessing
import mock
import sub
def main():
with mock.patch('sub.spam') as spam_mock:
spam_mock.return_value = 'eggs'
p = multiprocessing.Process(target=sub.main)
p.start()
p.join()
if __name__ == '__main__':
main()
# EOF
Cheers,
Stefan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2772 bytes
Desc: not available
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20120925/d4e4f6b1/attachment.bin>
More information about the testing-in-python
mailing list