2015-10-30 20 views
2

我已經編寫了這個演示腳本來問我關於subprocess.call()的問題 我試圖一個接一個地運行python測試腳本。當其中一個由於無效測試條件而導致的測試中止時,我想終止subprocess.call(),然後轉到下一個測試腳本。我已經通讀了其他查詢,但沒有找到足夠的解釋。請在此方面感謝任何建議或幫助。在演示文件teminate subprocess.call('python script「)並且繼續執行下一個測試腳本查詢

文件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一些打印statments:

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) 
+0

我可能沒有正確地說出口。如果你想一個接一個地運行這些腳本,爲什麼不在失敗時退出子進程,而是使用這樣的全局變量? – ferdy

回答

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) 
+1

Thankyou。現在工作。 – smi

+0

@smi如果我的回答對您有幫助,如果您將其標記爲解決方案將會很好。 – ferdy

+0

:)。如何將其標記爲解決方案。仍然是使用堆棧溢出功能的新功能。 – smi

相關問題