Given the following code that attempts to configure the shell_with_tag()
to be mocked by `mock_shell_with_tag():
#!/usr/bin/env python3
import pytest
def shell(cmdline, debug=True):
return f"Original Shell command for {cmdline}.", None
def shell_with_tag(cmdline, debug=True, test_tag=None):
return shell(cmdline, debug)
def mock_shell_with_tag(cmd, test_tag=None):
if 'udf delete' in cmd:
return ['MOCK Record deleted' if test_tag == 'success' else 'Not Found. ', None]
elif 'udf show' in cmd:
return ['MOCK Record shown' if test_tag == 'success' else 'Not Found. ', None]
else:
raise Exception(f"mock_shell: what function is this {cmd}")
def testUdfs(mocker):
mocker.patch('tests.mocked.test_mock.shell_with_tag', mock_shell_with_tag)
def testDelete(name, expected_delete='true'):
out,err=shell_with_tag(f"udf delete --name {name}", test_tag ='success' if expected_delete else 'failure')
print(f"Results from UDF delete: {out} err: {err}")
assert 'MOCK Record' in out, "We went to the real one not the mock"
testDelete('abc')
Note that the mocked function tests.mocked.test_udfs.shell_with_tag()
was found: if it were not then an error message gets generated: it took some effort before getting this correctly.
But when running the testDelete
we get the original function invoked instead of the mocked function. What is missing to cause the mock function to activate?