在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发布于 2022-05-06 19:02:41
您正在遍历函数返回的数据结构的错误部分。循环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变量将永远不会被分配给,因此函数末尾的return语句将引发异常。如果您认为这种情况可能发生,您可能应该设置一个默认值,或者在遇到这种情况时引发您自己的异常(带有更明确的信息)。
https://stackoverflow.com/questions/72146094
复制相似问题