1

I have a file I am trying to test. I have been able to test everything but causing a ClientRequestError with the library. I want to mock the connection to https://dev.azure.com/ms/_apis/projects/calculator and essentially cause a connection error to this endpoint so the library throws the ClientRequestError.

This is what I am trying to test

from azure.devops.connection import Connection
from azure.devops.exceptions import AzureDevOpsServiceError
from msrest.exceptions import ClientRequestError

...

connection = Connection(
    base_url=f"https://dev.azure.com/ms"
)

try:
    core_client = connection.clients.get_core_client()
    core_client.get_project("calculator")
except AzureDevOpsServiceError as exception:
    _LOGGER.warning(exception)
    ...
except ClientRequestError as exception:
    _LOGGER.warning(exception)
    ...

The ClientRequestError being the one I want to cover.

Timmo
  • 2,266
  • 4
  • 34
  • 54

1 Answers1

0

I see you didnot provide the credentials for the Connection constructor. You need to be authenticated to connected to azure devops server.

You can try getting a Person Access Token and provide the credentials for the connection to azure devops organization. Check out below example:

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.exceptions import AzureDevOpsServiceError
from msrest.exceptions import ClientRequestError


token = 'Personal Access Token'

credentials = BasicAuthentication("", token)
connection = Connection(
    base_url=f"https://dev.azure.com/ms", 
    creds=credentials
)

try:
    core_client = connection.clients.get_core_client()
    core_client.get_project("calculator")
except AzureDevOpsServiceError as exception:
    ....

You can also check out the example here.

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
  • not sure, my comment got deleted for no reason? the person is not asking how to mock the method – 4c74356b41 Apr 15 '20 at 04:45
  • I copied the code in the question, and tried to reproduce op's issue. I got not authorizated error instead of ClientRequestError. Then i noted the credentials was not provided. I could successfully run above code after the credentials were provided. So i thought it might be the credentials not being provided that caused the error. @4c74356b41 Do you have any insight about this case? – Levi Lu-MSFT Apr 17 '20 at 10:03
  • do you know what a `mock` is? apparently not. https://stackoverflow.com/questions/2665812/what-is-mocking – 4c74356b41 Apr 17 '20 at 10:04