利用python的getattr自省方法,执行excle中定义的方法========
from unit.operExcle import operExcle
from unit.base import ActtionMethod
from event.asertion import Assert
from event.log_event import *
from event.log_event import log
class TestCase:
oper = operExcle()
lines = oper.get_lines()
action_method = ActtionMethod()
True_sum = 0
false_sum=0
def excle_value(self):
try:
for i in range(2, self.lines+1):
#是否执行
is_run = self.oper.get_value(i,3)
if is_run=="yes":
#method
method = self.oper.get_value(i,4)
#获取定位元素
locator = self.oper.get_value(i,5)
#用户输入内容
content = self.oper.get_value(i,6)
#预期结果
per = self.oper.get_value(i,7)
log.info(f"执行【{method}】操作")
result = self.run_action(method,content,locator)
self.oper.write_data(i,8,result)
ret=Assert(i,9,per,result)
if ret:
self.oper.write_data(i,10,1)
else:
self.oper.write_data(i,10,0)
self.run_action("screenshot",f"预期值【{per}】和实际值【{result}】不一致")
except Exception as e:
raise e
finally:
self.total()
def run_action(self,method,*args):
action_function=getattr(self.action_method,method)
result=action_function(*args)
return result
def total(self):
for i in range(2, self.lines):
if self.oper.get_value(i, 10) == 0:
self.false_sum += 1
elif self.oper.get_value(i, 10) == 1:
self.True_sum += 1
log.info(f"成功案例数:{self.True_sum},失败案例数:{self.false_sum}")
if __name__=="__main__":
testcase = TestCase()
print(testcase.excle_value())