Search code examples
python-3.xdoctest

Running doctests using a different function


I wrote a module with a few functions along with their doctests, and I would like to run these tests on functions with the same names but written by someone else.

The documentation provides the following snippet for retrieving all tests for somefunction in mymodule and then running them in the usual way (like running doctest.testmod()):

TESTS = doctest.DocTestFinder().find(mymodule.somefunction)
DTR = doctest.DocTestRunner(verbose=True)
for test in TESTS:
    print (test.name, '->', DTR.run(test))

But I don't know where to go from here to have those tests run on theirmodule.somefunction instead. I tried changing the filename field from mymodule to theirmodule in the Example objects for each test, but to no avail. Does anyone know how to achieve this?


Solution

  • This may not be the most elegant solution, but simply copying my docstrings to their functions in my script works:

    theirmodule.somefunction.__doc__ = mymodule.somefunction.__doc__
    

    And then I only need to run the snippet in my question on theirmodule.somefunction.