背景
前一篇博客記錄的可以上傳用例到testlink指定用例集的腳本,內部分享給了之后,同事希望能將testlink上原有的用例下載下來,用於下次修改上傳,所有有了本文腳本。
具體實現
獲取用例信息
def download_testcase():
"""
獲取(下載)testlink上面指定用例集的數據
:return:
"""
datas = []
for data in tlc.getTestCasesForTestSuite(father_id, True, 'full'):
actions = []
expected_results = []
name = data["name"]
summary = data["summary"]
preconditions = data["preconditions"]
importance = data["importance"]
execution_type = data["execution_type"]
author = data["author_id"]
# print(json.dumps(data, indent=4))
for i in range(len(data["steps"])):
actions.append(data["steps"][i]["actions"])
expected_results.append(data["steps"][i]["expected_results"])
datas.append((name, preconditions, '\n'.join(actions), '\n'.join(expected_results), format_execution_type(execution_type), format_auth(author), format_importance(importance), summary))
進行數據轉換
def format_execution_type(source_data):
"""
轉換執行方式
:param source_data:
:return:
"""
switcher = {
'2': "自動化",
'1': "手工"
}
return switcher.get(source_data, "Param not defind")
def format_importance(source_data):
"""
轉換優先級
:param source_data:
:return:
"""
switcher = {
'1': "低",
'2': "中",
'3': "高"
}
return switcher.get(source_data, "Param not defind")
def format_auth(source_data):
"""
轉換作者:可以通過testlink的user表查詢到對應id->name對
:param source_data:
:return:
"""
switcher = {
'100': "tester_name",
}
return switcher.get(source_data, "Param not defind")
保存至Excel
def save_suits(file_path, datas):
"""
保存用例
:param file_path: 保存路徑
:param datas:
:return:
"""
book = xlrd.open_workbook(file_path, formatting_info=True) # 讀取Excel
new_book = copy.copy(book) # 復制讀取的Excel
sheet = new_book.get_sheet(0) # 取第一個sheet頁
line_num = 1
for i in range(0, len(datas)):
name, preconditions, actions, expected_results, execution_type, author, importance, summary = datas[i]
sheet.write(line_num, 0, u'%s' % name)
sheet.write(line_num, 1, u'%s' % preconditions)
sheet.write(line_num, 2, u'%s' % actions)
sheet.write(line_num, 3, u'%s' % expected_results)
sheet.write(line_num, 4, u'%s' % execution_type)
sheet.write(line_num, 5, u'%s' % author)
sheet.write(line_num, 6, u'%s' % importance)
sheet.write(line_num, 7, u'%s' % summary)
line_num += 1
report_path = os.path.abspath(os.path.join('download'))
if not os.path.exists(report_path):
os.makedirs(report_path)
suits_name = get_suites(father_id)["name"]
new_book.save(os.path.abspath(os.path.join(report_path, '用例集_{}@{}.xlsx'.format(suits_name, time.strftime('%Y.%m.%d@%H%M%S')))))
def get_suites(suite_id):
"""
獲取用例集信息
:return:
"""
try:
suites = tlc.getTestSuiteByID(suite_id)
return suites
except testlink.testlinkerrors.TLResponseError as e:
# traceback.print_exc()
logger.warning(str(e).split('\n')[1])
logger.warning(str(e).split('\n')[0])
return
使用方法
環境依賴
環境依賴 | 安裝方法 |
---|---|
Python3 | 略 |
xlrd庫 | pip install xlrd |
testlink庫 | pip install TestLink-API-Python-client |
xlutils | pip install xlutils |
具體方法
- 將上述的代碼保存到一個文件中,底部添加下列代碼進行調用
if __name__ == "__main__":
url = "http://localhost/lib/api/xmlrpc/v1/xmlrpc.php"
key = "6c3fe0796142db21" # 這個key是錯誤的key
tlc = testlink.TestlinkAPIClient(url, key)
father_id = "274539" # 想要下載的用例集的ID,可通過在testlink界面選取用例集,然后點擊右鍵獲取
download_testcase()
- 目錄結構參考,與上一篇文章的腳本放在了一起
D:\Project\UPLOAD_DATA2TESTLINK
│ download_testcase.py
│ logger_better.py
│ upload_excel_data.py
│
└─testCase
down_load_template.xls
- 在上一步的文件同一目錄下創建一個testCase文件夾,創建一個xls文件,文件格式如下: