Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Unit test Get Requests Assertion Error not picking up call

Tags:

python

I'm testing basic GET requests under from the requests module. I'm using requests.Session to create a session instance which is later passed into the function.

I'm mocking the the function calls. I've patched the session.get call within the function, however I get the following error:

AssertionError: get('https://example-name/v1/0001') call not found

I'm assuming I haven't patched the location correctly but I'm not sure where specifically I am going wrong.

main.py

import requests

base_url = "https://example-name/v1/"

session = requests.Session()

def fetch_data_with_session(session, base_url, endpoint):
    response = session.get(base_url + endpoint)  
    return response.json()

test_main.py

import unittest
from requests import Session
from main import fetch_data_with_session
from unittest.mock import Mock, patch


class TestingGet(unittest.TestCase):
    
    @patch.object(Session, 'get')
    def test_fetch(self, mock_get):
        data = {
                "id": "0001",
                "name": "John Doe",
                }
        mock_response = Mock()
        mock_response.json.return_value = data
        
        mock_get.return_value = mock_response
        
        mock_session = Mock()
        
        retrieved_get_data = fetch_data_with_session(mock_session, "https://example-name/v1/", "0001")
        
        mock_get.assert_any_call("https://example-name/v1/0001")
        
        self.assertEqual = (outages_data, data)

if __name__ == '__main__':
    unittest.main()

I also tried using the mock.get.assert_called_with and got a similar error.

assert_called_with
    raise AssertionError(error_message)
AssertionError: expected call not found.
Expected: get('https://example-name/v1/0001')
Actual: not called.

Again, I'm assuming I'm not patching the GET call correctly, but not sure how to correct it.

EDIT: Thanks @Guy who provided the answer. The updated module code is below. Please see @Guy's answer for the explanation

@patch.object(requests.Session, 'get')
def test_fetch(self, mock_get):
    data = {
            "id": "exampleID",
            "name": "example",
            "devices": [
                {
                "id": "0001",
                "name": "Device A"
                }
            ]
            }
    mock_response = Mock()
    mock_response.json.return_value = data
    
    mock_get.return_value = mock_response
    
    outages_data = fetch_data_with_session(mock_get, "https://example-name/v1/", "0001")
    
    mock_get.get.assert_called_with("https://example-name/v1/0001")
    
    self.assertEqual = (outages_data, data)
like image 956
and Avatar asked Oct 24 '25 16:10

and


1 Answers

There are two issues in you code

  1. You use mock_session for the API call but test mock_get for it

  2. mock_get and mock_session are the session.get function, not Session instance

Simplified test with two options

@patch.object(Session, 'get')
def test_fetch(self, mock_get):

    fetch_data_with_session(mock_get, "https://example-name/v1/", "0001")
    mock_get.assert_any_call("https://example-name/v1/0001")

    # or

    mock_session = Mock()
    fetch_data_with_session(mock_session, "https://example-name/v1/", "0001")
    mock_session.assert_any_call("https://example-name/v1/0001")

and main.py

def fetch_data_with_session(get, base_url, endpoint):
    response = get(base_url + endpoint)
    return response.json()
like image 179
Guy Avatar answered Oct 26 '25 05:10

Guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!