def docTestLineNumberFix(): """Modify doctest behaviour to figure out line numbers of strings in __test__ """ class MyDocTestFinder(doctest.DocTestFinder): def _find_lineno(self, obj, source_lines): if isinstance(obj, str): #find a unique line in the string, so we know where it is for offset,line in enumerate(obj.splitlines(True)): if source_lines.count(line)==1: return source_lines.index(line)-offset else: return super()._find_lineno(obj, source_lines) doctest.DocTestFinder = MyDocTestFinder