the following 2 solution enable my view function doctest-able: ----------------------solution 1---------it is not convenient to go to the error point in source.py -------------------------------------------------- add the following code to "models.py" of your "views.py" from common import get_doctest __test__ = get_doctest('userprofile.views')
######common.get_doctest defination:######## def get_doctest(module_name): # you test_dict={} module = __import__(module_name,{},{},module_name.split('.') [-1]) for obj_name,obj in module.__dict__.items(): if '__module__' in dir(obj) and obj.__module__ == module_name: if obj.__doc__: test_dict[obj_name] = obj.__doc__ return test_dict -----------solution2-------------you can 'click' and go to the error point in source.py (when in eclipse)---------------------------------------------- #modify C:\Python25\Lib\site-packages\django\test\simple.py #add get_views function def get_views(app_module): TEST_MODULE = 'views' try: app_path = app_module.__name__.split('.')[:-1] test_module = __import__('.'.join(app_path + [TEST_MODULE]), {}, {}, TEST_MODULE) except ImportError, e: # Couldn't import tests.py. Was it due to a missing file, or # due to an import error in a tests.py that actually exists? import os.path from imp import find_module try: mod = find_module(TEST_MODULE, [os.path.dirname (app_module.__file__)]) except ImportError: # 'tests' module doesn't exist. Move on. test_module = None else: # The module exists, so there must be an import error in the # test module itself. We don't need the module; so if the # module was a single file module (i.e., tests.py), close the file # handle returned by find_module. Otherwise, the test module # is a directory, and there is nothing to close. if mod[0]: mod[0].close() raise return test_module #modify the build_suite function as follow: def build_suite(app_module): "Create a complete Django test suite for the provided application module" suite = unittest.TestSuite() # Load unit and doctests in the models.py module. If module has # a suite() method, use it. Otherwise build the test suite ourselves. if hasattr(app_module, 'suite'): suite.addTest(app_module.suite()) else: suite.addTest(unittest.defaultTestLoader.loadTestsFromModule (app_module)) try: suite.addTest(doctest.DocTestSuite(app_module, checker=doctestOutputChecker, runner=DocTestRunner)) except ValueError: # No doc tests in models.py pass #added by hehao,enable 'views' doctestable view_module = get_views(app_module) if hasattr(view_module, 'suite'): suite.addTest(view_module.suite()) else: suite.addTest(unittest.defaultTestLoader.loadTestsFromModule (view_module)) try: suite.addTest(doctest.DocTestSuite(view_module, checker=doctestOutputChecker, runner=DocTestRunner)) except ValueError: # No doc tests in models.py pass # Check to see if a separate 'tests' module exists parallel to the # models module test_module = get_tests(app_module) if test_module: # Load unit and doctests in the tests.py module. If module has # a suite() method, use it. Otherwise build the test suite ourselves. if hasattr(test_module, 'suite'): suite.addTest(test_module.suite()) else: suite.addTest (unittest.defaultTestLoader.loadTestsFromModule(test_module)) try: suite.addTest(doctest.DocTestSuite(test_module, checker=doctestOutputChecker, runner=DocTestRunner)) except ValueError: # No doc tests in tests.py pass return suite #modify C:\Python25\Lib\site-packages\django\test\_doctest.py find the following code: # Recurse to functions & classes. if ((inspect.isfunction(val) or inspect.isclass(val)) and self._from_module(module, val)): self._find(tests, val, valname, module, source_lines, globs, seen) update to : # Recurse to functions & classes & decorator-object if ((inspect.isfunction(val) or inspect.isclass(val) or callable(val) ) and self._from_module(module, val)): self._find(tests, val, valname, module, source_lines, globs, seen) --------------------you can write doctest for view function--------------------------- @login_required def overview(request, template = "userprofile/profile/overview.html"): """ >>> from django.test.client import Client >>> c = Client() >>> c.login(username='aaa', password='aaa') True >>> response = c.get('/user/') >>> response.status_code 200 >>> if isinstance(response.context, list): ... response.context[0]['email'] ... else: ... response.context['email'] u'speed...@gmaila.com' """ ..................... ..................... ................... On Oct 10, 11:31 am, hao he <speed...@gmail.com> wrote: > @login_required > def overview(request, template = "userprofile/profile/overview.html"): > """ > this is the doctest of a view function! > > >>> from django.test.client import Client > >>> c = Client() > >>> c.login(username='fred', password='secret') > >>> response = c.get('/userprofile/overview/') > >>> response.context.email > > f...@gmail.com > > """ > > profile, created = Profile.objects.get_or_create > (user=request.user) > validated = False > try: > email = EmailValidation.objects.get(user=request.user).email > except EmailValidation.DoesNotExist: > email = request.user.email > > if email: > validated = True > > data = { 'email': email, 'validated': validated, 'fields' : > Profile._meta.fields } > > return render_to_response(template, data, > context_instance=RequestContext(request)) --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---