匹配嵌套字典中的值的问题
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
您正在遍历函数返回的数据结构的错误部分。循环 for test_run in test_runs:
仅遍历 top-level 字典的键("_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
变量将永远不会被赋值,所以 return
函数末尾的语句将引发异常。如果您认为这可能会发生,您可能应该设置一个默认值,或者在遇到这种情况时提出您自己的异常(带有更明确的消息)。
在 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
您正在遍历函数返回的数据结构的错误部分。循环 for test_run in test_runs:
仅遍历 top-level 字典的键("_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
变量将永远不会被赋值,所以 return
函数末尾的语句将引发异常。如果您认为这可能会发生,您可能应该设置一个默认值,或者在遇到这种情况时提出您自己的异常(带有更明确的消息)。