定义测试执行类Tester.py
python
# 执行试验类
import time
class Tester(object):
def __init__(self, jobName, stepList):
self.jobName = jobName
self.stepList = stepList
# 开始执行
def start(self, flag):
print(f'{self.jobName} start')
self.status = "start"
stepIndex = 0
while not flag.isSet() and self.status != "stop":
if self.status == "start":
step = self.stepList[stepIndex]
print(f"{self.jobName} run step: {step}")
stepIndex += 1
if stepIndex >= len(self.stepList):
self.status = "stop"
time.sleep(1)
def stop(self):
self.status = "stop"
self.stepList = []
self.jobName = ""
def pause(self):
self.status = "pause"
def reStart(self):
self.status = "start"
执行主类ACClient.py
python
from obj.Tester import Tester
import threading
import time
tester = Tester("AJob", ['a','b','c','d','e','f','g','h'])
testerB = Tester("BJob", ['a','b','c','d','e','f','g','h'])
# 创建线程,目标是my_instance的my_method方法,并传入参数
flag = threading.Event()
thread = threading.Thread(target=tester.start, args=(flag, ))
# 启动线程
thread.start()
# 创建线程,目标是my_instance的my_method方法,并传入参数
flagB = threading.Event()
threadB = threading.Thread(target=testerB.start, args=(flagB, ))
# 启动线程
threadB.start()
print("====================")
# 测试结束
# time.sleep(1)
# flag.set();
# 测试暂停
time.sleep(2)
tester.pause()
time.sleep(10)
tester.reStart() # 10秒后继续
print("====================")
有点懒得解释,写完之后,执行主类ACClient.py看效果,可以打开测试结束看效果,反正我不知道什么原理。