简体   繁体   English

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

[英]How do I configure PyCharm to run py.test tests?

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 .我想开始为我的 Python 代码编写单元测试,而py.test框架听起来比 Python 捆绑的unittest更好。 So I added a "tests" directory to my project, and added test_sample.py to it.所以我在我的项目中添加了一个“tests”目录,并在其中添加了test_sample.py Now I want to configure PyCharm to run all the tests in my "tests" directory.现在我想配置 PyCharm 来运行我的“测试”目录中的所有测试。

PyCharm allegedly supports py.test in its test runner. PyCharm 据称在其测试运行程序中 支持 py.test 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 .您应该能够创建一个运行/调试配置来运行您的测试,并且 PyCharm 据称有一个专门用于 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".如果我右键单击项目工具 window 中的目录,我应该会看到“创建 <名称>”菜单项,但唯一以“创建”开头的菜单项是“创建运行配置”。 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.不幸的是,其子菜单中仅有的两项是“C:\mypath... 中的单元测试”和“C:\mypath... 中的 Doctests”,这两个都不适用——我既没有使用单元测试也没有使用 doctest。 There is no menu item for py.test. 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...'".如果我打开我的 test_sample.py 并在编辑器 window 中右键单击,我会得到承诺的“创建 <name>”菜单项:有“在 test_sa 中创建单元测试 ...”...”,然后是“运行'test_sa 中的单元测试 ...'”和“调试 'test_sa 中的单元测试 ...'”。 So again, it's all specific to the unittest framework;再说一遍,这一切都特定于 unittest 框架; nothing for py.test. 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.如果我尝试显示“unittest”的菜单项,我会看到一个对话框,其中包含“名称”、“类型”选项、“测试”组框以及“文件夹”和“模式”以及“脚本”和“类” " 和 "Function" 等。这听起来与记录为对话框添加配置 Python Unit Test的内容完全一样,而不像应该显示的 "Name" 和 "Test to run" 和 "Keywords" 选项在py.test 对话框的配置中 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.我正在使用PyCharm 1.5.2 on ZAEA23489CE3AA9B6406EBBBBBBBBBBBB28E0C28E0C28E0C28E0C28E0C28E0C28E0C28E0C28E0C28E0C28E0C28EF5F55F35426B92727411FC9231B561B563131381381381381381382138138.138.138.138.1138.138.1138.138.138.138.138.138和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.我可以从命令行成功地在我的测试中运行py.test ,所以它不像 pytest 安装不正确这样简单。

How do I configure PyCharm to run my py.test tests?如何配置 PyCharm 来运行我的 py.test 测试?

Please go to File |请转到文件| Settings |设置| Tools |工具| Python Integrated Tools and change the default test runner to py.test. Python 集成工具并将默认测试运行程序更改为 py.test。 Then you'll get the py.test option to create tests instead of the unittest one.然后你会得到 py.test 选项来创建测试而不是 unittest 。

PyCharm 2017.3 PyCharm 2017.3

  1. Preference -> Tools -> Python integrated Tools - Choose py.test as Default test runner . Preference -> Tools -> Python integrated Tools - 选择py.test作为Default test runner
  2. If you use Django Preference -> Languages&Frameworks -> Django - Set tick on Do not use Django Test runner如果您使用 Django Preference -> Languages&Frameworks -> Django - 勾选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.Run/Debug configuration清除所有以前存在的测试配置,否则测试将使用那些旧配置运行。
  4. To set some default additional arguments update py.test default configuration.要设置一些默认的附加参数,请更新 py.test 默认配置。 Run/Debug Configuration -> Defaults -> Python tests -> py.test -> Additional Arguments

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.在左窗格的“默认”部分中,有一个“py.test”项目,我认为这正是您想要的。

I also found that the manual didn't match up to the UI for this.我还发现手册与 UI 不匹配。 Hope I've understood the problem correctly and that helps.希望我已经正确理解了这个问题,这会有所帮助。

Here is how I made it work with pytest 3.7.2 (installed via pip) and pycharms 2017.3 :这是我如何使它与 pytest 3.7.2 (通过 pip 安装)和 pycharms 2017.3

  1. Go to edit configurations转到edit configurations

  1. Add a new run config and select py.test添加新的运行配置并选择py.test

  1. In the run config details, you need to set target = python and the unnamed field below to tests .在运行配置详细信息中,您需要将target = python和下面的未命名字段设置为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.我还建议使用-s参数,这样如果您调试测试,控制台将正常运行。 Without the argument pytest captures the output and makes the debug console buggy.没有参数 pytest 会捕获输出并使调试控制台出错。

  1. My tests folder looks like that.我的测试文件夹看起来像那样。 This is just below the root of my project ( my_project/tests ).这就在我的项目 ( my_project/tests ) 的根目录之下。

  1. My foobar_test.py file: (no imports needed):我的foobar_test.py文件:(不需要导入):
def test_foobar():
    print("hello pytest")
    assert True
  1. Run it with the normal run command使用正常的运行命令运行它

In pycharm 2019.2, you can simply do this to run all tests:在 pycharm 2019.2 中,您可以简单地执行以下操作来运行所有测试:

  1. Run > Edit Configurations > Add pytest运行 > 编辑配置 > 添加 pytest
  2. Set options as shown in following screenshot设置选项,如下面的屏幕截图所示配置截图
  3. Click on Debug (or run pytest using eg hotkeys Shift+Alt+F9)单击调试(或使用热键 Shift+Alt+F9 运行 pytest)

For a higher integration of pytest into pycharm, see https://www.jetbrains.com/help/pycharm/pytest.html pytest 与 pycharm 的更高集成,参见https://www.jetbrains.com/help/pycharm/pytest.html

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.一旦您从默认设置中添加新配置,您将进入运行“/Applications/PyCharm CE.app/Contents/helpers/pycharm/pytestrunner.py”脚本的领域。 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.试着玩转,反转脚本,看看你是否能以某种方式让 py.test 接受参数。 It might work;它可能会起作用; it didn't in the first half hour for me.对我来说,前半个小时没有。
  2. Just run "py.test *.py" from a console.只需从控制台运行“py.test *.py”。

Oddly, you will find it hard to find any discussion as JetBrains does a good job of bombing Google algorithms with its own pages.奇怪的是,您会发现很难找到任何讨论,因为 JetBrains 在用自己的页面轰炸 Google 算法方面做得很好。

当我遇到同样的问题并找到解决方案 pycharm 版本时找到此线程:2017.1.2 转到“首选项”->“工具”->“Python 集成工具”并将右侧面板中的默认测试运行程序设置为 py.test解决我的问题

I'm using 2018.2我正在使用 2018.2

I do Run -> Edit Configurations... Then click the + in the upper left of the modal dialog.我执行 Run -> Edit Configurations... 然后单击模式对话框左上角的 +。 Select "python tests" -> py.test Then I give it a name like "All test with py.test"选择“python 测试”-> py.test 然后我给它一个名字,比如“所有测试与 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.我设置了 Python 解释器。

I set the working directory to the project directory.我将工作目录设置为项目目录。

Enable Pytest for you project为您的项目启用 Pytest

  1. Open the Settings/Preferences |打开设置/首选项 | Tools |工具 | Python Integrated Tools settings dialog as described in Choosing Your Testing Framework. Python 集成工具设置对话框,如选择您的测试框架中所述。
  2. In the Default test runner field select pytest .Default test runner字段中选择pytest
  3. Click OK to save the settings.单击确定以保存设置。

在此处输入图片说明

Open preferences windows (Command key + "," on Mac):打开首选项窗口(Mac 上的 Command 键 +“,”):

Python 首选项链接

1.Tools 1.工具

2.Python Integrated Tools 2.Python集成工具

3.Default test runner 3.默认测试运行器

python 默认测试运行器

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 ;使用特殊的 Conda python 设置,其中包括 py.test 的 pip 安装以及 Specs 插件(选项 --spec)的使用(对于 Rspec 之类的不错的测试摘要语言),我必须这样做;

1.Edit the default py.test to include option= --spec , which means use the plugin: https://github.com/pchomik/pytest-spec 1.编辑默认的 py.test 以包含 option= --spec ,这意味着使用插件: https : //github.com/pchomik/pytest-spec

2.Create new test configuration, using py.test. 2.创建新的测试配置,使用py.test。 Change its python interpreter to use ~/anaconda/envs/ your choice of interpreters, eg py27 for my namings.更改其 python 解释器以使用 ~/anaconda/envs/ 您选择的解释器,例如 py27 用于我的命名。

3.Delete the 'unittests' test configuration. 3.删除'unittests'测试配置。

4.Now the default test config is py.test with my lovely Rspec style outputs. 4.现在默认的测试配置是 py.test 和我可爱的 ​​Rspec 风格的输出。 I love it!我喜欢它! Thank you everyone!谢谢大家!

ps 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 ps Jetbrains 关于运行/调试配置的文档在这里: https ://www.jetbrains.com/help/pycharm/2016.1/run-debug-configuration-py-test.html?search = py.test

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.在 2018.3 中,它似乎自动检测到我正在使用 pytest,这很好,但它仍然不允许从项目的顶层运行。 I had to run pytest for each tests directory individually.我必须pytest为每个tests目录运行pytest

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.但至少它允许我一次运行所有测试。

There is a PyCharm documentation: Run/Debug Configuration: pytest available as of SEP 2020.有一个 PyCharm 文档: 运行/调试配置:pytest可用于 2020 年 SEP。

在此处输入图片说明

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.检查您的 venv 中是否确实安装了pytest I have pytest installed globally and it's confused me.我在全球范围内安装了pytest ,这让我很困惑。

If pip is saying that pytest is already installed - check what kind of pip it is (global pip or pip of venv). 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. which python应该返回到您的 venv python 的路径。 Then python -m pip install pytest然后python -m pip install pytest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM