xsmscb 发表于 2018-8-11 09:03:56

python 测试工具

# -*- coding: utf-8 -*-  

  
import mechanize
  
import time
  
import traceback
  
import logging
  

  
class BaseTransaction(object):
  
    _TEST_CASE_PREFIX = "test_"
  

  
    def __init__(self):
  
      self._init()
  

  
      self.custom_timers = {}
  

  
      self.browser = mechanize.Browser()
  
      self.browser.set_handle_robots(False)
  
      self.browser.set_handle_redirect(True)
  
      self.browser.set_handle_referer(True)
  

  
    def _init(self):
  
      self.funcs = []
  
      funcs_ = dir(self)
  
      for func_ in funcs_:
  
            if func_.startswith(self._TEST_CASE_PREFIX):
  
                self.funcs.append(func_)
  

  
    def run(self):
  
    """"所有继承BaseTransaction的类,只需要在以test_开头的方法里实现测试case即可,运行时多个case都可以得到测试"""
  
      try:
  
            for func in self.funcs:
  
                start_timer = time.time()
  
                getattr(self, func)()# run test
  
                latency = time.time() - start_timer
  

  
                self.custom_timers['%s' % func] = latency
  
      except Exception, e:
  
            logging.error(traceback.format_exc())
  
            raise e
页: [1]
查看完整版本: python 测试工具