import doctest import os import tempfile path = tempfile.mktemp() f = open(path, 'w') f.write(""" >>> '__file__' in globals() True """.strip()) f.close() test = doctest.DocFileTest(path, module_relative=False, optionflags=doctest.NORMALIZE_WHITESPACE) for i in range(1, 3): print("Test run #%d" % i) test.setUp() test.runTest() test.tearDown()