问题描述
在 PyCharm IDE 中,如果我右键单击带有 doctest 的函数/方法,有时右键菜单会给我选项:Run '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).
什么决定什么时候会给出run 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?
推荐答案
在 PyCharm
中运行一个模块(或其中的测试)是通过 运行配置.当您右键单击一个模块时,PyCharm
会搜索该模块的现有 Run Configuration
.如果找到配置(这可能是由于之前的运行,或手动创建 Configuration
),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
/单元测试
).一旦选择了一个选项,PyCharm
会隐式地创建相应的临时 Run 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.
重要的旁注: PyCharm 最多保存 6 个临时(即,通过运行模块创建的 Configurations
)Run Configurations
- 3在Python"中,即脚本,以及在Python 测试"中的 3.这意味着如果您运行 moduleA.py
、moduleB.py
、moduleC.py
,然后运行 moduleD.py
,PyCharm 中的临时 Configurations
将是 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.
这种行为可以重现如下:
This behaviour can be reproduced as following:
- 在
PyCharm
中,新建一个Python模块:temp"
- 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
- 右键单击 doctest 部分可以选择Run 'Doctests in temp'"
- 右键单击main部分,可以选择Run 'temp'"
- 选择任何一个选项都会使另一个选项在后续运行中消失.例如,选择 run 模块将使运行 Doctests 的选项在后续运行中消失,反之亦然.回到第一阶段,可以在两个选项之间进行选择,可以通过删除模块的运行配置":
- Right click on the doctest section gives the option to "Run 'Doctests in temp' "
- Right click on the main section gives the option to "Run 'temp' "
- 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)
- 如果未找到
Run Configuration
,PyCharm 建议以任何可能的方式运行模块(作为脚本、文档测试或单元测试) - 如果找到
Run Configuration
,PyCharm 只会建议Configuration
. - 如果 PyCharm 没有为您提供所需的运行选项,请找到阻止它为您提供该选项的
Run Configuration
并删除它,或者创建一个新的,它将按照您想要的方式运行文件或方法/函数.
- 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 thatConfiguration
. - 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 运行文档测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!