如何配置PyCharm以运行py.test测试?

问题:如何配置PyCharm以运行py.test测试?

我想开始为我的Python代码编写单元测试,而py.test框架听起来比Python捆绑的unittest更好。因此,我向项目添加了“ tests”目录,并向其添加了test_sample.py。现在,我想配置PyCharm以运行“ tests”目录中的所有测试。

据称,PyCharm 在其测试运行程序支持py.test。您应该能够创建运行/调试配置来运行测试,并且PyCharm据称具有专门用于py.test“创建配置”对话框。但这是他们关于该主题的文档的完整范围,我无法在任何地方找到这个所谓的对话框。

如果在“项目”工具窗口中右键单击目录,则应该看到“创建<名称>”菜单项,但是唯一以“创建”开头的菜单项是“创建运行配置”。好的,也许文档只是错误的,“创建运行配置”听起来确实很有希望。不幸的是,其子菜单中仅有的两项是“ C:\ mypath …中的Unittests”和“ C:\ mypath …中的Doctests”,都不适用-我既未使用unittest也未使用doctest。py.test没有菜单项。

如果我打开test_sample.py并在编辑器窗口中单击鼠标右键,则会得到承诺的“创建<名称>”菜单项:有“在test_sa中创建’Unittests …’…”,然后是“运行” “ test_sa中的单元测试…”和“调试test_sa中的单元测试…”。同样,这都是单元测试框架所特有的。py.test没有。

如果我确实尝试显示“ unittest”的菜单项,则会出现一个对话框,其中包含“名称”,“类型”,带有“文件夹”,“模式”,“脚本”和“类”的“测试”组框”和“函数”等。这听起来与为Python单元测试添加配置的对话框所记录的内容完全不同,而不像应该显示的“名称”和“运行测试”以及“关键字”选项一样在py.test对话框的配置中。对话框中没有任何内容可以切换我要添加的测试框架。

我在Windows上使用Python 3.1.3和pytest 2.0.3的PyCharm 1.5.2。我可以从命令行成功运行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?


回答 0

请转到文件| 设置| 工具| Python Integrated Tools,并将默认测试运行程序更改为py.test。然后,您将获得py.test选项来创建测试,而不是unittest。

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.


回答 1

PyCharm 2017.3

  1. Preference -> Tools -> Python integrated Tools-选择py.testDefault test runner
  2. 如果您使用Django- Preference -> Languages&Frameworks -> Django勾选Do not use Django Test runner
  3. 从中清除所有以前存在的测试配置Run/Debug configuration,否则将使用这些较旧的配置运行测试。
  4. 要设置一些默认的其他参数,请更新py.test默认配置。 Run/Debug Configuration -> Defaults -> Python tests -> py.test -> Additional Arguments

PyCharm 2017.3

  1. Preference -> Tools -> Python integrated Tools – Choose py.test as Default test runner.
  2. If you use Django Preference -> Languages&Frameworks -> Django – Set tick on Do not use Django Test runner
  3. Clear all previously existing test configurations from Run/Debug configuration, otherwise tests will be run with those older configurations.
  4. To set some default additional arguments update py.test default configuration. Run/Debug Configuration -> Defaults -> Python tests -> py.test -> Additional Arguments

回答 2

我认为您需要使用工具栏上的“运行/调试配置”项。单击它并单击“编辑配置”(或使用菜单项“运行”->“编辑配置”)。在左侧窗格的“默认”部分中,有一个“ 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.


回答 3

这是我使它与pytest 3.7.2(通过pip安装)和pycharms一起工作的方式2017.3

  1. edit configurations

  1. 添加一个新的运行配置并选择 py.test

  1. 在运行配置详细信息中,您需要将target= python和下面的未命名字段设置为tests。看起来这是您的测试文件夹的名称。不太确定强硬。我还建议使用该-s参数,以便在调试测试时,控制台将正常运行。没有参数pytest会捕获输出,并使调试控制台出错。

  1. 我的测试文件夹如下所示。这刚好在我的项目(my_project/tests)的根目录之下。

  1. 我的foobar_test.py文件:(无需导入):
def test_foobar():
    打印(“ Hello pytest”)
    断言为真
  1. 使用常规运行命令运行它

Here is how I made it work with pytest 3.7.2 (installed via pip) and pycharms 2017.3:

  1. Go to edit configurations

  1. Add a new run config and select py.test

  1. In the run config details, you need to set target=python and the unnamed field below to tests. 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.

  1. My tests folder looks like that. This is just below the root of my project (my_project/tests).

  1. My foobar_test.py file: (no imports needed):
def test_foobar():
    print("hello pytest")
    assert True
  1. Run it with the normal run command


回答 4

可以肯定地说,它的文献不多。从默认设置添加新配置后,您将可以运行“ / Applications / PyCharm CE.app/Contents/helpers/pycharm/pytestrunner.py”脚本。它没有记录,并且有自己的命令行参数构想。

您可以:

  1. 尝试玩一下,反转脚本,看看是否可以使py.test接受参数。它可能会起作用;上半个小时对我来说不是。
  2. 只需从控制台运行“ py.test * .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:

  1. Try to play around, reverse the script, and see if you can somehow get py.test to accept arguments. It might work; it didn’t in the first half hour for me.
  2. Just run “py.test *.py” from a console.

Oddly, you will find it hard to find any discussion as JetBrains does a good job of bombing Google algorithms with its own pages.


回答 5

当我遇到相同的问题并找到解决方案pycharm版本时找到此线程:2017.1.2转到“首选项”->“工具”->“ Python集成工具”,然后从右侧面板中将默认测试运行器设置为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


回答 6

在pycharm 2019.2中,您可以简单地执行以下操作以运行所有测试:

  1. 运行>编辑配置>添加pytest
  2. 设置选项,如以下屏幕截图所示
  3. 单击调试(或使用快捷键Shift + Alt + F9运行pytest)

有关pytest与pycharm的更高集成,请参阅https://www.jetbrains.com/help/pycharm/pytest.html

In pycharm 2019.2, you can simply do this to run all tests:

  1. Run > Edit Configurations > Add pytest
  2. Set options as shown in following screenshot
  3. Click on Debug (or run pytest using e.g. hotkeys Shift+Alt+F9)

For a higher integration of pytest into pycharm, see https://www.jetbrains.com/help/pycharm/pytest.html


回答 7

我正在使用2018.2

我运行->编辑配置…然后单击模式对话框左上方的+。选择“ python测试”-> py.test然后给它一个名称,例如“使用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.


回答 8

使用特殊的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


回答 9

在2018.3中,它似乎会自动检测到我正在使用pytest,这很好,但是仍然不允许从项目的顶层运行。我必须分别pytest为每个tests目录运行。

但是,我发现我可以选择一种配置,然后手动对其进行编辑以在项目的根目录下运行,并且可以正常工作。我必须在“配置”下拉列表中手动选择它-不能右键单击“项目”窗格中的根文件夹。但是至少它允许我同时运行所有测试。

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 each tests 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.


回答 10

为您的项目启用Pytest

  1. 打开设置/首选项| 工具| 如选择测试框架中所述的Python Integrated Tools设置对话框。
  2. 默认测试运行器字段中,选择pytest
  3. 单击确定以保存设置。

Enable Pytest for you project

  1. Open the Settings/Preferences | Tools | Python Integrated Tools settings dialog as described in Choosing Your Testing Framework.
  2. In the Default test runner field select pytest.
  3. Click OK to save the settings.