Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run doctests with PyCharm?

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?

like image 711
Troy Avatar asked Mar 24 '15 19:03

Troy


People also ask

How do I get the run option in PyCharm?

Choose Run | Run from the main menu or press Alt+Shift+F10 , and then select the desired run/debug configuration.

How does PyCharm run all Pytests?

Run all tests in a directoryFrom the context menu, select the corresponding run command. If the directory contains tests that belong to the different testing frameworks, select the configuration to be used. For example, select Run pytest in <directory name>'. Explore results in the test runner.

How do I run a Testcase in PyCharm?

PyCharm makes it easy to select just one test to run. In fact, there are several ways to do it: With the cursor anywhere in the test you want to focus on, right-click and choose to run that in the test runner. Right-click on the test in the test tool listing and choose to run it.


1 Answers

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.
like image 88
asherbret Avatar answered Sep 20 '22 23:09

asherbret