Create a gist now

Instantly share code, notes, and snippets.

@Redsz /gist:5736166
Last active Mar 10, 2018

What would you like to do?
A Nose plugin intended to provide ordered test execution.
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):
def test_foo(self):
def test_boo(self):
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment