Django

Code

Changeset 646

Show
Ignore:
Timestamp:
09/18/05 20:18:04 (3 years ago)
Author:
adrian
Message:

Fixed #506 -- runtests.py now allows models to be tested individually. Thanks, Simon

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/tests/runtests.py

    r466 r646  
    4343        log_error(test.name, "API test raised an exception", 
    4444            "Code: %r\nLine: %s\nException: %s" % (example.source.strip(), example.lineno, tb)) 
    45              
     45 
    4646class DjangoDoctestOutputChecker(doctest.OutputChecker): 
    4747    def check_output(self, want, got, optionflags): 
     
    5555 
    5656class TestRunner: 
    57     def __init__(self, verbosity_level=0): 
     57    def __init__(self, verbosity_level=0, which_tests=None): 
    5858        self.verbosity_level = verbosity_level 
     59        self.which_tests = which_tests 
    5960 
    6061    def output(self, required_level, message): 
     
    6768        from django.core import management, meta 
    6869 
    69         self.output(0, "Running tests with database %r" % settings.DATABASE_ENGINE) 
    70  
    7170        # Manually set INSTALLED_APPS to point to the test app. 
    7271        settings.INSTALLED_APPS = (APP_NAME,) 
     72 
     73        # Determine which models we're going to test. 
     74        test_models = get_test_models() 
     75        if self.which_tests: 
     76            # Only run the specified tests. 
     77            bad_models = [m for m in self.which_tests if m not in test_models] 
     78            if bad_models: 
     79                sys.stderr.write("Models not found: %s\n" % bad_models) 
     80                sys.exit(1) 
     81            else: 
     82                test_models = self.which_tests 
     83 
     84        self.output(0, "Running tests with database %r" % settings.DATABASE_ENGINE) 
    7385 
    7486        # If we're using SQLite, it's more convenient to test against an 
     
    108120        # Run the tests for each test model. 
    109121        self.output(1, "Running app tests") 
    110         for model_name in get_test_models()
     122        for model_name in test_models
    111123            self.output(1, "%s model: Importing" % model_name) 
    112124            try: 
     
    133145                db.rollback() 
    134146 
    135         # Run the non-model tests in the other tests dir 
    136         self.output(1, "Running other tests") 
    137         other_tests_dir = os.path.join(os.path.dirname(__file__), OTHER_TESTS_DIR) 
    138         test_modules = [f[:-3] for f in os.listdir(other_tests_dir) if f.endswith('.py') and not f.startswith('__init__')] 
    139         for module in test_modules: 
    140             self.output(1, "%s module: Importing" % module) 
    141             try: 
    142                 mod = __import__("othertests." + module, '', '', ['']) 
    143             except Exception, e: 
    144                 log_error(module, "Error while importing", ''.join(traceback.format_exception(*sys.exc_info())[1:])) 
    145                 continue 
    146             if mod.__doc__: 
    147                 p = doctest.DocTestParser() 
    148                 dtest = p.get_doctest(mod.__doc__, mod.__dict__, module, None, None) 
    149                 runner = DjangoDoctestRunner(verbosity_level=verbosity_level, verbose=False) 
    150                 self.output(1, "%s module: runing tests" % module) 
    151                 runner.run(dtest, clear_globs=True, out=sys.stdout.write) 
    152             if hasattr(mod, "run_tests") and callable(mod.run_tests): 
    153                 self.output(1, "%s module: runing tests" % module) 
     147        if not self.which_tests: 
     148            # Run the non-model tests in the other tests dir 
     149            self.output(1, "Running other tests") 
     150            other_tests_dir = os.path.join(os.path.dirname(__file__), OTHER_TESTS_DIR) 
     151            test_modules = [f[:-3] for f in os.listdir(other_tests_dir) if f.endswith('.py') and not f.startswith('__init__')] 
     152            for module in test_modules: 
     153                self.output(1, "%s module: Importing" % module) 
    154154                try: 
    155                     mod.run_tests(verbosity_level
     155                    mod = __import__("othertests." + module, '', '', ['']
    156156                except Exception, e: 
    157                     log_error(module, "Exception running tests", ''.join(traceback.format_exception(*sys.exc_info())[1:])) 
     157                    log_error(module, "Error while importing", ''.join(traceback.format_exception(*sys.exc_info())[1:])) 
    158158                    continue 
     159                if mod.__doc__: 
     160                    p = doctest.DocTestParser() 
     161                    dtest = p.get_doctest(mod.__doc__, mod.__dict__, module, None, None) 
     162                    runner = DjangoDoctestRunner(verbosity_level=verbosity_level, verbose=False) 
     163                    self.output(1, "%s module: running tests" % module) 
     164                    runner.run(dtest, clear_globs=True, out=sys.stdout.write) 
     165                if hasattr(mod, "run_tests") and callable(mod.run_tests): 
     166                    self.output(1, "%s module: running tests" % module) 
     167                    try: 
     168                        mod.run_tests(verbosity_level) 
     169                    except Exception, e: 
     170                        log_error(module, "Exception running tests", ''.join(traceback.format_exception(*sys.exc_info())[1:])) 
     171                        continue 
    159172 
    160173        # Unless we're using SQLite, remove the test database to clean up after 
     
    177190        # Display output. 
    178191        if error_list: 
    179             print "Got %s error%s:" % (len(error_list), len(error_list) != 1 and 's' or '') 
    180192            for d in error_list: 
    181193                print 
     
    183195                print "=" * len(d['title']) 
    184196                print d['description'] 
     197            print "%s error%s:" % (len(error_list), len(error_list) != 1 and 's' or '') 
    185198        else: 
    186199            print "All tests passed." 
     
    188201if __name__ == "__main__": 
    189202    from optparse import OptionParser 
     203    usage = "%prog [options] [model model model ...]" 
    190204    parser = OptionParser() 
    191205    parser.add_option('-v', help='How verbose should the output be? Choices are 0, 1 and 2, where 2 is most verbose. Default is 0.', 
     
    199213    if options.settings: 
    200214        os.environ['DJANGO_SETTINGS_MODULE'] = options.settings 
    201     t = TestRunner(verbosity_level
     215    t = TestRunner(verbosity_level, args
    202216    t.run_tests()