[英]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.py
, moduleB.py
, moduleC.py
,然后moduleD.py
,临时Configurations
在PyCharm将moduleB.py
, moduleC.py
和moduleD.py
。 The configuration of moduleA.py
will be automatically deleted, unless explicitly saved. 除非明确保存,否则
moduleA.py
的配置将自动删除。
This behaviour can be reproduced as following: 此行为可以重现如下:
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
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)
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) Run Configuration
is found, PyCharm only suggests that Configuration
. Run Configuration
,PyCharm仅建议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. 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.