zoukankan      html  css  js  c++  java
  • python nose 自写插件支持用例带进度

    在自动化测试过程中,当用例很多且要跑很久时,就会出现这样一个问题,不知道当前跑到第几个用例了,还有多少用例要跑,怎么办?

    因为用的nose框架,那就看看nose有没有这样的库支持,结果看了一圈,只找到一个nose-progressive,装完后,有两个问题:

    1、不支持windows

    2、对接jenkins没法用

    傻眼了吧,该怎么解决呢,就得自己写插件了。折腾了两个终于搞定,后需会再总结一个nose plugin接口的说明,这里就不说了。

    自己写的插件,效果如下:

    root@localhost]# nosetests -v -s test1.py --with-scheduling
    nose.config: INFO: Ignoring files matching ['^\.', '^_', '^setup\.py$']
    [1/3] test1.test_aa ... ok
    [2/3] test1.test_bb ... ok
    [3/3] test1.test_cc ... ok
    
    ----------------------------------------------------------------------
    Ran 3 tests in 6.008s
    
    OK

    用例前面有简陋的进度条提示,正好是我自己的需求,不用太复杂

    插件代码如下:

    """Print progress to stdout. Enabled by --with-scheduling"""
    
    from functools import partial
    import itertools
    import logging
    import os
    import sys
    
    from nose.plugins import Plugin
    
    log = logging.getLogger('nose.plugins.nosescheduling')
    
    
    class Progress(Plugin):
        name = 'scheduling'
        _handler_prefix = 'nose_schedulings_'
        #encoding = "UTF-8"
        _totalTests = 0
    
        def __init__(self):
            super(Progress, self).__init__()  # involved the Plugin init
            self.test_numbers = itertools.count(1)
    
        def options(self, parser, env=os.environ):
            super(Progress, self).options(parser, env=env)
    
        def configure(self, options, conf):
            super(Progress, self).configure(options, conf)
            if not self.enabled:
                return
    
        def prepareTestLoader(self, loader):
            def capture_suite(orig_method, *args, **kwargs):
                self._totalTests += orig_method(*args, **kwargs).countTestCases()
                loader._visitedPaths = set()
                return orig_method(*args, **kwargs)
            if hasattr(loader, 'loadTestsFromNames'):
                loader.loadTestsFromNames = partial(capture_suite,
                                                    loader.loadTestsFromNames)
    
        def startTest(self, test):
            progress = '[{0}/{1}] '.format(next(self.test_numbers), self._totalTests)
            sys.stderr.write(progress)

    简单几行,把需求搞定。。

    有需要的朋友可以通过pip install noseprogress 安装使用

    pypi 上的链接如下:https://pypi.org/project/noseprogress/

    如果有特别需求也可联系我

  • 相关阅读:
    215. Kth Largest Element in an Array
    214. Shortest Palindrome
    213. House Robber II
    212. Word Search II
    210 Course ScheduleII
    209. Minimum Size Subarray Sum
    208. Implement Trie (Prefix Tree)
    207. Course Schedule
    206. Reverse Linked List
    sql 开发经验
  • 原文地址:https://www.cnblogs.com/landhu/p/10560289.html
Copyright © 2011-2022 走看看