argdata = open(argpath).read() if os.path.exists(argpath) else ''
# местоположение файлов для сохранения stdout и stderr,
# очистить предыдущие результаты outfile = testname.replace(‘.py’, ‘.out’) outpath = os.path.join(testdir, ‘Outputs’, outfile) outpathbad = outpath + ‘.bad’
if os.path.exists(outpathbad): os.remove(outpathbad)
errfile = testname.replace(‘.py’, ‘.err’) errpath = os.path.join(testdir, ‘Errors’, errfile) if os.path.exists(errpath): os.remove(errpath)
# запустить тестируемый сценарий, перенаправив потоки ввода-вывода pypath = sys.executable
command = ‘%s %s %s’ % (pypath, testpath, argdata) trace(command, indata)
process = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
process.stdin.write(indata)
process.stdin.close()
outdata = process.stdout.read()
errdata = process.stderr.read() # при работе с двоичными файлами
exitstatus = process.wait() # данные имеют тип bytes
trace(outdata, errdata, exitstatus)
# проанализировать результаты if exitstatus != 0:
print(‘ERROR status:’, testname, exitstatus) # код заверш.
if errdata: # и/или stderr
print(‘ERROR stream:’, testname, errpath) # сохр. текст ошибки
open(errpath, ‘wb’).write(errdata)
if exitstatus or errdata: # оба признака ошибки
numfail += 1 # можно получить код завершения + код ошибки
open(outpathbad, ‘wb’).write(outdata) # сохранить вывод
elif not os.path.exists(outpath) or forcegen:
print(‘generating:’, outpath) # создать файл, если
open(outpath, ‘wb’).write(outdata) # необходимо
else:
priorout = open(outpath, ‘rb’).read() # или сравнить с прежними
# результатами
if priorout == outdata:
print(‘passed:’, testname) else: