23

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.

0 _
  • 10,524
  • 11
  • 77
  • 109
nikow
  • 21,190
  • 7
  • 49
  • 70

7 Answers7

39

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)
0 _
  • 10,524
  • 11
  • 77
  • 109
nikow
  • 21,190
  • 7
  • 49
  • 70
12

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

Gulzar
  • 23,452
  • 27
  • 113
  • 201
Richard
  • 721
  • 5
  • 16
7

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"])
Nathan
  • 11,814
  • 11
  • 50
  • 93
JJC
  • 9,547
  • 8
  • 48
  • 53
5

For me it was this:

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

For example:

import pytest
pytest.main(["-x", "/api/test", "-vv"])
Akavall
  • 82,592
  • 51
  • 207
  • 251
2

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

Juan Antonio Gomez Moriano
  • 13,103
  • 10
  • 47
  • 65
  • Thanks, for the suggestion. Just recently I had the chance to use PyCharm 2.5, and I agree that it works very well. Right now I'm kind of split between PyCharm and Aptana Studio. – nikow Aug 31 '12 at 07:57
  • @nikow : I started using aptana... i hated it, also, if you get familiar with pycharm you can also go for phpstorm, rubymine, intellij... all these IDEs are from jetbrains, and have the same structure, shortcuts... So it is definately worth. I use to be an eclipse fan, but know jetbrains got me :) – Juan Antonio Gomez Moriano Aug 31 '12 at 07:59
  • +1 for PyCharm . I was using Aptana for PHP but after using PyCharm I am going to gradually switch to JetBrain's products – Kostas Demiris Jan 08 '14 at 11:22
1

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.

Community
  • 1
  • 1
Debilski
  • 66,976
  • 12
  • 110
  • 133
  • Thanks, I was aware of the `--pdb` switch. I think in this case pdb is controlled and started by py.test, which won't work for Eclipse (I want my running Eclipse to take over the debugging). – nikow Jul 27 '10 at 13:14
0

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

citynorman
  • 4,918
  • 3
  • 38
  • 39