Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 811215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:00:33+00:00 2026-05-15T01:00:33+00:00

So I created the following file (testlib.py) to automatically load all doctests (throughout my

  • 0

So I created the following file (testlib.py) to automatically load all doctests (throughout my nested project directories) into the __tests__ dictionary of tests.py:

# ./testlib.py
import os, imp, re, inspect
from django.contrib.admin import site

def get_module_list(start):
    all_files = os.walk(start)
    file_list = [(i[0], (i[1], i[2])) for i in all_files]
    file_dict = dict(file_list)

    curr = start
    modules = []
    pathlist = []
    pathstack = [[start]]

    while pathstack is not None:

        current_level = pathstack[len(pathstack)-1]
        if len(current_level) == 0:
            pathstack.pop()

            if len(pathlist) == 0:
                break
            pathlist.pop()
            continue
        pathlist.append(current_level.pop())
        curr = os.sep.join(pathlist)

        local_files = []
        for f in file_dict[curr][1]:
            if f.endswith(".py") and os.path.basename(f) not in ('tests.py', 'models.py'):
                local_file = re.sub('\.py$', '', f)
                local_files.append(local_file)

        for f in local_files:
            # This is necessary because some of the imports are repopulating the registry, causing errors to be raised
            site._registry.clear()
            module = imp.load_module(f, *imp.find_module(f, [curr]))
            modules.append(module)

        pathstack.append([sub_dir for sub_dir in file_dict[curr][0] if sub_dir[0] != '.'])

    return modules

def get_doc_objs(module):
    ret_val = []
    for obj_name in dir(module):
        obj = getattr(module, obj_name)
        if callable(obj):
            ret_val.append(obj_name)
        if inspect.isclass(obj):
            ret_val.append(obj_name)

    return ret_val

def has_doctest(docstring):
    return ">>>" in docstring

def get_test_dict(package, locals):
    test_dict = {}
    for module in get_module_list(os.path.dirname(package.__file__)):
        for method in get_doc_objs(module):
            docstring = str(getattr(module, method).__doc__)
            if has_doctest(docstring):

                print "Found doctests(s) " + module.__name__ + '.' + method

                # import the method itself, so doctest can find it
                _temp = __import__(module.__name__, globals(), locals, [method])
                locals[method] = getattr(_temp, method)

                # Django looks in __test__ for doctests to run. Some extra information is
                # added to the dictionary key, because otherwise the info would be hidden.
                test_dict[method + "@" + module.__file__] = getattr(module, method)

    return test_dict

To give credit where credit is due, much of this came from here

In my tests.py file, I have the following code:

# ./project/tests.py
import testlib, project
__test__ = testlib.get_test_dict(project, locals())

All of this works quite well to load my doctests from all of my files and subdirectories. The problem is that when I import and invoke pdb.set_trace() anywhere, this is all I see:

(Pdb) l
(Pdb) args
(Pdb) n
(Pdb) n
(Pdb) l
(Pdb) cont

doctest is apparently capturing and mediating the output itself, and is using the output in assessing the tests. So, when the test run completes, I see everything that should have printed out when I was in the pdb shell within doctest’s failure report. This happens regardless of whether I invoke pdb.set_trace() inside a doctest line or inside the function or method being tested.

Obviously, this is a big drag. Doctests are great, but without an interactive pdb, I cannot debug any of the failures that they are detecting in order to fix them.

My thought process is to possibly redirect pdb’s output stream to something that circumvents doctest’s capture of the output, but I need some help figuring out the low-level io stuff that would be required to do that. Also, I don’t even know if it would be possible, and am too unfamiliar with doctest’s internals to know where to start. Anyone out there have any suggestions, or better, some code that could get this done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:00:34+00:00Added an answer on May 15, 2026 at 1:00 am

    I was able to get pdb by tweaking it. I just put the following code at the bottom of my testlib.py file:

    import sys, pdb
    class TestPdb(pdb.Pdb):
        def __init__(self, *args, **kwargs):
            self.__stdout_old = sys.stdout
            sys.stdout = sys.__stdout__
            pdb.Pdb.__init__(self, *args, **kwargs)
    
        def cmdloop(self, *args, **kwargs):
            sys.stdout = sys.__stdout__
            retval = pdb.Pdb.cmdloop(self, *args, **kwargs)
            sys.stdout = self.__stdout_old
    
    def pdb_trace():
        debugger = TestPdb()
        debugger.set_trace(sys._getframe().f_back)
    

    In order to use the debugger I just import testlib and call testlib.pdb_trace() and am dropped into a fully functional debugger.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.