Sashi K
Sashi K

Reputation: 51

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"

Upvotes: 0

Views: 246

Answers (1)

Sashi K
Sashi K

Reputation: 51

Solved! This is what it was happening: run_command() function was implemented in app.vm_create.utility.py, but it was imported by app.vm_create.controllers.py. When I tried to mock run_command by patching app.vm_create.utility.run_command (in utility where it was implemented), it didn't work. However, this worked:

@patch("app.vm_create.controllers.run_command")
def test_etc:
    etc

As you can see, I'm still patching run_command(), but this time, with the path of the module which is importing it, controllers.

Upvotes: 1

Related Questions