简体   繁体   English

如何使用PyCharm运行doctests?

[英]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). 在PyCharm IDE中,如果我右键单击带有doctest的函数/方法,有时右键单击菜单会给我选项:“ 运行'Doctest my_function_name' ”,有时右键单击菜单只会给出运行整个文件的选项(不作为doctest)。

What determines when it will give the "run doctest" option and when it will not? 什么时候它会给出“运行doctest”选项以及何时不会? 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 . PyCharm运行模块(或其中的测试)是通过运行配置完成的 When you right click a module, PyCharm searches for an existing Run Configuration for that module. 右键单击模块时, PyCharm搜索该模块的现有Run Configuration 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. 如果找到配置(这可能是由于先前的运行或手动创建Configuration ), PyCharm将仅建议运行该配置。

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 . 例如,如果创建了module.py的配置来运行其doctests ,那么右键单击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 ). 但是,如果没有找到配置, PyCharm建议在不同的选项中运行模块,具体取决于模块中的代码(定期运行,或运行doctests / unittests )。 Once an option is chosen, PyCharm creates the respective, temporary, Run Configuration , implicitly. 选择一个选项后, PyCharm会隐式创建相应的临时Run Configuration 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 (ie, Configurations that were created via running a module) Run Configurations - 3 in "Python", ie, scripts, and 3 in "Python Tests". 重要侧面说明:PyCharm节省多达6个临时(即Configurations通过运行一个模块所创建的) Run Configurations - 3在“巨蟒”,即脚本,并在“巨蟒测试” 3。 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 . 这意味着,如果你运行moduleA.pymoduleB.pymoduleC.py ,然后moduleD.py ,临时Configurations在PyCharm将moduleB.pymoduleC.pymoduleD.py The configuration of moduleA.py will be automatically deleted, unless explicitly saved. 除非明确保存,否则moduleA.py的配置将自动删除。

This behaviour can be reproduced as following: 此行为可以重现如下:

  1. In PyCharm , create a new Python module: "temp" PyCharm ,创建一个新的Python模块:“temp”

2.Add the following to the module: 2.将以下内容添加到模块中:

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

if __name__ == '__main__':
    pass
  1. Right click on the doctest section gives the option to "Run 'Doctests in temp' " 右键单击doctest部分,可以选择“在temp中运行'Doctests'”
  2. Right click on the main section gives the option to "Run 'temp' " 右键单击主要部分,可以选择“运行'temp'”
  3. Choosing anyone of the options, makes the other option disappear in subsequent runs. 选择任何选项,使另一个选项在后续运行中消失。 Eg, choosing to run the module will make the option to run Doctests disappear in subsequent runs, and vice versa. 例如,选择运行模块将使后续运行中运行Doctests的选项消失,反之亦然。 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. 现在我们回到第3步。

(Reproduced in PyCharm 5.0 and 4.5) (转载于PyCharm 5.0和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) 如果没有找到Run Configuration ,PyCharm建议以任何可能的方式运行模块(作为脚本,doctests或unittests)
  • If a Run Configuration is found, PyCharm only suggests that Configuration . 如果找到Run Configuration ,PyCharm仅建议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. 如果PyCharm没有为您提供所需的运行选项,请找到阻止它为您提供该选项并将其删除Run Configuration ,或创建一个将运行该文件或方法/函数的新配置,就像您一样想。

If you don't want to delete configurations, you can also hit the shortcut key for Run | 如果您不想删除配置,还可以点击Run |的快捷键 Resume Program (F9 for me) to pop up a complete list of choices 恢复程序(我的F9)弹出一个完整的选择列表

If the above doesn't work for you - make sure that your module is not named doctest ; 如果上述为你工作-确保你的模块没有命名doctest ; it will cause a conflict and therefore cause the exception. 它会导致冲突,从而导致异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM