利用python的getattr自省方法,執(zhí)行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): #是否執(zhí)行 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) #用戶輸入內(nèi)容 content = self.oper.get_value(i,6) #預(yù)期結(jié)果 per = self.oper.get_value(i,7) log.info(f"執(zhí)行【{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"預(yù)期值【{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"成功案例數(shù):{self.True_sum},失敗案例數(shù):{self.false_sum}") if __name__=="__main__": testcase = TestCase() print(testcase.excle_value()) |
|