In the PyCharm IDE, if I right-click on a function/method with a doctest, sometimes the right-click menu will give me the option: "Run 'Doctest my_function_name'" and sometimes the right-click menu, instead, only gives the option to run the whole file (NOT as a doctest).
What determines when it will give the "run doctest" option and when it will not? Is there a way to force it one way or the other?
Running a module (or the tests in it) in
PyCharm
is done via a Run Configuration. When you right click a module,PyCharm
searches for an existingRun Configuration
for that module. If a configuration is found (this can be due to a previous run, or a manual creation of aConfiguration
),PyCharm
will only suggest to run that configuration.For example, if a configuration of
module.py
was created to run itsdoctests
, that is the option we'll see when right-clickingmodule.py
. However, if no configuration is found,PyCharm
suggests to run the module in different options, depending on the code in the module (run regularly, or rundoctests
/unittests
). Once an option is chosen,PyCharm
creates the respective, temporary,Run Configuration
, implicitly. From here on, when right clicking the module, you'll only get the configuration that was created for that module.Important side note: PyCharm saves up to 6 temporary (i.e.,
Configurations
that were created via running a module)Run Configurations
- 3 in "Python", i.e., scripts, and 3 in "Python Tests". This means that if you runmoduleA.py
,moduleB.py
,moduleC.py
, and thenmoduleD.py
, the temporaryConfigurations
in PyCharm will bemoduleB.py
,moduleC.py
, andmoduleD.py
. The configuration ofmoduleA.py
will be automatically deleted, unless explicitly saved.This behaviour can be reproduced as following:
PyCharm
, create a new Python module: "temp"2.Add the following to the module:
Run --> Edit configuration --> Locate the module's current configuration (usually highlighted) --> Click the "Minus" button (top left corner) --> Click "Apply" --> Click OK. Now we are back at step 3.
(Reproduced in
PyCharm
5.0 and 4.5)To summarize:
Run Configuration
is found, PyCharm suggests to run the module in any possible way (as a script, doctests, or unittests)Run Configuration
is found, PyCharm only suggests thatConfiguration
.Run Configuration
that is preventing it from giving you that option and delete it, or create a new one that will run the file, or method/function, the way you want.If you don't want to delete configurations, you can also hit the shortcut key for Run | Resume Program (F9 for me) to pop up a complete list of choices