Skip to content Skip to sidebar Skip to footer

Python Http Post Method Returns Response As Magicmock Object Instead Of Value

I am trying to check the response status code after trigerring some API with a POST method, Response status code is of Magicmock instance type, i am checking whether the status cod

Solution 1:

This isn't exactly a fix, more of a workaround.

Instead of making that <= comparison, write a separate method:

defis_4xx_or_5xx_code(status_code):
    return400 <= status_code <= 500if is_4xx_or_5xx_code(status_code=response.status_code):
    print('works')

Then mock it in your tests.

@mock.patch('path.to_code.under_test.is_4xx_or_5xx_code')deftest_your_method(mock_status_code):
    mock_status_code.return_value = True# rest of the test.

Post a Comment for "Python Http Post Method Returns Response As Magicmock Object Instead Of Value"