[TIP] Mock plumbum commands

Oliver Bestwalter oliver at bestwalter.de
Wed Mar 8 04:22:33 PST 2017

Hi Paulo,

On Wed, 8 Mar 2017 at 11:09 Paulo Matos <pmatos at linki.tools> wrote:

I would probably like that during testing, ls will return always the
same thing and for that I need to mock it. But what would be the correct

if you want local to return a simple function instead of the real commadn
you can directly monkeypatch the __getitem__ method of LocalMachine() local
is a module level instance of that class.


from plumbum.machines import LocalMachine

def get_item_mock(*_, **__):
    def i_am_the_mocked_function():
        print("I am mocked")
        return "mock value"

    return i_am_the_mocked_function

LocalMachine.__getitem__ = get_item_mock

def test_something_with_mocked_local():
    from plumbum import local

    assert local["dontcare"]() == "mock value"

if the formatting is mangled - I copied it into a gist:

if you are looking for a nice mocking framework I would recommend flexmock:

There you could say something like

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20170308/e4653af1/attachment.html>

More information about the testing-in-python mailing list