testbase.util Package

共用类模块

testbase.util.ForbidOverloadMethods(func_name_list)

生成metaclass用于指定基类禁止子类重载函数

class testbase.util.LazyInit(obj, propname, init_func)

基类:object

实现延迟初始化

使用方式示例:

class _Win32Window(object)
    def click(self):
        #......
class Control(object):
    def __init__(self, locator):
        self._locator = locator
        self._initobj = LazyInit(self, '_initobj', self._init_window)
    def _init_window(self):
        return _Win32Window(self._locator)
    def click(self):
        return self._initobj.click()

ctrl = Control("/Name=xxx)
ctrl.click()  # <-- call _init_window
ctrl.click()
class testbase.util.Singleton(name, bases, dic)

基类:type

单实例元类,用于某个类需要实现单例模式。
使用方式示例如下:

import six class MyClass(with_metaclass(Singleton, object)):

def __init__(self, *args, **kwargs):
pass
class testbase.util.ThreadGroupLocal

基类:object

使用线程组本地存储的元类

  • 当配合ThreadGroupScope使用,类似threading.local()提供的TLS变种,一个线程和其子孙线程共享一个存储

详细使用方式请参考ThreadGroupScope类

  • 当不在ThreadGroupScope中使用时,行为和threading.local()一致
class testbase.util.ThreadGroupScope(name)

基类:object

指定线程组作用域,进入这个作用域的线程,以及在其作用域内创建的线程都同属于一个线程组

使用示例如下:

def _thread_proc():
    ThreadGroupLocal().counter +=1

with ThreadGroupScope("test_group"):
    ThreadGroupLocal().counter = 0
    t = threading.Thread(target=_thread_proc)
    t.start()
    t.join()
    t = threading.Thread(target=_thread_proc)
    t.start()
    t.join()
    assert ThreadGroupLocal().counter == 2
static current_scope()

返回当前线程所在的线程组作用域,如果不存在于任务线程组作用域,则返回None

class testbase.util.Timeout(timeout=10, interval=0.5)

基类:object

TimeOut类,实现超时重试逻辑

check(func, expect)

多次检查func的返回值是否符合expect设定的期望值,如果设定时间内满足,则返回True,否则返回False

参数:
  • func -- 尝试调用的函数
  • expect -- 设定的期望值

:returns bool - 检查是否符合预期

retry(func, args, exceptions=(), resultmatcher=None, nothrow=False)

多次尝试调用函数,成功则并返回调用结果,超时则根据选项决定抛出TimeOutError异常。

参数:
  • func -- 尝试调用的函数
  • args (dict或tuple) -- func函数的参数
  • exceptions (tuple类型,tuple元素是异常类定义,如QPathError, 而不是异常实例,如QPathError()) -- 调用func时抛出这些异常,则重试。 如果是空列表(),则不捕获异常。
  • resultmatcher (函数指针类型) --
    函数指针,用于验证第1个参数func的返回值。
    默认值为None,表示不验证func的返回值,直接返回。

    其函数原型为:

    def result_match(ret): # 参数ret为func的返回值
    pass
    当result_match返回True时,直接返回,否则继续retry。

:type nothrow:bool :param nothrow:如果为True,则不抛出TimeOutError异常

返回:返回成功调用func的结果

:rtype : any

waitObjectProperty(obj, property_name, waited_value, regularMatch=False)
通过比较obj.property_name和waited_value,等待属性值出现。
如果属性值obj.property_name是字符类型则waited_value做为正则表达式进行比较。 比较成功则返回,超时则抛出TimeoutError异常。
参数:
  • obj -- 对象
  • property_name -- 要等待的obj对象的属性名
  • waited_value -- 要比较的的属性值,支持多层属性
  • regularMatch -- 参数 property_name和waited_value是否采用正则表达式的比较。 默认为不采用(False)正则,而是采用恒等比较
class testbase.util.classproperty(getter)

基类:object

类属性修饰器

testbase.util.get_thread_traceback(thread)

获取用例线程的当前的堆栈

参数:thread (Thread) -- 要获取堆栈的线程
testbase.util.getmembers(object, predicate=None)

Return all members of an object as (name, value) pairs sorted by name. Optionally, only return members that satisfy a given predicate.

testbase.util.smart_binary(s, encoding='utf8', decoding=None)

convert any text or binary to binary of specified encoding

testbase.util.smart_bytify(obj, encoding='utf-8', decoding=None)

recursively convert objects from string types to binary

testbase.util.smart_strfy(obj, decoding=None)

recursively convert objects from binary to text

testbase.util.smart_text(s, decoding=None)

convert any text or binary to text py2 text: utf-8 bytes py3 text: unicode

testbase.util.to_pretty_xml(doc, encoding='utf-8')

we need to ensure each line to be binary type