在自動化測試過程當中,當用例不少且要跑好久時,就會出現這樣一個問題,不知道當前跑到第幾個用例了,還有多少用例要跑,怎麼辦?windows
由於用的nose框架,那就看看nose有沒有這樣的庫支持,結果看了一圈,只找到一個nose-progressive,裝完後,有兩個問題:框架
一、不支持windows測試
二、對接jenkins無法用ui
傻眼了吧,該怎麼解決呢,就得本身寫插件了。折騰了兩個終於搞定,後需會再總結一個nose plugin接口的說明,這裏就不說了。spa
本身寫的插件,效果以下:插件
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
用例前面有簡陋的進度條提示,正好是我本身的需求,不用太複雜code
插件代碼以下:orm
"""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)
簡單幾行,把需求搞定。。blog
有須要的朋友能夠經過pip install noseprogress 安裝使用接口
pypi 上的連接以下:https://pypi.org/project/noseprogress/
若是有特別需求也可聯繫我