本文介绍了如何使用 Python 中的`pytest`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个最近切换到 pytest 的项目中工作a> unittest 框架.我习惯于从 Eclipse 调用我的测试,以便我可以使用调试器(例如,放置断点来分析测试失败的发展方式).现在这不再可能,因为运行测试的唯一方法是通过命令行黑盒.

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.

有什么方法可以在 Python 中使用 pytest,这样就不会被迫退出 IDE?测试当然不应在单独的进程中运行.

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.

推荐答案

我想我现在可以回答我自己的问题了,很简单:

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

import pytest

pytest.main(args)

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

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 是命令行参数的列表,所以例如只运行特定的测试我可以使用类似的东西:

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)

这篇关于如何使用 Python 中的`pytest`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 20:30
查看更多