如何使用PyCharm运行doctests? [英] How do I run doctests with PyCharm?

查看:1816
本文介绍了如何使用PyCharm运行doctests?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PyCharm IDE中,如果我右键单击带有doctest的函数/方法,有时右键单击菜单会给我选项:运行'Doctest my_function_name'有时候相反,右键单击菜单只提供运行整个文件的选项(不作为doctest)。

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).

什么时候决定何时给出运行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?

推荐答案

在<运行模块(或其中的测试)中code> PyCharm 是通过完成的运行配置。当您右键单击某个模块时, PyCharm 将搜索该模块的现有运行配置。如果找到配置(这可能是由于先前的运行,或手动创建配置), PyCharm 只会建议运行该配置。

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.

例如,如果创建 module.py 的配置来运行其 doctests ,这是我们在右键单击 module.py 时会看到的选项。但是,如果没有找到配置, PyCharm 建议在不同的选项中运行模块,具体取决于模块中的代码(定期运行,或运行 doctests / unittests )。选择一个选项后, 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. 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.

重要的附注: PyCharm最多可以保存6个临时(即通过运行模块创建的配置运行配置 - 3 in Python,即脚本,以及Python测试中的3。这意味着,如果您运行 moduleA.py moduleB.py moduleC.py ,然后 moduleD.py ,PyCharm中的临时配置 moduleB.py moduleC.py moduleD.py 。除非明确保存,否则将自动删除 moduleA.py 的配置。

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.

此行为可以重现如下:


  1. PyCharm 中,创建一个新的Python模块:temp

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

2.将以下内容添加到模块中:

2.Add the following to the module:

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

if __name__ == '__main__':
    pass




  1. 右键单击doctest部分给出在'temp'中运行'Doctests'选项

  2. 右键单击 main 部分,选择运行'temp'

  3. 选择其中任何一个选项,使其他选项在后续运行中消失。例如,选择运行该模块将使后续运行中 Doctests 的选项消失,反之亦然。
    回到第一阶段,可以通过删除模块的运行配置来选择两个选项:

  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":

运行 - >编辑配置 - >找到模块的当前配置(通常突出显示) - >单击减号按钮(左上角) - >单击应用 - >单击确定。
现在我们回到第3步。

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.

(转载于 PyCharm 5.0和4.5)

(Reproduced in PyCharm 5.0 and 4.5)


  • 如果没有运行配置,PyCharm建议以任何可能的方式运行模块(作为脚本,doctests或unittests)。

  • 如果运行配置<找到/ code>,PyCharm只建议配置

  • 如果PyCharm没有给你运行选项你想要的,找到运行配置阻止它提供该选项并删除它,或者创建一个新的运行该文件的文件,或方法/功能,你想要的方式。

  • 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.

这篇关于如何使用PyCharm运行doctests?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆