public
Last active

A Nose plugin intended to provide ordered test execution.

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
"""
In some cases during testing you will want test execution to occur in a certain order. For example while performaing some forms of integration testing. This plugin is intended to provide this type of ordered test execution by decorating the tests with a step number.
 
def Foo(unittest.TestCase):
 
@step(number=1)
def test_foo(self):
pass
 
@step(number=2)
def test_boo(self):
pass
 
"""
 
import logging
import os
from nose import loader
 
from nose.plugins import Plugin
 
log = logging.getLogger('nose.plugins.step')
 
 
def step(**kwargs):
"""
Define ordered steps
"""
def wrapped(obj):
for key, value in kwargs.iteritems():
setattr(obj, key, value)
return obj
return wrapped
 
 
class Steps(Plugin):
"""
Order the tests within a unittest.TestCase defined by the @step decorator
"""
name = 'steps'
 
def options(self, parser, env=os.environ):
"""
Register commandline options.
"""
Plugin.options(self, parser, env)
parser.add_option('--steps', action='store_true', dest='steps',
help="Order the tests within a unittest.TestCase class")
 
def configure(self, options, conf):
"""
Configure plugin.
"""
Plugin.configure(self, options, conf)
if options.steps:
self.enabled = True
 
def loadTestsFromTestCase(self, cls):
"""
Return tests in this test case class. Ordered by the step definitions.
"""
l = loader.TestLoader()
tmp = l.loadTestsFromTestCase(cls)
 
test_order = []
for test in tmp._tests:
order = test.test._testMethodName
func = getattr(cls, test.test._testMethodName)
if hasattr(func, 'number'):
order = getattr(func, 'number')
test_order.append((test, order))
test_order.sort(key=lambda tup: tup[1])
tmp._tests = (t[0] for t in test_order)
return tmp

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.