Why can't I patch multiprocessing.Process?

355 Views Asked by At

So I have these files:

transmitter.py

import multiprocessing as MP
from common import BaseWorker

class TxWorker(BaseWorker):
    ... code ...

common.py

import multiprocessing as MP

class BaseWorker(MP.Process):
    ... code ...

Now I have this test file, written for pytest with pytest-mock plugin:

from transmitter import TxWorker

class MockProcess:
    def __init__(self, name, *args, **kwargs):
        self.name = name

    def canary():
        return "Mocked!"

@pytest.fixture(scope="module")
def mocked_process(module_mocker):
    module_mocker.patch(WHERE_TO_PATCH, MockProcess)
    yield

def test_case(mocked_process):
    tw = TxWorker()
    assert tw.canary() == "Mocked!"

I can't get this test to work.

I've tried setting WHERE_TO_PATCH to:

  • transmitter.MP.Process
  • common.MP.Process
  • multiprocessing.Process

None of them works. I keep having the error AttributeError: 'TxWorker' object has no attribute 'canary'.

Where did I go wrong?

0

There are 0 best solutions below