我想开始为我的 Python 代码编写单元测试,py.test 框架听起来比 Python 的捆绑框架更好单元测试。所以我在我的项目中添加了一个“tests”目录,并添加了 test_sample.py 到它。现在我想配置 PyCharm 来运行“tests”目录中的所有测试。
据称,PyCharm 在其测试运行程序中支持 py.test。您应该能够 创建一个运行/debug 配置 来运行你的测试,据称 PyCharm 有一个 专门用于 py.test 的“创建配置”对话框。但这是他们关于该主题的文档的完整范围,我在任何地方都找不到这个所谓的对话框。
如果我右键单击项目工具窗口中的目录,我将 应该看到“创建<名称>”菜单项,但唯一以“创建”开头的菜单项是“创建运行配置”。好吧,也许文档只是错误的,“创建运行配置”听起来确实很有希望。不幸的是,其子菜单中仅有的两个项目是“C:\mypath... 中的Unittests”和“C:\mypath... 中的Doctests”,这两个项目都不适用——我既不使用unittest,也不使用doctest。 py.test 没有菜单项。
如果我打开 test_sample.py 并在编辑器窗口中右键单击,我确实会得到承诺的“Create ”菜单项:有“创建'test_sa中的单元测试...'...”,然后是“运行'test_sa中的单元测试...'”和“调试'test_sa中的单元测试...'”。再说一遍,这都是特定于单元测试框架的; py.test 没有任何内容。
如果我确实尝试显示“unittest”的菜单项,则会出现一个对话框,其中包含“名称”、“类型”选项、一个带有“文件夹”、“模式”、“脚本”和“类”选项的“测试”组框”和“函数”等。这听起来与添加 Python 单元测试的配置,而不是像应该显示在 py.test 对话框的配置。对话框中没有任何内容可以切换我要添加的测试框架。
我在 Windows 上使用 PyCharm 1.5.2 以及 Python 3.1.3 和 pytest 2.0.3。我可以从命令行成功地在我的测试中运行py.test
,所以这不是像未正确安装 pytest 这样简单的事情。
如何配置 PyCharm 来运行 py.test 测试?
I want to start writing unit tests for my Python code, and the py.test framework sounds like a better bet than Python's bundled unittest. So I added a "tests" directory to my project, and added test_sample.py to it. Now I want to configure PyCharm to run all the tests in my "tests" directory.
PyCharm allegedly supports py.test in its test runner. You're supposed to be able to create a run/debug configuration to run your tests, and PyCharm allegedly has a "create configuration" dialog box specifically for py.test. But that's the complete extent of their documentation on the subject, and I can't find this alleged dialog box anywhere.
If I right-click the directory in the Project tool window, I'm supposed to see a "Create <name>" menu item, but the only menu item starting with "Create" is "Create Run Configuration". Okay, maybe the documentation is just wrong, and "Create Run Configuration" does sound promising. Unfortunately, the only two items in its submenu are "Unittests in C:\mypath..." and "Doctests in C:\mypath...", neither of which applies -- I'm using neither unittest nor doctest. There is no menu item for py.test.
If I open my test_sample.py and right-click in the editor window, I do get the promised "Create <name>" menu items: there's "Create 'Unittests in test_sa...'...", followed by "Run 'Unittests in test_sa...'" and "Debug 'Unittests in test_sa...'". So again, it's all specific to the unittest framework; nothing for py.test.
If I do try the menu items that say "unittest", I get a dialog box with options for "Name", "Type", a "Tests" group box with "Folder" and "Pattern" and "Script" and "Class" and "Function", etc. This sounds exactly like what's documented as the dialog to add a configuration for Python Unit Test, and not like the "Name" and "Test to run" and "Keywords" options that are supposed to show up in the configuration for py.test dialog. There's nothing inside the dialog to switch which test framework I'm adding.
I'm using PyCharm 1.5.2 on Windows with Python 3.1.3 and pytest 2.0.3. I can successfully run py.test
on my tests from the command line, so it's not something simple like pytest not being installed properly.
How do I configure PyCharm to run my py.test tests?
发布评论
评论(15)
请转到文件| 设置 | 工具 | Python 集成工具 并将默认测试运行器更改为 py.test。然后,您将获得 py.test 选项来创建测试,而不是单元测试。
Please go to File| Settings | Tools | Python Integrated Tools and change the default test runner to py.test. Then you'll get the py.test option to create tests instead of the unittest one.
PyCharm 2017.3
首选项 ->工具-> Python 集成工具
- 选择py.test
作为默认测试运行程序
。Preference ->语言和框架 -> Django
- 设置勾选不使用 Django Test runner
运行/调试配置
中清除所有以前存在的测试配置,否则测试将使用那些旧配置运行。运行/调试配置 ->默认值-> Python 测试 -> py.测试->附加参数
PyCharm 2017.3
Preference -> Tools -> Python integrated Tools
- Choosepy.test
asDefault test runner
.Preference -> Languages&Frameworks -> Django
- Set tick onDo not use Django Test runner
Run/Debug configuration
, otherwise tests will be run with those older configurations.Run/Debug Configuration -> Defaults -> Python tests -> py.test -> Additional Arguments
以下是我如何使其与 pytest
3.7.2
(通过 pip 安装)和 pycharms2017.3
配合使用:编辑配置
py.test
target
=python
以及下面的未命名字段到tests
。看起来这是您的测试文件夹的名称。不太确定很难。我还推荐使用-s
参数,这样如果您调试测试,控制台将正常运行。如果没有参数,pytest 会捕获输出并使调试控制台出现错误。my_project/tests
) 的下方。foobar_test.py
文件:(无需导入):Here is how I made it work with pytest
3.7.2
(installed via pip) and pycharms2017.3
:edit configurations
py.test
target
=python
and the unnamed field below totests
. It looks like this is the name of your test folder. Not too sure tough. I also recommend the-s
argument so that if you debug your tests, the console will behave properly. Without the argument pytest captures the output and makes the debug console buggy.my_project/tests
).foobar_test.py
file: (no imports needed):我认为您需要使用工具栏上的运行/调试配置项。单击它并“编辑配置”(或者使用菜单项“运行”->“编辑配置”)。在左窗格的“默认”部分中,有一个“py.test”项目,我认为这就是您想要的。
我还发现手册与用户界面不匹配。希望我正确理解了这个问题,这会有所帮助。
I think you need to use the Run/Debug Configuration item on the toolbar. Click it and 'Edit Configurations' (or alternatively use the menu item Run->Edit Configurations). In the 'Defaults' section in the left pane there is a 'py.test' item which I think is what you want.
I also found that the manual didn't match up to the UI for this. Hope I've understood the problem correctly and that helps.
在 pycharm 2019.2 中,您可以简单地执行以下操作来运行所有测试:
要将 pytest 更好地集成到 pycharm 中,请参阅https://www.jetbrains.com/help/pycharm/pytest.html
In pycharm 2019.2, you can simply do this to run all tests:
For a higher integration of pytest into pycharm, see https://www.jetbrains.com/help/pycharm/pytest.html
可以肯定的是,它的记录很少。从默认值添加新配置后,您将进入运行“/Applications/PyCharm CE.app/Contents/helpers/pycharm/pytestrunner.py”脚本的范围。它没有记录,并且有自己的命令行参数想法。
您可以:
奇怪的是,你会发现很难找到任何讨论,因为 JetBrains 在用自己的页面轰炸 Google 算法方面做得很好。
It's poorly documented to be sure. Once you get add a new configuration from defaults, you will be in the realm of running the "/Applications/PyCharm CE.app/Contents/helpers/pycharm/pytestrunner.py" script. It's not documented and has its own ideas of command line arguments.
You can:
Oddly, you will find it hard to find any discussion as JetBrains does a good job of bombing Google algorithms with its own pages.
当我遇到同样的问题并找到解决方案时找到此线程
pycharm版本:2017.1.2
转到“首选项”-> 「工具」-> “Python Integrated Tools”并将右侧面板中的默认测试运行器设置为 py.test
解决我的问题
find this thread when I hit the same question and found the solution
pycharm version:2017.1.2
go to "Preferences" -> "Tools" -> "Python Integrated Tools" and set the default test runner from right side panel as py.test
solve my problem
给我帮助下一个解决方案:
To me help next solution:
我正在使用 2018.2
我确实运行 ->编辑配置...
然后单击模式对话框左上角的+。
选择“python测试”-> py.测试
然后我给它一个名称,如“All test with py.test”
我选择目标:模块名称
并放入我的测试所在的模块(对我来说就是“测试”),或者如果我的测试与我的代码混合在一起,则放入我所有代码所在的模块。这让我很困惑。
我设置了Python解释器。
我将工作目录设置为项目目录。
I'm using 2018.2
I do Run -> Edit Configurations...
Then click the + in the upper left of the modal dialog.
Select "python tests" -> py.test
Then I give it a name like "All test with py.test"
I select Target: module name
and put in the module where my tests are (that is 'tests' for me) or the module where all my code is if my tests are mixed in with my code. This was tripping me up.
I set the Python interpreter.
I set the working directory to the project directory.
为您的项目启用 Pytest
Enable Pytest for you project
打开首选项窗口(Mac 上为 Command 键 +“,”):
1.工具
2.Python 集成工具
3.默认测试运行器
Open preferences windows (Command key + "," on Mac):
1.Tools
2.Python Integrated Tools
3.Default test runner
使用特殊的 Conda python 设置,其中包括 py.test 的 pip 安装以及 Specs 插件(选项 --spec)的使用(对于像好的测试摘要语言一样的 Rspec),我必须做;
1.编辑默认的 py.test 以包含 option= --spec ,这意味着使用插件: https:// /github.com/pchomik/pytest-spec
2.使用 py.test 创建新的测试配置。更改其 python 解释器以使用 ~/anaconda/envs/ 您选择的解释器,例如 py27 作为我的命名。
3.删除“unittests”测试配置。
4.现在默认的测试配置是 py.test ,带有我可爱的 Rspec 风格输出。我喜欢它!谢谢大家!
ps Jetbrains 关于运行/调试配置的文档位于:https://www.jetbrains.com/help/pycharm/2016.1/run-debug-configuration-py-test.html?search=py.test
With a special Conda python setup which included the pip install for py.test plus usage of the Specs addin (option --spec) (for Rspec like nice test summary language), I had to do ;
1.Edit the default py.test to include option= --spec , which means use the plugin: https://github.com/pchomik/pytest-spec
2.Create new test configuration, using py.test. Change its python interpreter to use ~/anaconda/envs/ your choice of interpreters, eg py27 for my namings.
3.Delete the 'unittests' test configuration.
4.Now the default test config is py.test with my lovely Rspec style outputs. I love it! Thank you everyone!
p.s. Jetbrains' doc on run/debug configs is here: https://www.jetbrains.com/help/pycharm/2016.1/run-debug-configuration-py-test.html?search=py.test
在 2018.3 中,它似乎会自动检测到我正在使用 pytest,这很好,但它仍然不允许从项目的顶层运行。我必须分别为每个
tests
目录运行pytest
。但是,我发现我可以选择其中一种配置并手动编辑它以在项目的根目录下运行,并且这有效。我必须在“配置”下拉列表中手动选择它 - 无法右键单击“项目”窗格中的根文件夹。但至少它允许我立即运行所有测试。
With 2018.3 it appears to automatically detect that I'm using pytest, which is nice, but it still doesn't allow running from the top level of the project. I had to run
pytest
for eachtests
directory individually.However, I found that I could choose one of the configurations and manually edit it to run at the root of the project and that this worked. I have to manually choose it in the Configurations drop-down - can't right click on the root folder in the Project pane. But at least it allows me to run all tests at once.
有一个 PyCharm 文档:运行/调试配置:pytest 自 2020 年 9 月起提供。
There is a PyCharm documentation: Run/Debug Configuration: pytest available as of SEP 2020.
我遇到了另一个案例,但同样的错误
(我无法忽略这里的错误,因为它已经修复了)。
检查您的 venv 中是否确实安装了 pytest。
我在全球范围内安装了 pytest,这让我很困惑。
如果 pip 说 pytest 已安装 - 检查它是什么类型的 pip(全局 pip 或 venv 的 pip)。
which python
应该返回 venv python 的路径。然后 python -m pip install pytest
I came across with a bit another case but the same error
(I can't past the error here cuz already fixed it).
Check that you really have
pytest
installed inside your venv.I have
pytest
installed globally and it's confused me.If pip is saying that pytest is already installed - check what kind of pip it is (global pip or pip of venv).
which python
should return the path to your venv python.Then
python -m pip install pytest