如何在 Python 中使用“pytest”?
我正在从事的项目最近切换到 pytest
unittest
框架。我习惯于从 Eclipse 调用我的测试,以便我可以使用调试器(例如放置断点来分析测试失败是如何发展的)。现在这不再可能了,因为运行测试的唯一方法是通过命令行黑盒。
有没有某种方法可以在 Python 中使用 pytest,这样就不会被迫退出 IDE?当然,测试不应该在单独的进程中运行。
I'm working in a project that recently switched to the pytest
unittest
framework. I was used to calling my tests from Eclipse, so that I can use the debugger (e.g. placing breakpoints to analyze how a test failure develops). Now this is no longer possible, since the only way to run the tests is via the command line blackbox.
Is there some way to use pytest
from within Python, so that one is not forced to drop out of the IDE? The tests should of course not be run in a separate process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我想我现在可以回答我自己的问题,这非常简单:
这在 "从 Python 代码调用 pytest"。
然后我可以运行这个模块和/或使用集成调试器启动它。
args 是命令行参数列表,因此例如要仅运行特定测试,我可以使用类似以下内容的内容:
I think I can now answer my own question, it's pretty simple:
which is documented in the Section "Calling pytest from Python code".
Then I can run this module and/or start it with the integrated debugger.
args
is the list of command-line arguments, so for example to run only particular tests I can use something like:看来现在(py.test版本2.0+)有人也可以这样做:
参考
It seems that now (py.test version 2.0+) someone can also do this :
Ref
现在 pytest 支持这一点,并在 文档。
This is now supported by pytest and described nicely in the documentation.
对我来说是这样的:
例如:
For me it was this:
For example:
也许你可以尝试 pycharm 它与 py.test 直接集成(我在工作中使用它)并且调试器运行完美。
Maybe you could give a try to pycharm it has direct integration with py.test (I use it at work) and debugger runs perfectly.
我没有尝试过使用 Eclipse,但正如
但是,调用标准
import pdb;pdb.set_trace()
不会直接调用调试器。首先它会发出一个错误,然后激活调试器。这可能会也可能不会使事情以不同的方式进行。I have not tried with eclipse, but as was suggested in a related question, it is possible to use the
--pdb
command line option withpy.test
. Maybe it is possible to configure eclipse that way.However, calling the standard
import pdb;pdb.set_trace()
will not directly call the debugger. First it will issue an error which in turn will activate the debugger. This might or might not make things work differently.如果您只想使用调试器并且不需要 IDE,则只需运行 py.test --pdb
You can just run
py.test --pdb
if you just want to a debugger and don't need the IDE