给nose写一个类似Robot Framework的Wait Until Keyword Succeeds

前言

由于产品是异步请求,往往一个request下去,只是将某些信息写入到了KVStore中,之后由daemon读取KVStore进行判断是否有发生变化,如果有变化,daemon才采取行动。这里在写自动化校验设置是否apply下去,以及apply下去后是否有生效,就不能靠time.sleep来做了,主要问题是:

  • 不确定要等待多久,等待久了,感觉是浪费时间,尤其用例很多的情况下,严重影响了用例的整体执行时间;等待短了,又没有成功的apply下去,或者apply下去了但还没有具体生效

鉴于此,就需要有一个类似于Robot Framework的Wait Until Keyword Succeeds 功能.

实践

python

#!/usr/bin/env python
# -*- coding:UTF-8 -*-


from __future__ import unicode_literals

import time

from common.config import GetConfig as config

retry_interval = config.retry_interval
retry_timeout = config.retry_timeout


def assert_check(func):
    """
    :param func, string, a function name
    """
    @wraps(func)
    def inner(param, *args, **kwargs):
        """
        :param param, string, a parameter
        """
        for i in xrange(int(retry_timeout)):
            try:
                func(param, *args, **kwargs)
                break
            except Exception as ex:
                logging.warn("[WARN]  Not match, %s time(s) to retry, "
                             "exception is : (%s) : (%s)", (i + 1), ex.__str__, unicode(ex))
                logging.warn("[WARN]  In file (%s) of function (%s), "
                             "at line (%s)", func.func_code.co_filename,
                             func.func_name,
                             func.func_code.co_firstlineno)
                time.sleep(int(retry_interval))
                continue
        else:
            logging.info("[ERROR]  Retry timeout or AssertionError")
            # raise AssertionError('[RetryTimeOut] Failure expected, retry timeout')
            # Deliberately doing this, this will mark the use case status as failed,
            # otherwise it will affect the normal output of the html report (the entire html content is empty)
            eq_(1, 2, "[RetryTimeOut] Failure expected, retry timeout")

    return inner

这里是一个示例的片段,通过装饰器,在调用check函数之前,引用这个装饰器,从而达到间隔一定时间(retry_timeout)、执行一定次数(retry_interval)来实现类似RFWait Until Keyword Succeeds 的功能

对应测试用例基类的检查操作示例如下:

python

    @assert_check
    def check_snapshot(self, gateway_group, target_id, iscsi_id, snap_name, op_type):
        """
        Check snapshot created or delete result
        :param gateway_group, string, a gateway group name
        :param target_id, string, a target name
        :param iscsi_id, string, a volume name
        :param snap_name, string, a snapshot name for a iSCSI volume
        :param op_type, string, del or add
        """

        if op_type == 'add':
            logging.info("[Check]    Check snapshot created result")
        elif op_type == 'del':
            logging.info("[Check]    Check snapshot deleted result")

        info = get_volume_info(gateway_group, target_id, iscsi_id, translate_pool_id=True)
        pool = info['pool']
        image_name = info['rbd_img']

        rbd_res = do_cmd("rbd -p {} snap ls {}".format(pool, image_name), 30, True).strip()
        if op_type == 'add':
            assert_in(snap_name, rbd_res, "[ERROR]  Not found : ({}) in "
                                          "rbd_res : ({})".format(snap_name, rbd_res))
            logging.info("[Success]  Create snapshot : (%s) success", snap_name)
        elif op_type == 'del':
            assert_not_in(snap_name, rbd_res, "[ERROR]  Still found : ({}) in "
                                              "rbd_res : ({})".format(snap_name, rbd_res))
            logging.info("[Success]  Delete snapshot : (%s) success", snap_name)


来源: Transcendent
文章作者: Gavin Wang
文章链接: 给nose写一个类似Robot Framework的Wait Until Keyword Succeeds | Transcendent
本文章著作权归作者所有,任何形式的转载都请注明出处。

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作为AI语言模型,我可以为您提供Nose.js开发个人系统的一般步骤和建议: 1. 确定系统需求:首先,您需要确定您的个人系统需要满足哪些需求。例如,您可能需要一个日历功能、任务管理功能、笔记功能、文件管理功能等等。确保您对系统的整体目标和功能有清晰的认识。 2. 设计系统架构:在确定需求之后,您需要为系统设计一个合适的架构。这包括选择合适的数据库、服务器和编程语言等。Nose.js 是一个非常流行的服务器端 JavaScript 框架,它可以帮助您构建高效、可扩展的应用程序。 3. 开发前端:一旦您确定了系统的需求和架构,您就可以开始开发前端界面。您可以使用HTML、CSS、JavaScript等技术来构建用户界面,并使用Nose.js作为后端框架。 4. 开发后端:在开发前端之后,您需要开发后端服务器以处理您的数据和逻辑。您可以使用Nose.js提供的路由器、中间件和控制器来开发后端。 5. 数据库设计:您需要为您的系统设计数据库架构。确定您需要存储哪些数据,并选择合适的数据库类型和表结构。 6. 测试和部署:在完成开发和数据库设计之后,您需要对系统进行测试和部署。确保系统能够在各种条件下正常运行,并且能够满足您的需求。 7. 维护和更新:一旦您的系统上线,您需要维护并定期更新它。确保系统能够保持高效、稳定和安全。 总之,这是一个大项目,需要您具备一定的技术能力和经验。如果您没有足够的技术能力,建议您寻找专业的开发团队或与其他开发者合作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值