Q:

Run doctest on text file from .py file?

I want to run Python's doctest on a .txt file (ie, not on docstring), but I don't want to go to the command line.

python -m doctest myfile.txt

The reason I don't want to do it from the command line is that I just don't want to leave Visual Studio.  I want to run it with F5 and then see my output in VS as well.
Question: How can I run doctest from a .py file, then, not from the command line?

A:

check out doctest.testfile.  I think that will do what you want...
Here's the some of the source from doctest.py for example usage:
def _test():
    testfiles = [arg for arg in sys.argv[1:] if arg and arg[0] != '-']
    if not testfiles:
        name = os.path.basename(sys.argv[0])
        if '__loader__' in globals():          # python -m
            name, _ = os.path.splitext(name)
        print("usage: {0} [-v] file ...".format(name))
        return 2
    for filename in testfiles:
        if filename.endswith(".py"):
            # It is a module -- insert its dir into sys.path and try to
            # import it. If it is part of a package, that possibly
            # won't work because of package imports.
            dirname, filename = os.path.split(filename)
            sys.path.insert(0, dirname)
            m = __import__(filename[:-3])
            del sys.path[0]
            failures, _ = testmod(m)
        else:
            failures, _ = testfile(filename, module_relative=False)
        if failures:
            return 1
    return 0

if __name__ == "__main__":
    sys.exit(_test())

