Changeset 127

Show
Ignore:
Timestamp:
Wed Nov 22 10:32:17 2006
Author:
jpellerin
Message:

For #107: import all unittest.TestCase assert funcs as pep8-named funcs in nose.tools

Files:

Legend:

Unmodified
Added
Removed
Modified
  • trunk/unit_tests/test_tools.py

    r111 r127  
    91 91         assert f2.setup == 'setup'  
    92 92         assert f2.teardown == 'teardown'  
      93  
      94     def test_testcase_funcs(self):  
      95         import nose.tools  
      96         tc_asserts = [ at for at in dir(nose.tools)  
      97                        if at.startswith('assert_') ]  
      98         print tc_asserts  
      99          
      100         # FIXME: not sure which of these are in all supported  
      101         # versions of python  
      102         assert 'assert_true' in tc_asserts  
      103         assert 'assert_raises' in tc_asserts  
      104  
    93 105              
    94 106 if __name__ == '__main__':  
  • trunk/nose/tools.py

    r111 r127  
    7 7 on any of these methods.  
    8 8 """  
      9 import re  
    9 10 import time  
      11 import unittest  
      12  
      13 __all__ = ['ok_', 'eq_', 'make_decorator', 'raises', 'timed', 'TimeExpired']  
      14  
      15  
      16 #  
      17 # Expose assert* from unittest.TestCase  
      18 # - give them pep8 style names  
      19 #  
      20 caps = re.compile('([A-Z])')  
      21  
      22 def pep8(name):  
      23     return caps.sub(lambda m: '_' + m.groups()[0].lower(), name)  
      24  
      25 class Dummy(unittest.TestCase):  
      26     def nop():  
      27         pass  
      28 _t = Dummy('nop')  
      29  
      30 for at in [ at for at in dir(_t)  
      31             if at.startswith('assert') and not '_' in at ]:  
      32     pepd = pep8(at)  
      33     vars()[pepd] = getattr(_t, at)  
      34     __all__.append(pepd)  
    10 35  
    11 36  
     
    13 38     pass  
    14 39  
      40  
    15 41 def ok_(expr, msg=None):  
    16 42     """Shorthand for assert. Saves 3 whole characters!  
     
    18 44     assert expr, msg  
    19 45  
      46  
    20 47 def eq_(a, b, msg=None):  
    21 48     """Shorthand for 'assert a == b, "%r != %r" % (a, b)  
     
    23 50     assert a == b, msg or "%r != %r" % (a, b)  
    24 51  
      52  
    25 53 def make_decorator(func):  
    26 54     """  
     
    42 70     return decorate  
    43 71  
      72  
    44 73 def raises(*exceptions):  
    45 74     """Test must raise one of expected exceptions to pass. Example use::  
     
    70 99     return decorate  
    71 100  
      101  
    72 102 def timed(limit):  
    73 103     """Test must finish within specified time limit to pass. Example use::  
     
    88 118     return decorate  
    89 119  
      120  
    90 121 def with_setup(setup=None, teardown=None):  
    91 122     """Decorator to add setup and/or teardown methods to a test function