终止 subprocess.call( 'python script") 并继续下一个测试脚本查询
teminate subprocess.call( 'python script") and continure with next test script query
我写了这个演示脚本来问我关于 subprocess.call()
的问题。
我正在尝试 运行 python 一个接一个地测试脚本。但是在这种情况下,当其中一个测试由于无效的测试条件而中止时,我想终止 subprocess.call()
。并继续下一个测试脚本。我已阅读其他查询,但找不到足够的解释。感谢在此问题上的任何建议或帮助。下面是演示文件。
文件 1:listscripts.py
-> 此文件列出了文件夹中的所有测试,并使用 subprocess.call()
运行s 它们
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.py
、testscript2.py
和 testscript3.py
-> 在 C:/Demo/question/scripts
中放置了一些打印语句
testscript1.py
和 testscript3.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)
您可以 运行 您的脚本(在不同的可能性中)如下:
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)
我写了这个演示脚本来问我关于 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.py
、testscript2.py
和 testscript3.py
-> 在 C:/Demo/question/scripts
testscript1.py
和 testscript3.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)
您可以 运行 您的脚本(在不同的可能性中)如下:
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)