Django

Code

Ticket #5751: doctest_submodules.diff

File doctest_submodules.diff, 2.0 kB (added by akaihola, 1 year ago)

suggested patch, doctests now found inside multiple model files

  • django/test/simple.py

    old new  
    4242     
    4343    # Load unit and doctests in the models.py module. If module has 
    4444    # a suite() method, use it. Otherwise build the test suite ourselves. 
    45     if hasattr(app_module, 'suite'): 
    46         suite.addTest(app_module.suite()) 
     45    if app_module.__file__.endswith(('/models/__init__.py', '/models/__init__.pyc')): 
     46        from types import ModuleType 
     47        import os.path 
     48        app_locals = [getattr(app_module, x) for x in dir(app_module)] 
     49        app_dirname = os.path.dirname(app_module.__file__) 
     50        submodules = [x for x in app_locals 
     51                      if isinstance(x, ModuleType) 
     52                      and os.path.dirname(x.__file__) == app_dirname] 
    4753    else: 
    48         suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(app_module)) 
    49         try: 
    50             suite.addTest(doctest.DocTestSuite(app_module, 
    51                                                checker=doctestOutputChecker, 
    52                                                runner=DocTestRunner)) 
    53         except ValueError: 
    54             # No doc tests in models.py 
    55             pass 
    56      
     54        submodules = [] 
     55    for module in [app_module] + submodules: 
     56        if hasattr(module, 'suite'): 
     57            suite.addTest(module.suite()) 
     58        else: 
     59            suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(module)) 
     60            try: 
     61                suite.addTest(doctest.DocTestSuite(module, 
     62                                                   checker=doctestOutputChecker, 
     63                                                   runner=DocTestRunner)) 
     64            except ValueError: 
     65                # No doc tests in models.py 
     66                pass 
     67 
    5768    # Check to see if a separate 'tests' module exists parallel to the  
    5869    # models module 
    5970    test_module = get_tests(app_module)