I am writing a unit test for the following function, and I'm looking first at the case where we reach the last line.
from azureml.core.authentication import InteractiveLoginAuthentication, ServicePrincipalAuthentication
def get_authentication() -> Union[InteractiveLoginAuthentication, ServicePrincipalAuthentication]:
service_principal_id = get_secret_from_environment(SERVICE_PRINCIPAL_ID, allow_missing=True)
tenant_id = get_secret_from_environment(TENANT_ID, allow_missing=True)
service_principal_password = get_secret_from_environment(SERVICE_PRINCIPAL_PASSWORD, allow_missing=True)
if service_principal_id and tenant_id and service_principal_password:
return ServicePrincipalAuthentication(
tenant_id=tenant_id,
service_principal_id=service_principal_id,
service_principal_password=service_principal_password)
logging.info("Using interactive login to Azure. To use Service Principal authentication")
return InteractiveLoginAuthentication()
And here is my very simple unit test:
@patch("azureml.core.authentication.InteractiveLoginAuthentication")
def test_get_authentication(mocked_interactive_authentication: Any) -> None:
util.get_authentication()
assert mocked_interactive_authentication.called
We never reach the assert
line of my unit test because InteractiveLoginAuthentication()
raises the exception
TypeError: super() argument 1 must be type, not MagicMock
Why is get_authentication
calling the actual constructor for InteractiveLoginAuthentication
and not using my patch?