Not sure if this is expected behaviour but I would appreciate some guidance about how to make this work.
I have a command like application that internally does things I don't want it to do while running unit tests. Therefore I'm using mock.patch to replace those implementation details. As the application is composed by commands and subcommands, using click.testing.CliRunner is very useful to us instead of calling the functions directly, specially because of the passed context between commands.
What we realised is that it seems that click.testing.CliRunner is ignoring patched functions using mock.patch. To showcase this behaviour I created the following sample that more or less describes what I'm trying to achieve: Ruenzuo/click_mock_patch.
Specifically this is the test that fails:
def test_app_should_not_raise(self):
with patch('helpers.helper.do_something_useful', return_value = None):
self.runner.invoke(entry_point, ['--opt1', 'Hi', '--opt2', 'You', 'cmd'], None, None, False)
The patch is working fine, if I debug the tests, inside the with patch context, the do_something_useful function returns None instead of raising (it's original implementation).
This had nothing to do with click, it was just my lack of understanding of "where to patch". Updated code works.
Most helpful comment
This had nothing to do with
click, it was just my lack of understanding of "where to patch". Updated code works.