34

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?

Troy
  • 21,172
  • 20
  • 74
  • 103
  • I haven't found an answer to this, yet, but it appears that certain imports within the file are what cause the problem to come up. So I sometimes can avoid the issue restructuring things so that the function I'm testing is moved to another module. Splitting things up into different modules often results in cleaner, more-reusable code anyway, so it's not a horrible workaround... but still, it would be better if PyCharm would just work consistently. Hopefully jetbrains will fix the issue eventually. – Troy Jun 02 '15 at 19:05
  • On a related note, I upgraded to PyCharm 4.5 yesterday, and haven't seen the issue again, so far. But it's only been 2 days, so it doesn't really mean that the problem is fixed. But I'm hopeful :) – Troy Jun 02 '15 at 19:10
  • Hmm.. I'm currently using PyCharm 4.5 and still seeing the problem :( Thanks for the tip about restructuring btw, I'll try that. – s1m0n Jun 02 '15 at 19:16
  • Not sure what causes it to give the option, but you can force it to run doctest by going to Run>Edit Configurations...>click the green +>Python tests>Doctests>Enter your script – Sepehr Nazari Nov 24 '15 at 20:33
  • I get the impression that the problem comes after importing sqlite3 – Christian Nov 26 '15 at 12:49
  • Seems to work absolutely fine for my under PyCharm 5. Unless i don't have a doc in the function, but that is obvious I guess. – Damian Chrzanowski Nov 28 '15 at 00:25
  • I checked with the latest version of PyCharm (v5) and it works very well. I did a copy/paste with the doctest example from https://docs.python.org/2/library/doctest.html into a new file and PyCharm recognized it and allowed me to "Run 'Doctest' factorial", returning the doctest results. If this still fails for you, I would suggest you to [raise a bug with PyCharm](https://youtrack.jetbrains.com/issues/PY). – sorin Nov 26 '15 at 13:15
  • Best I can tell, if the cursor is positioned within the docstring of a method, and that docstring has `>>>` in it, then a right click will bring up the Run Doctest ... option. Right clicking with the mouse pointer over the docstring does not bring up the dialog if the cursor was not within in the docstring. Is this different then the behavior that you see? – Laizer Nov 29 '15 at 05:43
  • @Troy, I checked my answer below on a friend's system running `PyCharm` 4.5 and it applies to that version as well. – asherbret Dec 02 '15 at 14:22
  • Also do not name the module as `test.py` and `doctest.py`, someone might make this mistake. – Guannan Shen Jan 17 '21 at 18:27

3 Answers3

20

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:

  1. In PyCharm, create a new Python module: "temp"

2.Add the following to the module:

"""
    >>> print 3.14
    3.14
"""

if __name__ == '__main__':
    pass
  1. Right click on the doctest section gives the option to "Run 'Doctests in temp' "
  2. Right click on the main section gives the option to "Run 'temp' "
  3. Choosing anyone of the options, makes the other option disappear in subsequent runs. E.g., choosing to run the module will make the option to run Doctests disappear in subsequent runs, and vice versa. Going back to the first stage, where it was possible to choose between the two options, is possible by deleting the module's "Run configuration":

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:

  • If no Run Configuration is found, PyCharm suggests to run the module in any possible way (as a script, doctests, or unittests)
  • If a Run Configuration is found, PyCharm only suggests that Configuration.
  • If PyCharm isn't giving you the run option that you want, find the 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.
Troy
  • 21,172
  • 20
  • 74
  • 103
asherbret
  • 5,439
  • 4
  • 38
  • 58
13

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

dugloon
  • 196
  • 1
  • 5
  • Thanks for this. PyCharm is a great tool, but they sure do bury a lot of features in non-intuitive ways. – ybull Feb 15 '17 at 16:37
  • 1
    Thank you so much! It seems that the main menu options `Run – Run …` and `Run – Debug …` are the canonical way to get all possible ways to run something. Why the right-click context menu doesn't have those two options is beyond me. – Hannes Sep 03 '17 at 17:15
0

If the above doesn't work for you - make sure that your module is not named doctest; it will cause a conflict and therefore cause the exception.

Julian Camilleri
  • 2,975
  • 1
  • 25
  • 34