Ticket #5787: updated_top_bottom_patch.patch

File updated_top_bottom_patch.patch, 5.5 KB (added by Ying Li, 9 years ago)
  • twisted/scripts/trial.py

     
    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
     
    9595
    9696
    9797
     98# orders which can be passed to trial --order
     99_run_orders = [
     100    ("alphabetical", "alphabetical order (default)"),
     101    ("toptobottom",
     102     "attempt to run tests in the order they were defined on the test case"),
     103]
     104
     105
     106
    98107class _BasicOptions(object):
    99108    """
    100109    Basic options shared between trial and its local workers.
     
    107116
    108117    optFlags = [["help", "h"],
    109118                ["no-recurse", "N", "Don't recurse into packages"],
     119                ['help-orders', None, "Help on available test running orders"],
    110120                ['help-reporters', None,
    111121                 "Help on available output plugins (reporters)"],
    112122                ["rterrors", "e", "realtime errors, print out tracebacks as "
     
    118128                ]
    119129
    120130    optParameters = [
     131        ["order", "o", None, "Specify what order to run tests "
     132         "within each test case. See --help-orders for more info."],
    121133        ["random", "z", None,
    122134         "Run tests in random order using the specified seed"],
    123135        ['temp-directory', None, '_trial_temp',
     
    127139         'more info.']]
    128140
    129141    compData = usage.Completions(
    130         optActions={"reporter": _reporterAction,
     142        optActions={"order": usage.CompleteList(
     143                        name for name, _ in _run_orders),
     144                    "reporter": _reporterAction,
    131145                    "logfile": usage.CompleteFiles(descr="log file name"),
    132146                    "random": usage.Completer(descr="random seed")},
    133147        extraActions=[usage.CompleteFiles(
     
    197211        sys.settrace(spewer)
    198212
    199213
     214    def opt_help_orders(self):
     215        synopsis = ("Trial can attempt to run the tests in each test case in a"
     216                    " few different\n orders. You can select any of the "
     217                    "following options using --order=<foo>\n.")
     218
     219        print synopsis
     220        for name, description in _run_orders:
     221            print '   ', name, '\t', description
     222
    200223    def opt_help_reporters(self):
    201224        synopsis = ("Trial's output can be customized using plugins called "
    202225                    "Reporters. You can\nselect any of the following "
     
    229252                "tbformat must be 'plain', 'emacs', or 'cgitb'.")
    230253
    231254
     255    def opt_order(self, order):
     256        """
     257        Run the tests in the given order.
     258
     259        """
     260
     261        if order == "toptobottom":
     262            self['order'] = _maybeFindSourceLine
     263        else:
     264            self['order'] = runner.name
     265
     266
    232267    def opt_recursionlimit(self, arg):
    233268        """
    234269        see sys.setrecursionlimit()
     
    400435    return loader.loadByNames(config['tests'], recurse)
    401436
    402437
     438def _maybeFindSourceLine(test_case):
     439    method = getattr(test_case, test_case._testMethodName)
    403440
     441    try:
     442        return inspect.getsourcelines(method)[1]
     443    except IOError:
     444        return -1
     445
     446
     447
    404448def _getLoader(config):
    405449    loader = runner.TestLoader()
    406450    if config['random']:
     
    408452        randomer.seed(config['random'])
    409453        loader.sorter = lambda x : randomer.random()
    410454        print 'Running tests shuffled with seed %d\n' % config['random']
     455    elif config['order']:
     456        loader.sorter = config['order']
    411457    if not config['until-failure']:
    412458        loader.suiteFactory = runner.DestructiveTestSuite
    413459    return loader
  • twisted/trial/test/test_script.py

     
    549549        runner = trial._makeRunner(options)
    550550        self.assertIsInstance(runner, TrialRunner)
    551551        self.assertEqual(TrialRunner.DRY_RUN, runner.mode)
     552
     553
     554
     555
     556
     557class OrderTestCase(unittest.TestCase):
     558    """
     559    Test case used to make assertions about the order its tests will be run in.
     560    """
     561
     562    def test_first(self):
     563        pass
     564
     565    def test_second(self):
     566        pass
     567
     568    def test_third(self):
     569        pass
     570
     571    def test_fourth(self):
     572        pass
     573
     574
     575
     576class OrderTests(unittest.TestCase):
     577    """
     578    Tests for the --order option.
     579    """
     580
     581    def setUp(self):
     582        self.config = trial.Options()
     583
     584    def tearDown(self):
     585        self.config = None
     586
     587    def test_alphabetical(self):
     588        """
     589        Check for alphabetical test ordering.
     590        """
     591
     592        self.config.parseOptions(["--order", "alphabetical"])
     593        loader = trial._getLoader(self.config)
     594        suite = loader.loadClass(OrderTestCase)
     595        self.assertEqual(
     596            ['test_first', 'test_fourth', 'test_second', 'test_third'],
     597            [test._testMethodName for test in suite._tests])
     598
     599    def test_toptobottom(self):
     600        """
     601        Check that toptobottom runs tests in the order they were defined.
     602        """
     603
     604        self.config.parseOptions(["--order", "toptobottom"])
     605        loader = trial._getLoader(self.config)
     606        suite = loader.loadClass(OrderTestCase)
     607        self.assertEqual(
     608            ['test_first', 'test_second', 'test_third', 'test_fourth'],
     609            [test._testMethodName for test in suite._tests])