如何在 Python 中使用“pytest”?

发布于 2024-09-11 13:23:09 字数 360 浏览 14 评论 0原文

我正在从事的项目最近切换到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

不必在意 2024-09-18 13:23:10

我想我现在可以回答我自己的问题,这非常简单:

import pytest

pytest.main(args)

这在 "从 Python 代码调用 pytest"
然后我可以运行这个模块和/或使用集成调试器启动它。

args 是命令行参数列表,因此例如要仅运行特定测试,我可以使用类似以下内容的内容:

args_str = "-k test_myfavorite"
args = args_str.split(" ")
pytest.main(args)

I think I can now answer my own question, it's pretty simple:

import pytest

pytest.main(args)

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:

args_str = "-k test_myfavorite"
args = args_str.split(" ")
pytest.main(args)
一曲爱恨情仇 2024-09-18 13:23:10

看来现在(py.test版本2.0+)有人也可以这样做:

import pytest

pytest.main('-x {0}'.format(argument))

# Or
# pytest.main(['-x', 'argument'])

参考

It seems that now (py.test version 2.0+) someone can also do this :

import pytest

pytest.main('-x {0}'.format(argument))

# Or
# pytest.main(['-x', 'argument'])

Ref

青朷 2024-09-18 13:23:10

现在 pytest 支持这一点,并在 文档

您可以直接从 Python 代码调用 pytest:

导入 pytest
pytest.main()

这就像您从命令行调用“pytest”一样。它不会引发 SystemExit,而是返回退出代码。您可以传入选项和参数:

pytest.main(["-x", "mytestdir"])

This is now supported by pytest and described nicely in the documentation.

You can invoke pytest from Python code directly:

import pytest
pytest.main()

this acts as if you would call “pytest” from the command line. It will not raise SystemExit but return the exitcode instead. You can pass in options and arguments:

pytest.main(["-x", "mytestdir"])
蓝眼睛不忧郁 2024-09-18 13:23:10

对我来说是这样的:

pytest.main(["-x", "path to test file", "args"])

例如:

import pytest
pytest.main(["-x", "/api/test", "-vv"])

For me it was this:

pytest.main(["-x", "path to test file", "args"])

For example:

import pytest
pytest.main(["-x", "/api/test", "-vv"])
沫离伤花 2024-09-18 13:23:10

也许你可以尝试 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.

起风了 2024-09-18 13:23:10

我没有尝试过使用 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 with py.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.

小红帽 2024-09-18 13:23:10

如果您只想使用调试器并且不需要 IDE,则只需运行 py.test --pdb

You can just run py.test --pdb if you just want to a debugger and don't need the IDE

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文