经过验证,运行后可以达到预期的结果,只需要手动输入自己的url和key,并输入正确的识别名,和suite的id就可以正常运行,匹配不同的id,批量上传到附件。
from __future__ import print_function
import testlink
import logging
import re
import html
from ApiClient import ApiClient
import sys
import collections
import testlink
import os
import os
import testlink
import xlrd
manual = 1
automation = 2
url = "########################testlink地址"
key = '##############################我的key'
tlc = testlink.TestlinkAPIClient(url, key)
import easygui as g
str1 = g.diropenbox('open file', 'C:/User/Administrator/Desktop/__pycache__')
print(str1)
path1=(str1+"\\11111")
path2=(str1+"\\22222")
os.mkdir(path1)
os.mkdir(path2)
import easygui as a
ret=a.enterbox(msg='请输入suite对应ID',title='脚本001',)
print(ret)
import os
import os
path = str1
z=[]
for filename in os.listdir(path):
x=os.path.join(path, filename)
y=x
# print(y)
z.append(y)
# print(z)
import zipfile
from os import path,walk,chdir
def zipFiles(pathsList):
for i,r in enumerate(pathsList,0):
if path.isfile(r):
fDir,fName=path.split(r)
fPre,fSuf=path.splitext(fName)
chdir(fDir)
zipPath=fDir+'\\'+fPre+'.zip'
zipObj = zipfile.ZipFile(zipPath, 'w', zipfile.ZIP_DEFLATED)
zipObj.write(fName)
zipObj.close()
else:
chdir(r)
fDir,fName=path.split(r)
fPre,fSuf=path.splitext(fName)
zipPath=fDir+'\\11111'+'\\'+fName+'.7z'
zipObj = zipfile.ZipFile(zipPath, 'w', zipfile.ZIP_DEFLATED)
for p, dirs, files in walk(r):
for name in files:
zPath=path.join(p, name).replace(r+'\\','')
zipObj.write(zPath)
for name in dirs:
zPath=path.join(p, name).replace(r+'\\','')
zipObj.write(zPath)
zipObj.close()
print('第{}个文件夹压缩完毕,储存路径{}'.format(i,zipPath))
pathsList=z
zipFiles(pathsList)
zz=[]
import shutil, os
for filename in os.listdir(path1):
xx=os.path.join(path1, filename)
yy=xx
print(os.path.join(path1, filename))
zz.append(yy)
# print(zz)
for zz in zz:
str1="ECUTEST--";
str2=zz
str2.find(str1)
# print(str2.find(str1))
if str2.find(str1)==-1:
print("111")
else:
print("222"),shutil.copy(zz, path2)
cases = tlc.getTestCasesForTestSuite(testsuiteid=ret,deep=2,details="Test case for Transmission")
for cases in cases:
print(cases['id'], cases['external_id'])
print(cases)
zzz=[]
import shutil, os
# path = r'E:\\TestReports\22222'
for filename in os.listdir(path2):
xxx=os.path.join(path2, filename)
yyy=xxx
# print(os.path.join(path, filename))
zzz.append(yyy)
# #
# #
cases = tlc.getTestCasesForTestSuite(testsuiteid=ret,deep=2,details="Test case for Transmission")
# for zzz in zzz:
# print(zzz)
for zzz in zzz:
cases = tlc.getTestCasesForTestSuite(testsuiteid=ret, deep=2, details="Test case for Transmission")
for cases in cases:
zzz.split("_")[0]
zzz.split("_")[0].split("\\")[4]
str3=cases["external_id"]
print(cases["external_id"])
str4=zzz
str5=zzz.split("_")[0].split("\\")[4]
if str3!=str5:
print("进行中")
else:
print("上传中")
now_id=cases['id']
print(now_id)
file_In = str4
(filepath, tempfilename) = os.path.split(file_In)
print(tempfilename)
NEWATTACHMENT_7Z=os.path.join(path2, tempfilename)
a_file=open(NEWATTACHMENT_7Z, mode='rb+')
try:
newAttachment = tlc.uploadTestCaseAttachment(NEWATTACHMENT_7Z,testcaseid=now_id,version=1
, description='1')
print("uploadTestSuiteAttachment", newAttachment)
except:
continue