特米纳特子流程调用('python script“)并继续进行下一个测试脚本查询

2024-10-02 08:21:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了这个演示脚本来问我关于subprocess.call()的问题。 我正在尝试一个接一个地运行python测试脚本。但是在这个场景中,当一个测试由于无效的测试条件而中止时,我想终止subprocess.call()。然后继续下一个测试脚本。我已经阅读了其他问题,但找不到充分的解释。感谢你在这件事上的任何建议或帮助。下面是演示文件。你知道吗

File1:listscripts.py->;此文件列出文件夹中的所有测试,并使用子流程调用()

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)

Tags: pathpyimporttimeosdemolibstdout
1条回答
网友
1楼 · 发布于 2024-10-02 08:21:11

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

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)

相关问题 更多 >

    热门问题