Ticket #6364: patch_django_6364.diff

File patch_django_6364.diff, 3.2 KB (added by David Larlet, 16 years ago)
  • django/test/simple.py

     
     1import os
    12import unittest
    23from django.conf import settings
    34from django.db.models import get_app, get_apps
     
    89
    910# The module name for tests outside models.py
    1011TEST_MODULE = 'tests'
    11    
     12DOCTEST_EXTENSION = '.txt'
     13
    1214doctestOutputChecker = OutputChecker()
    1315
    1416def get_tests(app_module):
     
    7173            except ValueError:
    7274                # No doc tests in tests.py
    7375                pass
     76            try:
     77                # Add doctests if there are .txt in tests' module
     78                doctests_dir = test_module.__path__[0]
     79                doctest_files = [os.path.join(doctests_dir, doc) \
     80                                    for doc in os.listdir(doctests_dir) \
     81                                        if doc.endswith(DOCTEST_EXTENSION)]
     82                for doctest_file in doctest_files:
     83                    suite.addTest(doctest.DocFileSuite(doctest_file,
     84                                    checker=doctestOutputChecker,
     85                                    runner=DocTestRunner,
     86                                    module_relative=False))
     87            except:
     88                # TODO
     89                pass
    7490    return suite
    7591
    7692def build_test(label):
    7793    """Construct a test case a test with the specified label. Label should
    7894    be of the form model.TestClass or model.TestClass.test_method. Returns
    7995    an instantiated test or test suite corresponding to the label provided.
    80        
    8196    """
    8297    parts = label.split('.')
    8398    if len(parts) < 2 or len(parts) > 3:
     
    100115    else: # label is app.TestClass.test_method
    101116        return TestClass(parts[2])
    102117
     118def build_doctest(label):
     119    """Construct a test case a test with the specified label. Label should
     120    be of the form model.doctest_filename. Returns an instantiated test or
     121    test suite corresponding to the label provided.
     122    """
     123    import os
     124    parts = label.split('.')
     125    if len(parts) != 2:
     126        raise ValueError("TODO")
     127    app = get_app(parts[0])
     128    test_filename = parts[1] + DOCTEST_EXTENSION
     129    test_file = os.path.join(get_tests(app).__path__[0], test_filename)
     130    try:
     131        return doctest.DocFileSuite(test_file,
     132                                    checker=doctestOutputChecker,
     133                                    runner=DocTestRunner,
     134                                    module_relative=False)
     135    except IOError:
     136        raise ValueError("File '%s' does not exist" % test_file)
     137
    103138def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
    104139    """
    105140    Run the unit tests for all the test labels in the provided list.
     
    127162    if test_labels:
    128163        for label in test_labels:
    129164            if '.' in label:
    130                 suite.addTest(build_test(label))
     165                try:
     166                    suite.addTest(build_test(label))
     167                except ValueError:
     168                    suite.addTest(build_doctest(label))
    131169            else:
    132170                app = get_app(label)
    133171                suite.addTest(build_suite(app))
Back to Top