pythontestingmocking

Why did the mocking API failed?


My project is huge, I tried to write unitest for one API session

import unittest
from unittest.mock import patch, MagicMock
from alm_commons_utils.mylau.lau_client import lauApiSession


class TestlauApiSession(unittest.TestCase):
    @patch("alm_commons_utils.mylau.lau_client.lauApiSession.get_component")
    def test_mock_get_component(self, mock_get_component):
        # Mock the return value of get_component
        mock_get_component.return_value = {"component": "mock_component"}

        # Initialize the lauApiSession
        session = lauApiSession(
            ldap_user="mock_user",
            ldap_password="mock_password",
            lau_api_url="https://mock-lau-api-url",
            lau_login_url="https://mock-lau-login-url"
        )

        # Call the mocked method
        result = session.get_component("mock_repo")

        # Assert the mocked method was called with the correct arguments
        mock_get_component.assert_called_once_with("mock_repo")

        # Assert the return value is as expected
        self.assertEqual(result, {"component": "mock_component"})


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

I am running it as a job on Github Action,got error

Run python -m unittest "alm_commons_utils.lautest.mock_lau_client" 
2025-06-03 10:58:33,011 - asyncio - DEBUG - Using selector: EpollSelector
2025-06-03 10:58:33,012 [INFO] Retrieving token from lau API... (alm_commons_utils.mylau.lau_client)
2025-06-03 10:58:33,012 - alm_commons_utils.mylau.lau_client - INFO - Retrieving token from lau API...
Error: -03 10:58:33,038 [ERROR] Error retrieving token: Cannot connect to host mock-lau-login-url:443 ssl:False [Name or service not known] (alm_commons_utils.mylau.lau_client)
2025-06-03 10:58:33,012 - root - INFO - Entry login_corporativo()
2025-06-03 10:58:33,012 - root - INFO - Entry on endpoint component_yaml_login_corporativo
2025-06-03 10:58:33,012 - root - DEBUG - Yaml alias is: mock_user
2025-06-03 10:58:33,012 - root - INFO - Exit on endpoint component_yaml_login_corporativo
2025-06-03 10:58:33,019 - root - ERROR - Timeout trying to make login
2025-06-03 10:58:33,022 - root - ERROR - Timeout trying to make login
2025-06-03 10:58:33,038 - alm_commons_utils.mylau.lau_client - ERROR - Error retrieving token: Cannot connect to host mock-lau-login-url:443 ssl:False [Name or service not known]

I want to tell him not to connect, just to mock everything as a real life scenario. What is wrong with my code?


Solution

  • Your output in line #4 suggests that you are not retrieving the token. So basically your test is trying to retrieve a token from the URL https://mock-lau-login-url, your lau_login_url. You'll need to mock also the token when you want to run the lauApiSession, since the constructor of laupApiSession is still making real network calls.

    Option #1:

    __Patch the method that retrieves the token__
    @patch("alm_commons_utils.mylau.lau_client.lauApiSession.get_component")
    @patch("alm_commons_utils.mylau.lau_client.lauApiSession._get_token")  # or whatever the method is
    def test_mock_get_component(self, mock_get_token, mock_get_component):
        mock_get_token.return_value = "mock_token"
        mock_get_component.return_value = {"component": "mock_component"}
    
        session = lauApiSession(
            ldap_user="mock_user",
            ldap_password="mock_password",
            lau_api_url="https://mock-lau-api-url",
            lau_login_url="https://mock-lau-login-url"
        )
    
        result = session.get_component("mock_repo")
        mock_get_component.assert_called_once_with("mock_repo")
        self.assertEqual(result, {"component": "mock_component"})
    

    Option #2:

    __Patch the entire constructor (not ideal since your are not testing the constructor)__
    @patch("alm_commons_utils.mylau.lau_client.lauApiSession.__init__", return_value=None)
    @patch("alm_commons_utils.mylau.lau_client.lauApiSession.get_component")
    def test_mock_get_component(self, mock_get_component, mock_init):
        mock_get_component.return_value = {"component": "mock_component"}
    
        session = lauApiSession()
        session.get_component = mock_get_component  # manually assign if needed
    
        result = session.get_component("mock_repo")
        mock_get_component.assert_called_once_with("mock_repo")
        self.assertEqual(result, {"component": "mock_component"})