Changeset 131

Show
Ignore:
Timestamp:
Sun Nov 26 11:51:56 2006
Author:
jpellerin
Message:

Work on moving scratch implementation of collectors into nose.suite.

Files:

Legend:

Unmodified
Added
Removed
Modified
  • branches/new_loader/unit_tests/test_suite.py

    r113 r131  
    10 10  
    11 11     def test_module_suite_repr(self):  
    12           from nose.importer import _import  
      12         from mock import Bucket  
    12 12         from nose.suite import ModuleSuite  
    13 13  
      14         loader = Bucket()  
    14 15         conf = Config()  
    15           conf.where = self.support  
    16           testmod = _import('test', [self.support], conf)  
    17           s = ModuleSuite([], module=testmod)  
      16         Bucket.conf = conf  
      17         s = ModuleSuite(loader=loader, modulename='test',  
      18                         filename=os.path.join(self.support, 'test.py'))  
    18 19         self.assertEqual("%s" % s,  
    19 20                          "test module test in %s" % self.support)  
    20    
    21           footestmod =  _import('foo.test_foo', [self.support], conf)  
    22           s = ModuleSuite([], module=footestmod)  
      21         s = ModuleSuite(loader=loader, modulename='foo.test_foo',  
      22                         filename=os.path.join(self.support, 'foo',  
      23                                               'test_foo.py'))  
    23 24         print s  
    24 25         self.assertEqual("%s" % s,  
    25 26                          "test module foo.test_foo in %s" % self.support)  
    26    
    27           foomod =  _import('foo', [self.support], conf)  
    28           s = ModuleSuite([], module=foomod)  
      27         s = ModuleSuite(loader=loader, modulename='foo',  
      28                         filename=os.path.join(self.support, 'foo'))  
    29 29         print s  
    30 30         self.assertEqual("%s" % s,  
  • branches/new_loader/nose/suite.py

    r116 r131  
    9 9 from nose.case import MethodTestCase  
    10 10 from nose.config import Config  
    11   from nose.importer import _import  
      11 from nose.importer import load_source  
    11 11 from nose.util import try_run  
    12 12  
    13 13 log = logging.getLogger('nose.suite')  
    14 14  
      15 class StopTest(Exception):  
      16     pass  
      17  
      18  
    15 19 class TestSuite(unittest.TestSuite):  
    16 20     """A test suite with setup and teardown methods.  
     
    24 28          
    25 29     def run(self, result):  
    26           result.startTest(self)  
    27 30         try:  
    28 31             try:  
    29                   self.setUp()  
    30               except KeyboardInterrupt:  
    31                   raise  
    32               except:  
    33                   result.addError(self, sys.exc_info())  
    34                   return  
    35               for test in self._tests:  
    36                   log.debug("running test %s", test)  
    37                   if result.shouldStop:  
    38                       break  
    39                   test(result)  
    40               try:  
    41                   self.tearDown()  
    42               except KeyboardInterrupt:  
    43                   raise  
    44               except:  
    45                   result.addError(self, sys.exc_info())             
    46               return result  
    47           finally:  
    48               result.stopTest(self)  
    49    
      32                 self.startTest(result)  
      33                 try:  
      34                     self.setUp()  
      35                 except KeyboardInterrupt:  
      36                     raise  
      37                 except StopTest:  
      38                     pass  
      39                 except:  
      40                     result.addError(self, sys.exc_info())  
      41                     return  
      42                 for test in self._tests:  
      43                     log.debug("running test %s", test)  
      44                     if result.shouldStop:  
      45                         break  
      46                     test(result)  
      47                 try:  
      48                     self.tearDown()  
      49                 except KeyboardInterrupt:  
      50                     raise  
      51                 except StopTest:  
      52                     pass  
      53                 except:  
      54                     result.addError(self, sys.exc_info())             
      55                 return result  
      56             finally:  
      57                 self.stopTest(result)  
      58         except StopTest:  
      59             pass  
      60          
    50 61     def setUp(self):  
    51 62         pass  
     
    53 64     def shortDescription(self):  
    54 65         return str(self) # FIXME  
      66  
      67     def startTest(self):  
      68         result.startTest(self)  
      69  
      70     def stopTest(self):  
      71         result.stopTest(self)  
    55 72      
    56 73     def tearDown(self):  
     
    58 75  
    59 76  
    60   class ModuleSuite(TestSuite):  
    61       """Suite of tests in a module.  
      77 class TestCollector(TestSuite):  
      78     """A TestSuite that collects its own tests.  
    62 79     """  
    63       def __init__(self, tests, module=None, error=None, **kw):  
    64           super(ModuleSuite, self).__init__(tests=tests, **kw)  
    65           self.module = module  
    66           self.error = error  
      80     def __init__(self, loader=None, **kw):  
      81         super(TestCollector, self).__init__(**kw)  
      82         self.loader = loader  
      83         self.conf = loader.conf  
      84         self._collected = False  
      85  
      86     def __nonzero__(self):  
      87         self.collectTests()  
      88         return bool(self._tests)  
      89  
      90     def __len__(self):  
      91         self.collectTests()  
      92         return len(self._tests)  
      93  
      94     def __iter__(self):  
      95         self.collectTests()  
      96         return iter(self._tests)  
    67 97  
      98     def collectTests(self):  
      99         pass  
      100  
      101     def startTest(self):  
      102         self.collectTests()  
      103         if not iter(self):  
      104             raise StopTest("No tests to run")  
      105         result.startTest(self)  
      106  
      107  
      108 class ModuleSuite(TestCollector):  
      109     """Test Collector that collects tests in a single module or  
      110     package. For pakages, tests are collected depth-first. This is to  
      111     ensure that a module's setup and teardown fixtures are run only if  
      112     the module contains tests.  
      113     """  
      114     def __init__(self, modulename=None, filename=None, working_dir=None,  
      115                  testnames=None, **kw):  
      116         self.modulename = modulename  
      117         self.filename = filename  
      118         self.working_dir = working_dir  
      119         self.testnames = testnames  
      120         self.module = None  
      121         super(ModuleSuite, self).__init__(**kw)  
      122          
    68 123     def __repr__(self):  
    69           try:  
    70               name = self.module.__name__  
    71               if hasattr(self.module, '__path__') and self.module.__path__:  
    72                   path = self.module.__path__[0]  
    73               else:                 
    74                   path = self.module.__file__  
    75               for i in xrange(0, len(name.split('.'))):  
    76                   path = os.path.dirname(path)  
    77               return "test module %s in %s" % (name, path)  
    78           except AttributeError:  
    79               from traceback import format_exception  
    80               if self.error:  
    81                   ef = ''.join(format_exception(*self.error))  
    82               else:  
    83                   ef = ''  
    84               return "test module (%s, %s)" % (self.module, ef)  
      124         path = os.path.dirname(self.filename)  
      125         while (os.path.exists(os.path.join(path, '__init__.py'))):  
      126             path = os.path.dirname(path)  
      127         return "test module %s in %s" % (self.modulename, path)  
    85 128     __str__ = __repr__  
    86 129  
    87       def run(self, result):  
    88           if self.error:  
    89               result.addError(self.error)  
    90               return  
    91           # Don't bother running setup and teardown if we don't  
    92           # have any tests to run.  
    93           if not self._tests:  
      130     def addTest(self, test):  
      131         # depth-first?  
      132         if test:  
      133             self._tests.append(test)  
      134  
      135     def collectTests(self):  
      136         # print "Collect Tests %s" % self  
      137         if self._collected or self._tests:  
    94 138             return  
    95           super(ModuleSuite, self).run(result)  
      139         self._collected = True  
      140         self._tests = []  
      141         if self.module is None:  
      142             # FIXME  
      143             # We know the exact source of each module so why not?  
      144             # We still need to add the module's parent dir (up to the top  
      145             # if it's a package) to sys.path first, though  
      146             self.module = load_source(self.name, self.path, self.conf)  
      147         for test in self.loader.loadTestsFromModule(self.module,  
      148                                                     self.testnames):  
      149             self.addTest(test)  
    96 150  
    97 151     def setUp(self):  
  • branches/new_loader/nose/importer.py

    r113 r131  
    8 8 import os  
    9 9 import sys  
    10   from imp import find_module, load_module, acquire_lock, release_lock  
      10 from imp import find_module, load_module, acquire_lock, release_lock, \  
      11      load_source as _load_source  
    11 12  
    12 13 log = logging.getLogger(__name__)  
     
    27 28         log.debug("insert %s into sys.path", path)  
    28 29         sys.path.insert(0, path)  
    29            
      30  
      31  
      32 def load_source(name, path, conf):  
      33     """Wrap load_source to make sure that the dir of the module (or package)  
      34     is in sys.path before the module is loaded.  
      35     """  
      36     if conf.addPaths:  
      37         add_path(os.path.dirname(path))  
      38     return _load_source(name, path)  
      39                    
    30 40 def _import(name, path, conf):  
    31 41     """Import a module *only* from path, ignoring sys.path and  
  • branches/new_loader/work.py

    r123 r131  
    34 34 class ModuleSuite:  
    35 35     def __init__(self, name, path, loader, working_dir, tests):  
      36         # FIXME name -> modulename  
      37         #       path -> filename  
      38         #       document tests vs _tests  
    36 39         self.name = name  
    37 40         self.path = path  
     
    78 81          
    79 82     def run(self, result):  
      83         # startTest  
    80 84         self.collectTests()  
    81 85         if not self:  
     
    86 90             test(result)  
    87 91         self.tearDown()  
    88    
      92         # stopTest()  
      93          
    89 94     def setUp(self):  
    90 95         print "SETUP %s" % self  
     
    96 101 class TestLoader:  
    97 102  
      103     # FIXME move collectTests to DirectorySuite  
      104      
    98 105     def collectTests(self, working_dir, names=None):  
    99 106         if not os.path.isabs(working_dir):