Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 347
MJoao Avatar asked Dec 01 '25 10:12

MJoao


1 Answers

Your problem was that you were mocking the function get_component but not the init of the class lauApiSession this will work now because we are mocking both the init and the get_component


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

class TestlauApiSession(unittest.TestCase):
    def test_mock_get_component(self):
        with patch("alm_commons_utils.mylau.lau_client.lauApiSession.__init__", return_value=None) as mock_init, \
             patch("alm_commons_utils.mylau.lau_client.lauApiSession.get_component") as mock_get_component:

            # __init__ is mocked; no network happens here
            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",
            )

            mock_get_component.return_value = {"component": "mock_component"}

            result = session.get_component("mock_repo")

            mock_init.assert_called_once()  # optional
            mock_get_component.assert_called_once_with("mock_repo")
            self.assertEqual(result, {"component": "mock_component"})

if __name__ == "__main__":
    unittest.main()
like image 138
omri Avatar answered Dec 03 '25 00:12

omri