The testrunner is probably the way to go!
I do this:
# somemod.test.py
from somemod import testing as reg # testing is a package with various
modules inside
__test__ = {
'create_tracker': reg.process.create_tracker,
'create_new_user': reg.process.create_new_user,
'default_promo': reg.process.default_promo,
'session_keys': reg.trackers.sess_keys,
}
The modules look like this:
_imports = _pre_setup + r"""
Import what we need
>>> from django.http import HttpRequest
>>> #Other imports here
"""
_pre_setup is a doctest that mimicks the pre_setup method on a django
TestCase and allows me to specify:
_pre_setup = r"""
>>> from django.core.management import call_command
>>> def _pre_setup(*fixtures):
... "see django.test.testcases.TextCase._pre_setup"
... call_command('flush', verbosity=0, interactive=False)
... if fixtures:
... call_command('loaddata', *fixtures, **{'verbosity': 0})
... mail.outbox = []
"""
This way I can mix and match doctests from lots of different places, and I
can use fixtures in my doctests. I find it pretty flexible, but I'm not
really 100% convinced it's the best way to go about this, so I'd be
interested in hearing from others.
Cheers,
Ben
2008/6/25 Andrew Fong <[EMAIL PROTECTED]>:
>
> 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
>
> >
>
--
Regards,
Ben Ford
[EMAIL PROTECTED]
+447792598685
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---