繁体   English   中英

匹配嵌套字典中的值的问题

[英]Problems matching values from nested dictionary

在 TestRail 中,我创建了几个测试运行。 当我执行时:

test_runs = client.send_get('get_runs/1')
pprint(test_runs)

返回以下结果:

{'_links': {'next': None, 'prev': None},
 'limit': 250,
 'offset': 0,
 'runs': [{'assignedto_id': None,
           'blocked_count': 0,
           'completed_on': None,
           'config': None,
           'config_ids': [],
           'created_by': 1,
           'created_on': 1651790693,
           'custom_status1_count': 0,
           'custom_status2_count': 0,
           'custom_status3_count': 0,
           'custom_status4_count': 0,
           'custom_status5_count': 0,
           'custom_status6_count': 0,
           'custom_status7_count': 0,
           'description': None,
           'failed_count': 1,
           'id': 13,
           'include_all': False,
           'is_completed': False,
           'milestone_id': None,
           'name': '2022-05-05-testrun',
           'passed_count': 2,
           'plan_id': None,
           'project_id': 1,
           'refs': None,
           'retest_count': 0,
           'suite_id': 1,
           'untested_count': 0,
           'updated_on': 1651790693,
           'url': 'https://xxxxxxxxxx.testrail.io/index.php?/runs/view/13'},
          {'assignedto_id': None,
           'blocked_count': 0,
           'completed_on': 1650989972,
           'config': None,
           'config_ids': [],
           'created_by': 5,
           'created_on': 1650966329,
           'custom_status1_count': 0,
           'custom_status2_count': 0,
           'custom_status3_count': 0,
           'custom_status4_count': 0,
           'custom_status5_count': 0,
           'custom_status6_count': 0,
           'custom_status7_count': 0,
           'description': None,
           'failed_count': 0,
           'id': 9,
           'include_all': False,
           'is_completed': True,
           'milestone_id': None,
           'name': 'This is a new test run',
           'passed_count': 0,
           'plan_id': None,
           'project_id': 1,
           'refs': None,
           'retest_count': 0,
           'suite_id': 1,
           'untested_count': 3,
           'updated_on': 1650966329,
           'url': 'https://xxxxxxxxxx.testrail.io/index.php?/runs/view/9'}],
 'size': 2}

在我的代码中,我试图扫描所有生成的测试运行,通过匹配测试运行名称找到我感兴趣的测试运行,然后返回测试运行的 ID。

from pprint import pprint
from testrail import *


class connecting():
    def connectPostRun(self):
        client = APIClient('https://xxxxxxxxxx.testrail.io')
        client.user = 'abc@abc.com'
        client.password = 'abc123'

        test_run_name = '2022-05-05-testrun'

        test_runs = client.send_get('get_runs/1')
        pprint(test_runs)

        for test_run in test_runs:
            if test_run['name'] == test_run_name:
                run_id = test_run['id']
                break

        return run_id
        pprint(run_id)

c=connecting()
c.connectPostRun()

现在按原样执行代码会导致以下错误:

    if test_run['name'] == test_run_name:
TypeError: string indices must be integers

您正在遍历 function 返回的数据结构的错误部分。 for test_run in test_runs:仅迭代顶级字典的键( "_links""limit"等)。

您想要循环遍历test_runs['runs'] ,这将为您提供带有您匹配的"name"键的字典。 尝试让你的循环看起来像这样:

    for test_run in test_runs['runs']:
        if test_run['name'] == test_run_name:
            run_id = test_run['id']
            break

我注意到此代码中存在一个潜在问题,如果您从未找到匹配的运行,则永远不会分配run_id变量,因此 function 末尾的return语句将引发异常。 如果您认为这可能会发生,您应该设置一个默认值,或者在遇到这种情况时引发您自己的异常(带有更明确的消息)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM