teminate subprocess.call(' python脚本")并继续下一个测试脚本查询

时间:2015-10-30 20:46:30

标签: python subprocess

我写了这个演示脚本,在subprocess.call()上提问。 我试图一个接一个地运行python测试脚本。但是在这种情况下,当其中一个测试因测试条件无效而中止时,我想终止subprocess.call()。然后转到下一个测试脚本。我已阅读其他查询,但无法找到足够的解释。感谢任何关于此事的建议或帮助。以下是演示文件。

文件1:listscripts.py - >此文件列出了文件夹中的所有测试,并使用subprocess.call()

运行它们
import os
from subprocess import *
import sys,os,time

Lib_Path = "C:\\Demo\\question"
sys.path.append(Lib_Path)
import globalsvar  # has a global variable

list = os.listdir("C:\\Demo\\question\\scripts")  # this has 3 example basic script

for testscripts in list:

    aborttest = globalsvar.aborttestcall  # when it encounters invalid condition from testscript1thru3 call() should terminate and go to next test

    while not aborttest:

           Desttestresultpath = os.path.join('C:/Demo/question/scripts',pyscripts)

           call(["python",Desttestresultpath]) #calls individual scripts

           aborttest = True
exit(1)

文件2:globalsvar.py ( aborttestcall = False )

testscript1.pytestscript2.pytestscript3.py - >在C:/Demo/question/scripts

中放置了一些打印件

testscript1.pytestscript3.py

import sys,os,time

Lib_Path = "C:\\Demo\\question"

sys.path.append(Lib_Path)

import globalsvar

print "Start of Test\n"
print "testing stdout prints --1"

time.sleep(1)


globalsvar.aborttestcall = False


print "testing stdout prints --2"

time.sleep(1)

print "testing stdout prints --3"

time.sleep(1)

testscript2.py

import sys,os,time

Lib_Path = "C:\\Demo\\question"

sys.path.append(Lib_Path)

import globalsvar

print "Start of Test\n"
print "testing stdout prints --1"

time.sleep(1)


globalsvar.aborttestcall = True


print "testing stdout prints --2"

time.sleep(1)

print "testing stdout prints --3"

time.sleep(1)

1 个答案:

答案 0 :(得分:0)

您可以像这样运行脚本(在不同的可能性中):

import subprocess
import os

for file_item in os.listdir('scripts'):
   script = os.sep.join(['scripts', file_item])
   return_value = subprocess.call(['python', script])
   print "OUTPUT: " + str(return_value)

虽然您的内部脚本可以使用您可以在调用过程中评估的退出代码退出其进程。

import time
import sys

print "Doing subprocess testing stuff"
time.sleep(2)
print "Doing more subprocess testing stuff"

# simulate error
time.sleep(2)
print "Error, jump out of the process"
sys.exit(1)

# finish
time.sleep(2)
print "done"

# this can be left out since it is called implicitely
# on successful step out of a process
# sys.exit(0)