When I run doctest in a script, it doesn't work on the first run, but does on the second run.
To reproduce the issue:
-
Make a script with a doctest and save it. Here's a minimal example which should pass:
""" >>> 1 1 """ import doctest print(doctest.testmod()) # Print results
-
Press Ctrl+R to run, and you get results as if there are no docstrings:
TestResults(failed=0, attempted=0)
-
Press Ctrl+R to run again, and you get the correct results:
TestResults(failed=0, attempted=1)
-
This issue also affects changes to an existing docstring. Change the code to this, which should fail:
""" >>> 1 2 """ import doctest print(doctest.testmod()) # Print results
-
Press Ctrl+R, and it succeeds:
TestResults(failed=0, attempted=1)
-
Press Ctrl+R again, and it fails as it should:
********************************************************************** File "/.../test.py", line 2, in __main__ Failed example: 1 Expected: 2 Got: 1 ********************************************************************** 1 items had failures: 1 of 1 in __main__ ***Test Failed*** 1 failures. TestResults(failed=1, attempted=1)
It's almost like it's getting the docstrings from the last run, but I have no idea why. FWIW, I've checked and made sure that __doc__
is working properly, and the doctest works properly if you run the script from a shell.