How to use py.test from Python?

ぐ巨炮叔叔 提交于 2019-12-03 02:04:41

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

import py
py.test.cmdline.main(args)

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"
py.test.cmdline.main(args_str.split(" "))

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 : https://pytest.org/latest/usage.html#calling-pytest-from-python-code

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

Debilski

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.

Maybe you could give a try to pycharm it has direct integration with py.test (I use it at work) and debugger runs perfectly.

For me it was this:

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

For example:

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!