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 existing Run Configuration
for that module. If a configuration is found (this can be due to a previous run, or a manual creation of a Configuration
), PyCharm
will only suggest to run that configuration.
For example, if a configuration of module.py
was created to run its doctests
, that is the option we'll see when right-clicking module.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 run doctests
/ 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 run moduleA.py
, moduleB.py
, moduleC.py
, and then moduleD.py
, the temporary Configurations
in PyCharm will be moduleB.py
, moduleC.py
, and moduleD.py
. The configuration of moduleA.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:
"""
>>> print 3.14
3.14
"""
if __name__ == '__main__':
pass
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)
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 that Configuration
.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.