python 调用多个文件(Unit Test),返回结果

代码

#coding=utf-8
import os

def run_cmd(ut):
    #print(ut)
    val = os.system(ut)
    print (val)


#"msg":"OK","data"
def call_cmd(ut):
    str = os.popen(ut).read()
    a = str.split("\n")
    ret = ''
    for b in a:
        print (b)
        end = b.find('"msg":"OK"')
        #print('end=',end)
        if end > 0 and ret != 'OK':
            if end > 0:
                ret = 'OK'
            else:
                start = b.find('"msg":"')
                ret = b[start:start+20]

    return ret
            
ut_list = []
ut_list.append("python ut1.py")
ut_list.append("python ut2.py")
ut_list.append("python ut3.py")

ut_result = []
for x in ut_list:
    #print(x)
    ret = call_cmd(x)
    ut_result.append(ret)

print('===========results===========')
index = 0
for x in ut_result:
    print(ut_list[index],'\t', x)
    index += 1

结果

===========results===========
python ut1.py      OK
python ut2.py      OK
python ut3.py      Error

version 2  多线程版本

#coding=utf-8
import os
import threading
import time

#"msg":"OK","data"
def call_cmd(index, ut):
    str = os.popen(ut).read()
    a = str.split("\n")
    ret = ''
    for b in a:
        #print (b)
        end = b.find('"msg":"OK"')
        #print('end=',end)
        if end > 0 and ret != 'OK':
            if end > 0:
                ret = 'OK'
            else:
                start = b.find('"msg":"')
                ret = b[start:start+20]

    ut_result[index] = ret
    return ret
   
start = time.time()

ut_list = []
ut_list.append("python ut1.py")
ut_list.append("python ut2.py")
ut_list.append("python ut3.py")


ut_result = []
threads = []
index = 0
for x in ut_list:
    print(x, 'is working...')
    ret = 'Error'
    ut_result.append(ret)
    t1 = threading.Thread(target=call_cmd, args=(index, x, ))
    t1.start()
    threads.append(t1)
    index += 1

for t in threads:
   t.join()
print('===========results===========')
index = 0
for x in ut_result:
    print(ut_list[index],'\t', x)
    index += 1

end = time.time()
cost_time = int((end - start) * 1000)
print("--- ut all cost", cost_time, "milliseconds ---")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值