小编Vin*_*uza的帖子

TypeError:super() 参数 1 必须是类型,而不是 MagicMock - azure-devops python 包

我正在使用 azure-devOps python 模块访问 azure API 以检索一些信息。我已使用以下代码来设置建立连接所需的身份验证。

def retrieve_projects_from_organization(self): 
   credentials = BasicAuthentication('', AZURE_PRIVATE_TOKEN)
   self.azure_connection = Connection(base_url=os.environ.get('AZURE_BASE_URL',''),creds=credentials)
   core_client = self.azure_connection.clients_v6_0.get_core_client()
   project_list = []
   projects_response = core_client.get_projects()
   while projects_response is not None:
     for project in projects_response:
        project_list.append(project)
Run Code Online (Sandbox Code Playgroud)

在编写单元测试用例时,我无法模拟 Connection 类,因为它需要 BasicAuthentication 类型的对象。以下是我的单元测试用例:

def test_retrieve_projects_from_organization(mocker):
    credentials = mocker.patch('msrest.authentication. BasicAuthentication')).return_value
    connection = mocker.patch('azure.devops.connection.Connection').return_value
    core_client = mocker.patch('azure.devops.v6_0.core.CoreClient').return_value
    projects = mocker.patch('azure.devops.v6_0.core.TeamProjectReference').return_value
    connection._creds = type(credentials)
    connection.clients_v6_0.get_core_client.return_value = core_client
    core_client.get_projects.return_value = [projects]

    assert AzureDataFetcher().retrieve_projects_from_organization() == [projects]
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

类型错误:super() 参数 1 必须是类型,而不是 MagicMock

是否有不同的方法来模拟此类或绕过 API 身份验证的方法。

python pytest python-3.x pytest-mock

4
推荐指数
1
解决办法
2965
查看次数

标签 统计

pytest ×1

pytest-mock ×1

python ×1

python-3.x ×1