from ast import Return
import os
import sys
import platform
import shutil
from unittest import suite
def removedir(rootdir):
for root,dirs,files in os.walk(rootdir, topdown=False):
for name in files:
os.remove(os.path.join(rootdir,name))
for name in dirs:
os.rmdir(os.path.join(rootdir,name))
os.rmdir(rootdir)
def new_report(bakdir, str):
files = os.listdir(bakdir)
lists = []
for f in files:
if "latest" in f:
continue
lists.append(f)
lists.sort(key=lambda fn:os.path.getmtime(bakdir + "/" + fn))
file_new = os.path.join(bakdir,lists[-1])
print("latest file:", file_new)
return file_new
if __name__ == '__main__':
suitename = sys.argv[1]
mustpassfile = sys.argv[2]
latestpath = new_report("reports", "")
tmpfile = "tmptestsuite.xml"
putfile = "/result/"+suitename+".xml"
tasklogfile = suitename+".qpa"
putdir = latestpath+putfile
tasklogpath = tasklogfile
mustpasspath = "testcases/opengldata/mustpass/"+mustpassfile
curtime = ""
if os.path.exists(tasklogpath):
size = os.path.getsize(tasklogpath)
if size == 0:
os.remove(tasklogpath)
else:
raise Exception('qpa文件不存在')
if sys.platform.startswith('linux'):
print('os is Linux')
elif sys.platform.startswith('win'):
print('os is Windows')
elif sys.platform.startswith('darwin'):
print('os is macOS')
else:
print('unknown os', sys.platform)
timelist = latestpath.split("/")
if len(timelist) > 1:
curtime = timelist[1].replace("\n", "")
else:
timelist = latestpath.split("\\")
curtime = timelist[1].replace("\n", "")
testcaselist = []
testcaselist1 = []
mustpasslist = []
total = 0
passcnt = 0
failcnt = 0
unavailablecnt = 0
with open(mustpasspath) as mustpassbuf:
for mustpassline in mustpassbuf:
mustpasslist.append(mustpassline.strip().strip("\r").strip("\n"))
print("mustfile line:", len(mustpasslist))
print("tasklogpath :", tasklogpath)
with open(tasklogpath, errors = 'ignore') as tasklogbuf:
casename = ""
testbegin = 0
testresult = 0
for tasklogline in tasklogbuf:
if "#beginTestCaseResult " in tasklogline:
freslist = tasklogline.split("#beginTestCaseResult ")
casename = freslist[1]
testbegin = 1
continue
elif "#endTestCaseResult" in tasklogline:
if testbegin == 1 and testresult == 0:
total += 1
failcnt += 1
testcaselist.append(casename+"@@@false@@@run")
testcaselist1.append(casename.strip("\n"))
testbegin = 0
testresult = 0
continue
elif "<Result StatusCode=\"Pass\">" in tasklogline:
total +=1
passcnt += 1
testresult = 1
testcaselist.append(casename+"@@@true@@@run")
testcaselist1.append(casename.strip("\n"))
continue
elif "<Result StatusCode=\"NotSupported\">" in tasklogline:
total +=1
passcnt += 1
testresult = 1
testcaselist.append(casename+"@@@true@@@run")
testcaselist1.append(casename.strip("\n"))
continue
elif "<Result StatusCode=\"Fail\">" in tasklogline:
total +=1
failcnt += 1
testresult = 1
testcaselist.append(casename+"@@@false@@@run")
testcaselist1.append(casename.strip("\n"))
i = 0
j = 0
notfindlist = []
while i < len(mustpasslist):
isfind = False
if mustpasslist[i] in testcaselist1:
isfind = True
j += 1
if isfind == False:
notfindlist.append(f"{mustpasslist[i]}@@@false@@@run")
failcnt += 1
total += 1
i += 1
testcaselist += notfindlist
with open(tmpfile, mode='w+') as f:
f.write("<?xml version='1.0' encoding='UTF-8'?>\n")
f.write("<testsuites name=\"{}\" timestamp=\"{}\" time=\"0.0\" errors=\"0\" disabled=\"0\" failures=\"{}\" tests=\"{}\" ignored=\"0\" unavailable=\"{}\" productinfo=\"{}\">\n".format(suitename, curtime, failcnt, total, unavailablecnt, "{}"))
f.write(" <testsuite name=\"{}\" time=\"0.0\" errors=\"0\" disabled=\"0\" failures=\"{}\" ignored=\"0\" tests=\"{}\" message=\"\">\n".format(suitename, failcnt, total))
for casename in testcaselist:
casename = casename.replace("\n","")
loccasename = casename.split("@@@")
recasename = loccasename[0]
caseresult = loccasename[1]
casestate = loccasename[2]
f.write(" <testcase name=\"{}\" status=\"{}\" time=\"0.0\" classname=\"{}\" result=\"{}\" level=\"1\" message=\"\" />\n".format(recasename, casestate, suitename, caseresult))
f.write(" </testsuite>\n")
f.write("</testsuites>\n")
if os.path.exists(latestpath+"/result") == False:
os.mkdir(latestpath+"/result")
print("putdir :", putdir)
shutil.copy2(tmpfile,putdir)