Ticket #5787: toptobottom.patch

File toptobottom.patch, 5.9 KB (added by Julian Berman, 9 years ago)
  • twisted/scripts/trial.py

    diff --git a/twisted/scripts/trial.py b/twisted/scripts/trial.py
    index 349b0c6..2159a3e 100644
    a b  
    44# See LICENSE for details.
    55
    66
    7 import sys, os, random, gc, time, warnings
     7import sys, os, random, gc, time, warnings, inspect
    88
    99from twisted.internet import defer
    1010from twisted.application import app
    def _reporterAction(): 
    9393    return usage.CompleteList([p.longOpt for p in
    9494                               plugin.getPlugins(itrial.IReporter)])
    9595
     96
     97# orders which can be passed to trial --order
     98_run_orders = [
     99    ("alphabetical", "alphabetical order (default)"),
     100    ("toptobottom",
     101     "attempt to run tests in the order they were defined on the test case"),
     102]
     103
     104
    96105class Options(usage.Options, app.ReactorSelectionMixin):
    97106    synopsis = """%s [options] [[file|package|module|TestCase|testmethod]...]
    98107    """ % (os.path.basename(sys.argv[0]),)
    class Options(usage.Options, app.ReactorSelectionMixin): 
    117126                 "Turn dirty reactor errors into warnings"],
    118127                ["until-failure", "u", "Repeat test until it fails"],
    119128                ["no-recurse", "N", "Don't recurse into packages"],
     129                ['help-orders', None, "Help on available test running orders"],
    120130                ['help-reporters', None,
    121131                 "Help on available output plugins (reporters)"]
    122132                ]
    123133
    124134    optParameters = [
    125135        ["logfile", "l", "test.log", "log file name"],
     136        ["order", "o", None, "Specify what order to run tests "
     137         "within each test case. See --help-orders for more info."],
    126138        ["random", "z", None,
    127139         "Run tests in random order using the specified seed"],
    128140        ['temp-directory', None, '_trial_temp',
    class Options(usage.Options, app.ReactorSelectionMixin): 
    133145
    134146    compData = usage.Completions(
    135147        optActions={"tbformat": usage.CompleteList(["plain", "emacs", "cgitb"]),
     148                    "order": usage.CompleteList(
     149                        name for name, _ in _run_orders),
    136150                    "reporter": _reporterAction,
    137151                    "logfile": usage.CompleteFiles(descr="log file name"),
    138152                    "random": usage.Completer(descr="random seed")},
    class Options(usage.Options, app.ReactorSelectionMixin): 
    203217        sys.settrace(spewer)
    204218
    205219
     220    def opt_help_orders(self):
     221        synopsis = ("Trial can attempt to run the tests in each test case in a"
     222                    " few different\n orders. You can select any of the "
     223                    "following options using --order=<foo>\n.")
     224
     225        print synopsis
     226        for name, description in _run_orders:
     227            print '   ', name, '\t', description
     228
    206229    def opt_help_reporters(self):
    207230        synopsis = ("Trial's output can be customized using plugins called "
    208231                    "Reporters. You can\nselect any of the following "
    class Options(usage.Options, app.ReactorSelectionMixin): 
    249272    opt_x = opt_extra
    250273
    251274
     275    def opt_order(self, order):
     276        """
     277        Run the tests in the given order.
     278
     279        """
     280
     281        if order == "toptobottom":
     282            self['order'] = _maybeFindSourceLine
     283        else:
     284            self['order'] = runner.name
     285
     286
    252287    def opt_recursionlimit(self, arg):
    253288        """
    254289        see sys.setrecursionlimit()
    def _getSuite(config): 
    333368    return loader.loadByNames(config['tests'], recurse)
    334369
    335370
     371def _maybeFindSourceLine(test_case):
     372    method = getattr(test_case, test_case._testMethodName)
     373
     374    try:
     375        return inspect.getsourcelines(method)[1]
     376    except IOError:
     377        return -1
     378
     379
    336380
    337381def _getLoader(config):
    338382    loader = runner.TestLoader()
    def _getLoader(config): 
    341385        randomer.seed(config['random'])
    342386        loader.sorter = lambda x : randomer.random()
    343387        print 'Running tests shuffled with seed %d\n' % config['random']
     388    elif config['order']:
     389        loader.sorter = config['order']
    344390    if not config['until-failure']:
    345391        loader.suiteFactory = runner.DestructiveTestSuite
    346392    return loader
  • twisted/trial/test/test_script.py

    diff --git a/twisted/trial/test/test_script.py b/twisted/trial/test/test_script.py
    index 6c93ebe..5ff54e5 100644
    a b class ExtraTests(unittest.TestCase): 
    480480        self.assertDeprecationWarning(self.config.opt_extra,
    481481                                      self.flushWarnings([self.test_xDeprecation]))
    482482
     483
     484
     485class OrderTestCase(unittest.TestCase):
     486    """
     487    Test case used to make assertions about the order its tests will be run in.
     488
     489    """
     490
     491    def test_first(self):
     492        pass
     493
     494    def test_second(self):
     495        pass
     496
     497    def test_third(self):
     498        pass
     499
     500    def test_fourth(self):
     501        pass
     502
     503
     504
     505class OrderTests(unittest.TestCase):
     506    """
     507    Tests for the --order option.
     508
     509    """
     510
     511    def setUp(self):
     512        self.config = trial.Options()
     513
     514    def tearDown(self):
     515        self.config = None
     516
     517    def test_alphabetical(self):
     518        """
     519        Check for alphabetical test ordering.
     520
     521        """
     522
     523        self.config.parseOptions(["--order", "alphabetical"])
     524        loader = trial._getLoader(self.config)
     525        suite = loader.loadClass(OrderTestCase)
     526        self.assertEqual(
     527            ['test_first', 'test_fourth', 'test_second', 'test_third'],
     528            [test._testMethodName for test in suite._tests])
     529
     530    def test_toptobottom(self):
     531        """
     532        Check that toptobottom runs tests in the order they were defined.
     533
     534        """
     535
     536        self.config.parseOptions(["--order", "toptobottom"])
     537        loader = trial._getLoader(self.config)
     538        suite = loader.loadClass(OrderTestCase)
     539        self.assertEqual(
     540            ['test_first', 'test_second', 'test_third', 'test_fourth'],
     541            [test._testMethodName for test in suite._tests])