与testrail的rest api接口的python库和cli
traw的Python项目详细描述
traw:testrail api包装器
(此项目现在处于测试阶段:如果没有弃用警告,API将不会更改)
traw旨在成为一个完整的python库,用于访问testrail的restful api。
快速参考
fromdatetimeimportdatetimeasdtimportrandomimporttrawclient=traw.Client(username='username',password='passwrod',url='url)project=client.project(15)# Get Project with Project ID of 15new_run=client.run()new_run.name="My new run name"new_run.description="My new run description"new_run.include_all=Truenew_run.project=projectrun=client.add(new_run)# Run is added to TestRailfortestinclient.tests(run):# Get all tests for runbegin=dt.now()# Do actual testing here, but lets pick a random statusstatus_str=random.choice(['passed','failed','retest'])elapsed=dt.now()-beginresult=client.result()result.test=testresult.status=client.status(status_str)result.comment="Setting {0} to {1}".format(test.title,result.status.label)result.elapsed=elapsed# Add the result to TestRailclient.add(result)# Everything complete, close the runclient.close(run)# Fín
安装
traw在pypi上可用,可以安装pip
$ pip install traw
凭证
traw客户端可以通过三种方式获取凭据:
在traw.client实例化期间将参数传递给
client=traw.Client(username='user@email.com',password='password',url='url')# client = traw.Client(username='user@email.com', user_api_key='userapikey', url='url')
设置环境变量
$ exportTRAW_USERNAME="user@email.com" $ exportTRAW_PASSWORD="userapikey" $ # export TRAW_USER_API_KEY="userapikey" # (Optional) - in place of TRAW_PASSWORD $ exportTRAW_URL="https://example.testrail.net"
将它们写入用户主目录中的配置文件
$ cat ~/.traw_config [TRAW]username= <username> password= <password> # user_api_key = <user_api_key> # (Optional) - in place of password url= <url>
您可以创建多个客户端来访问不同的TestRail安装:
client1=traw.Client(username='user1@email.com',password='password',url='https://example.testrail.net')client2=traw.Client(username='user2@email.com',password='password',url='https://your.domain.com')
创建/添加/关闭/删除/更新TestRail对象
traw使用一致的模式来创建新的testrail对象并将它们添加到testrail:
调用不带任何参数的相关客户端方法,将返回一个新的/空对象:
new_run=client.run()new_result=client.result()new_section=client.section()new_milestone=client.milestone()# etc
配置新对象。注意大多数可添加对象需要至少一个引用对象才能将它们添加到testrail。例如,运行对象需要对项目的引用,结果对象需要对测试的引用,如果项目未处于单套件模式,则截面对象需要对项目和套件的引用:
new_run.name="Run Name"new_run.project=client.project(15)# Project with Project ID 15new_result.comment="Result added by TRAW"new_result.test=client.tests(123)# Test with Test ID of 123new_result.status=client.status('passed')# Status with Status Label of 'passed'new_section.name="Suite Name"new_section.project=client.project(15)# Project with Project ID 15, with suite-mode of 2new_section.suite=client.suite(456)# Suite with Suite ID 456
此时,这些对象只存在于本地,并且尚未添加到testrail。为此,用新对象调用client.add()。traw将新对象添加到testrail,成功后testrail api将返回一个新对象:
run=client.add(new_run)result=client.add(result)section=client.add(section)
返回的对象现在将具有附加信息集。尚未指定的属性将设置为无:
fromdatetimeimportdatetimeasdtimportrandomimporttrawclient=traw.Client(username='username',password='passwrod',url='url)project=client.project(15)# Get Project with Project ID of 15new_run=client.run()new_run.name="My new run name"new_run.description="My new run description"new_run.include_all=Truenew_run.project=projectrun=client.add(new_run)# Run is added to TestRailfortestinclient.tests(run):# Get all tests for runbegin=dt.now()# Do actual testing here, but lets pick a random statusstatus_str=random.choice(['passed','failed','retest'])elapsed=dt.now()-beginresult=client.result()result.test=testresult.status=client.status(status_str)result.comment="Setting {0} to {1}".format(test.title,result.status.label)result.elapsed=elapsed# Add the result to TestRailclient.add(result)# Everything complete, close the runclient.close(run)# Fín
0支持更新的对象(运行、套件、里程碑等)可以在本地更新,然后更新可以发送到testrail:
fromdatetimeimportdatetimeasdtimportrandomimporttrawclient=traw.Client(username='username',password='passwrod',url='url)project=client.project(15)# Get Project with Project ID of 15new_run=client.run()new_run.name="My new run name"new_run.description="My new run description"new_run.include_all=Truenew_run.project=projectrun=client.add(new_run)# Run is added to TestRailfortestinclient.tests(run):# Get all tests for runbegin=dt.now()# Do actual testing here, but lets pick a random statusstatus_str=random.choice(['passed','failed','retest'])elapsed=dt.now()-beginresult=client.result()result.test=testresult.status=client.status(status_str)result.comment="Setting {0} to {1}".format(test.title,result.status.label)result.elapsed=elapsed# Add the result to TestRailclient.add(result)# Everything complete, close the runclient.close(run)# Fín
1可以通过Traw客户端关闭的对象(运行、计划):
fromdatetimeimportdatetimeasdtimportrandomimporttrawclient=traw.Client(username='username',password='passwrod',url='url)project=client.project(15)# Get Project with Project ID of 15new_run=client.run()new_run.name="My new run name"new_run.description="My new run description"new_run.include_all=Truenew_run.project=projectrun=client.add(new_run)# Run is added to TestRailfortestinclient.tests(run):# Get all tests for runbegin=dt.now()# Do actual testing here, but lets pick a random statusstatus_str=random.choice(['passed','failed','retest'])elapsed=dt.now()-beginresult=client.result()result.test=testresult.status=client.status(status_str)result.comment="Setting {0} to {1}".format(test.title,result.status.label)result.elapsed=elapsed# Add the result to TestRailclient.add(result)# Everything complete, close the runclient.close(run)# Fín
2可以通过traw客户端删除的对象(运行、计划、案例等)。请注意,调用 client.delete() 后不会返回任何对象。还要注意一些事情(跑步,计划)可以关闭或删除,但不能同时关闭和删除,而其他内容(项目)可以在关闭后删除(假设您的用户具有管理员权限):
fromdatetimeimportdatetimeasdtimportrandomimporttrawclient=traw.Client(username='username',password='passwrod',url='url)project=client.project(15)# Get Project with Project ID of 15new_run=client.run()new_run.name="My new run name"new_run.description="My new run description"new_run.include_all=Truenew_run.project=projectrun=client.add(new_run)# Run is added to TestRailfortestinclient.tests(run):# Get all tests for runbegin=dt.now()# Do actual testing here, but lets pick a random statusstatus_str=random.choice(['passed','failed','retest'])elapsed=dt.now()-beginresult=client.result()result.test=testresult.status=client.status(status_str)result.comment="Setting {0} to {1}".format(test.title,result.status.label)result.elapsed=elapsed# Add the result to TestRailclient.add(result)# Everything complete, close the runclient.close(run)# Fín
3