[TIP] Mock plumbum commands
oliver at bestwalter.de
Wed Mar 8 04:22:33 PST 2017
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(*_, **__):
print("I am mocked")
return "mock value"
LocalMachine.__getitem__ = get_item_mock
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...
More information about the testing-in-python