【Python】实现将testlink上的用例指定格式保存至Excel,用于修改上传
生活随笔
收集整理的這篇文章主要介紹了
【Python】实现将testlink上的用例指定格式保存至Excel,用于修改上传
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
背景
前一篇博客記錄的可以上傳用例到testlink指定用例集的腳本,內(nèi)部分享給了之后,同事希望能將testlink上原有的用例下載下來(lái),用于下次修改上傳,所有有了本文腳本。
具體實(shí)現(xiàn)
獲取用例信息
def download_testcase():"""獲取(下載)testlink上面指定用例集的數(shù)據(jù):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)) 進(jìn)行數(shù)據(jù)轉(zhuǎn)換
def format_execution_type(source_data):"""轉(zhuǎn)換執(zhí)行方式:param source_data::return:"""switcher = {'2': "自動(dòng)化",'1': "手工"}return switcher.get(source_data, "Param not defind")def format_importance(source_data):"""轉(zhuǎn)換優(yōu)先級(jí):param source_data::return:"""switcher = {'1': "低",'2': "中",'3': "高"}return switcher.get(source_data, "Param not defind")def format_auth(source_data):"""轉(zhuǎn)換作者:可以通過(guò)testlink的user表查詢(xún)到對(duì)應(yīng)id->name對(duì):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) # 讀取Excelnew_book = copy.copy(book) # 復(fù)制讀取的Excelsheet = new_book.get_sheet(0) # 取第一個(gè)sheet頁(yè)line_num = 1for 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 += 1report_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 suitesexcept testlink.testlinkerrors.TLResponseError as e:# traceback.print_exc()logger.warning(str(e).split('\n')[1])logger.warning(str(e).split('\n')[0])return 使用方法
環(huán)境依賴(lài)
| 環(huán)境依賴(lài) | 安裝方法 |
|---|---|
| Python3 | 略 |
| xlrd庫(kù) | pip install xlrd |
| testlink庫(kù) | pip install TestLink-API-Python-client |
| xlutils | pip install xlutils |
具體方法
- 將上述的代碼保存到一個(gè)文件中,底部添加下列代碼進(jìn)行調(diào)用
if __name__ == "__main__":url = "http://localhost/lib/api/xmlrpc/v1/xmlrpc.php"key = "6c3fe0796142db21" # 這個(gè)key是錯(cuò)誤的keytlc = testlink.TestlinkAPIClient(url, key)father_id = "274539" # 想要下載的用例集的ID,可通過(guò)在testlink界面選取用例集,然后點(diǎn)擊右鍵獲取download_testcase() - 目錄結(jié)構(gòu)參考,與上一篇文章的腳本放在了一起
D:\Project\UPLOAD_DATA2TESTLINK
│ download_testcase.py
│ logger_better.py
│ upload_excel_data.py
│
└─testCasedown_load_template.xls
- 在上一步的文件同一目錄下創(chuàng)建一個(gè)testCase文件夾,創(chuàng)建一個(gè)xls文件,文件格式如下:
轉(zhuǎn)載于:https://www.cnblogs.com/Detector/p/9030650.html
總結(jié)
以上是生活随笔為你收集整理的【Python】实现将testlink上的用例指定格式保存至Excel,用于修改上传的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 天涯牛人多。。进来看看求告知女主裙子是什
- 下一篇: 工资