如何配置 PyCharm 以运行 py.test 测试? [英] How do I configure PyCharm to run py.test tests?

查看:101
本文介绍了如何配置 PyCharm 以运行 py.test 测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开始为我的 Python 代码编写单元测试,py.test 框架听起来是一个更好的选择比 Python 捆绑的 unittest.所以我在我的项目中添加了一个tests"目录,并添加了 test_sample.py 到它.现在我想配置 PyCharm 来运行我的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. 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 据称 在其测试运行器中支持 py.test.你应该能够创建一个运行/debug configuration 来运行你的测试,据称 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.

如果我右键单击项目"工具窗口中的目录,我是 supposed 看到一个Create "菜单项,但唯一以创建"开头的菜单项是创建运行配置".好吧,也许文档是错误的,创建运行配置"听起来很有希望.不幸的是,它的子菜单中只有两个项目是C:\mypath...中的单元测试"和C:\mypath...中的文档测试",这两个都不适用——我既不使用 unittest 也不使用 doctest.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.

如果我打开我的 test_sample.py 并在编辑器窗口中右键单击,我确实得到了承诺的创建 <名称>"菜单项:有在 test_sa 中创建'Unittests...'...",然后是在 test_sa 中运行'Unittests...'"和在 test_sa...中调试'Unittests...'".再说一次,这都是针对 unittest 框架的;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.

如果我尝试使用unittest"菜单项,我会得到一个对话框,其中包含名称"、类型"选项、测试"组框和文件夹"、模式"和脚本"和类"和函数"等.这听起来与添加Python 单元测试的配置,而不是应该出现在 中的名称"和要运行的测试"和关键字"选项href="http://www.jetbrains.com/pycharm/webhelp/run-debug-configuration-py-test.html" rel="noreferrer">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.

我在 Windows 上使用 PyCharm 1.5.2 和 Python 3.1.3 和 pytest 2.0.3.我可以从命令行成功地在我的测试中运行 py.test,所以这不像 pytest 没有正确安装那样简单.

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.

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

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

推荐答案

请到文件|设置 |工具 |Python 集成工具 并将默认测试运行程序更改为 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.

这篇关于如何配置 PyCharm 以运行 py.test 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆