因此,我创建了以下文件(testlib.py),以自动将所有doctest(整个嵌套项目目录中的文件)加载到tests.py的__tests__
词典中:
# ./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
为了在应得的信用额度上给予信用,其中大部分来自here
在我的tests.py文件中,我有以下代码:
# ./project/tests.py
import testlib, project
__test__ = testlib.get_test_dict(project, locals())
所有这些都能很好地从我的所有文件和子目录加载doctest。问题是,当我在任何地方导入并调用pdb.set_trace()时,这就是我所看到的:
(Pdb) l
(Pdb) args
(Pdb) n
(Pdb) n
(Pdb) l
(Pdb) cont
doctest显然是在捕获和中介输出本身,并且正在使用输出评估测试。因此,当测试运行完成时,在doctest的失败报告中,当我进入pdb shell时,我会看到应该打印的所有内容。无论我是在doctest行中还是在要测试的函数或方法中调用pdb.set_trace(),都会发生这种情况。
显然,这是一个很大的阻力。 Doctests很棒,但是没有交互式pdb,我无法调试它们为修复它们而检测到的任何故障。
我的思维过程是可能将pdb的输出流重定向到某种情况,从而避免doctest捕获输出,但是我需要一些帮助来确定执行此操作所需的低级io东西。另外,我什至不知道是否有可能,而且对doctest的内部知识也不熟悉,无法知道从哪里开始。任何人都可以提出建议,或者更好的一些代码可以做到这一点?
最佳答案
通过调整它,我可以获得pdb。我只是将以下代码放在我的testlib.py文件的底部:
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)
为了使用调试器,我只需
import testlib
并调用testlib.pdb_trace()
,然后放入一个功能齐全的调试器中。