'Not able to mock the method inside flask URL

I written below url in flask

@app_url.route('/createvm', methods=['GET', 'POST'], defaults={'buildid': None})
def form(buildid):
    command = prepare_ansible_command(data)
    success,reason = run_command(command)
    # here run_coomand method not returning mock return value.
    ....

writing below unit test case

@patch('app.vm_create.utility.run_command')
def test_vm_create_negative3(self, run_command_mock):
    run_command_mock.return_value = True, "response123456"
    from app.vm_create.utility import run_command 
    #I checked run_command here it's returning mock return value (True, "response123456")
    with self.client:
        resp = self.client.post("/signin/", data={"username": self.act.username, 
                                "password": self.password, "token":True})
        resp= self.client.post("/createvm", data=data)

The run_commnad returning mocked return value inside test method. It's not returning mock return value inside view function (createvm) running the above test using pytest pytest test_app.py -k "test_vm_create_negative3"



Solution 1:[1]

run_command definition written in app.vm_create.utility. Imported in to views file app.vm_create.controllers.py. My urls written in controllers.py. If I mock app.vm_create.utility.run_command then it's not working. If I mock app.vm_create.controllers.run_command then mocking is working.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sashi K