[Python] Why did the mocking API failed?

Discussão em 'Python' iniciado por Stack, Junho 3, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página