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

时间:2021-01-02 01:20:31

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.

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

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.

PyCharm据称支持py。在测试运行器中进行测试。您应该能够创建运行/调试配置来运行您的测试,并且PyCharm有一个专门针对py.test的“创建配置”对话框。但这是他们关于这个主题的完整的文档,我在任何地方都找不到这个所谓的对话框。

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.

如果我右键单击Project tool窗口中的目录,我应该会看到一个“Create ”菜单项,但是唯一以“Create”开头的菜单项是“Create Run Configuration”。好吧,也许文档是错误的,“创建运行配置”听起来很有希望。不幸的是,它的子菜单中仅有的两个项目是“C:\mypath中的unittest”和“C:\mypath中的Doctests…”,这两个都不适用,我既没有使用unittest也没有使用doctest。没有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.

如果我打开test_sample。在编辑器窗口中单击py,我确实得到了承诺的“创建 ”菜单项:test_sa中有“创建”Unittests……,然后在test_sa中“运行”unittest…'和' Debug 'Unittests in test_sa…'。同样,它都是针对unittest框架的;没有对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.

如果我试着说“unittest”的菜单项,我得到一个对话框,选择“名称”、“类型”、“测试”组框与“文件夹”和“模式”和“脚本”和“类”和“功能”,等等。这听起来完全一样的记录作为Python的对话框添加一个配置单元测试,而不是像“Name”和“测试运行”和“关键词”选项,应该出现在py的配置。测试对话框。对话框中没有什么可以切换我添加的测试框架。

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.

在使用Python 3.1.3和pytest 2.0.3的Windows上使用PyCharm 1.5.2。我可以成功运行py。从命令行测试我的测试,所以它不是简单的像pytest没有正确安装。

How do I configure PyCharm to run my py.test tests?

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

6 个解决方案

#1


165  

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.

请访问|设置|工具| Python集成工具,并将默认测试运行器更改为py.test。然后你会得到py。创建测试的测试选项,而不是unittest。

#2


15  

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.

我认为您需要使用工具栏上的Run/Debug配置项。单击它并“编辑配置”(或者使用菜单项Run->编辑配置)。在左边窗格的“default”部分中,有一个“py”。测试项目,我认为这是你想要的。

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.

我还发现手册与UI并不匹配。希望我已经正确地理解了这个问题,这对我很有帮助。

#3


13  

PyCharm 2017.3

PyCharm 2017.3

  1. Preference -> Tools -> Python integrated Tools - Choose py.test as Default test runner.
  2. 首选项->工具-> Python集成工具-选择py。作为默认测试运行器进行测试。
  3. If you use Django Preference -> Languages&Frameworks -> Django - Set tick on Do not use Django Test runner
  4. 如果您使用Django首选项—>语言和框架—> Django—设置勾,则不要使用Django测试运行器
  5. Clear all previous runned tests from Run/Debug configuration, otherwise tests will be runned with that configurations.
  6. 从运行/调试配置中清除所有以前运行的测试,否则将使用该配置运行测试。
  7. To set some default additional arguments update py.test default configuration. Run/Debug Configuration -> Defaults -> Python tests -> py.test -> Additional Arguments
  8. 要设置一些默认的附加参数,请更新py。测试默认配置。运行/调试配置->默认值-> Python测试-> py。测试- >附加参数

#4


5  

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.

确切地说,它的文档记录很糟糕。一旦您从默认设置中添加了新的配置,您就可以运行“/ application /PyCharm .app/Contents/helper /PyCharm /pytestrunner”了。py脚本”。它没有文档记录,并且有自己的命令行参数思想。

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. 试着修改一下脚本,看看能不能找到py。测试接受参数。它可能工作;对我来说,前半个小时还没有。
  3. Just run "py.test *.py" from a console.
  4. py只运行”。测试*。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用自己的页面很好地轰炸了谷歌算法。

#5


2  

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

当我遇到同样的问题并找到解决方案pycharm版本:2017.1.2进入“Preferences”->“Tools”->“Python integration Tools”时,并将右侧面板的默认测试运行器设置为py。测试解决我的问题

#6


0  

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安装pip。测试加上spec插件(选项—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。测试包含选项=——spec,这意味着使用插件: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.

2。使用py.test创建新的测试配置。将它的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. I love it! Thank you everyone!

4所示。现在默认的测试配置是py。用我可爱的Rspec样式输出进行测试。我爱它!谢谢大家!

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

Jetbrains的doc运行/调试配置如下:https://www.jetbrains.com/help/pycharm/2016.1/rundebug -py-test.html?

#1


165  

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.

请访问|设置|工具| Python集成工具,并将默认测试运行器更改为py.test。然后你会得到py。创建测试的测试选项,而不是unittest。

#2


15  

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.

我认为您需要使用工具栏上的Run/Debug配置项。单击它并“编辑配置”(或者使用菜单项Run->编辑配置)。在左边窗格的“default”部分中,有一个“py”。测试项目,我认为这是你想要的。

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.

我还发现手册与UI并不匹配。希望我已经正确地理解了这个问题,这对我很有帮助。

#3


13  

PyCharm 2017.3

PyCharm 2017.3

  1. Preference -> Tools -> Python integrated Tools - Choose py.test as Default test runner.
  2. 首选项->工具-> Python集成工具-选择py。作为默认测试运行器进行测试。
  3. If you use Django Preference -> Languages&Frameworks -> Django - Set tick on Do not use Django Test runner
  4. 如果您使用Django首选项—>语言和框架—> Django—设置勾,则不要使用Django测试运行器
  5. Clear all previous runned tests from Run/Debug configuration, otherwise tests will be runned with that configurations.
  6. 从运行/调试配置中清除所有以前运行的测试,否则将使用该配置运行测试。
  7. To set some default additional arguments update py.test default configuration. Run/Debug Configuration -> Defaults -> Python tests -> py.test -> Additional Arguments
  8. 要设置一些默认的附加参数,请更新py。测试默认配置。运行/调试配置->默认值-> Python测试-> py。测试- >附加参数

#4


5  

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.

确切地说,它的文档记录很糟糕。一旦您从默认设置中添加了新的配置,您就可以运行“/ application /PyCharm .app/Contents/helper /PyCharm /pytestrunner”了。py脚本”。它没有文档记录,并且有自己的命令行参数思想。

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. 试着修改一下脚本,看看能不能找到py。测试接受参数。它可能工作;对我来说,前半个小时还没有。
  3. Just run "py.test *.py" from a console.
  4. py只运行”。测试*。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用自己的页面很好地轰炸了谷歌算法。

#5


2  

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

当我遇到同样的问题并找到解决方案pycharm版本:2017.1.2进入“Preferences”->“Tools”->“Python integration Tools”时,并将右侧面板的默认测试运行器设置为py。测试解决我的问题

#6


0  

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安装pip。测试加上spec插件(选项—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。测试包含选项=——spec,这意味着使用插件: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.

2。使用py.test创建新的测试配置。将它的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. I love it! Thank you everyone!

4所示。现在默认的测试配置是py。用我可爱的Rspec样式输出进行测试。我爱它!谢谢大家!

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

Jetbrains的doc运行/调试配置如下:https://www.jetbrains.com/help/pycharm/2016.1/rundebug -py-test.html?