A quick browse of a few other posts on the list tell me this question
has been asked before but the previous answers didn't exactly work for
me. I created my own solution, but given that I'm very much a Django
(and Python) newbie, if there are any glaring errors in my approach,
please point them out.

Anyhow, we want the Django test runner to run tests from modules other
than tests and models. The documentation says to use tests.py as a
hook to other tests, but it's not very explicit in how to do so. From
previous posts, we know that to "hook" unit tests, we just have to
import the testcase like so:

from project.app.some_module import testcase

... where testcase is an instance of unittest.TestCase. This seems to
work swell.

For doctests, the simplest solution seems to be creating a dictionary
called __test__ that points to the functions and classes whose
docstrings you want to test. For me at least however, this didn't work
well. Doctest does not recurse on the items in __test__, meaning if
you have something in __test__ pointing to a class, it won't read the
docstrings for each class method, only the class's docstring. This
meant you had to manually enter in something for each class method, a
huge pain.

After that, I just put in a custom test suite in my tests.py file. It
seems to work fine, at least in the development version of Django. If
anyone sees any glaring mistakes, please point them out. Otherwise,
this might help someone else down the road:

# Start tests.py
# Place all your tests in separate modules and hook them by
# importing them and adding them to TEST_MODULE_LIST

import unittest
from django.test import _doctest as doctest
from django.test.testcases import OutputChecker, DocTestRunner
doctestOutputChecker = OutputChecker()

import project.app.views as views
import project.app.helpers as helpers

TEST_MODULE_LIST = [views, helpers]

def suite():
  s = unittest.TestSuite()
  for m in TEST_MODULE_LIST:
    s.addTest(unittest.defaultTestLoader.loadTestsFromModule(m))
    try:
      s.addTest(doctest.DocTestSuite(m,
        checker=doctestOutputChecker,
        runner=DocTestRunner))
    except ValueError:
      # No doc tests in tests.py
      pass
  return s

# End tests.py

-- Andrew

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to