In eclipse, i would like to know how to configure debug for single function.
I use pytest interpreter.
In my file 'hello.py', i have several function
How to make this without using console:
pytest hello.py::function
Related
In PyCharm, it is possible to set a script that runs upon opening a new console (through Settings -> 'Build, Execution, Deployment' -> Console -> Python Console -> Starting script).
Is there a way to similarly apply a startup script to the debugger console? I find myself importing the same packages over and over again, each time I run the code.
When you run Python Console inside PyCharm, it executes custom PyCharm script at <PYCHARM_PATH>/plugins/python/helpers/pydev/pydevconsole.py.
On the other hand, when you run PyCharm Debug Console while debugging, it executes custom PyCharm script at <PYCHARM_PATH>/Plugins/python/helpers/pydev/pydevd.py with command line parameter --file set to script you are debugging.
You can modify pydevd.py file if you want (Apache 2 license), but the easier approach would be to create startup script in which you import modules you need, functions and such and import ALL while inside PyCharm Debug Console. This would reduce all your imports to one.
Walkthrough:
Let's create 2 files:
main.py - Our main script which we will debug
startup.py - Modules, functions or something else that we would like to import.
main.py content:
sentence = 'Hello Debugger'
def replace_spaces_with_hyphens(s):
return s.replace(' ', '-')
replace_spaces_with_hyphens(sentence) # <- PLACE BREAKPOINT!
When breakpoint is hit, this is what we have inside scope:
If you always find yourself importing some modules and creating some functions, you can define all that inside startup.py script and import everything as from startup import *.
startup.py:
# Example modules you always find yourself importing.
import random
import time
# Some function you always create because you need it.
def my_imported_function():
print("Imported !")
Inside Python Debugger Console, use from startup import * as mentioned above and you would see all modules and function inside scope, ready for use.
you could just create a new debug configuration (run > edit configurations) and point it to a script in your project (e.g. called debug.py that you gitignore). Then when you hit debug it will run that script and drop you into a console.
Personally, I prefer to just launch ipython in the embedded terminal than using the debug console. On linux, you can create a bash alias in your .bashrc such as alias debug_myproject=PYTHONSTARTUP=$HOME/myproject/debug.py ipython. Then calling debug_myproject will run that script and drop you into an ipython console.
I'm trying to run pytest from within a script, so I want it to run all test files by simply invoking pytest with no arguments. However, pytest doesn't collect the testfiles.
Hirarchy is as follows
project
-somedirectory
--somecode.py
-tests
--test.py
test.py look as follows
def test_one():
assert True
Running pytest via the PyCharm GUI works as expected, test.py is collected, run and passed.
Running pytest tests/test.py works as expected, test.py is collected, run and passed.
Running pytest without arguments does not work as expected, it doesn't collect test.py.
Running pwd returns /path/to/project.
The behaviour is the same for Windows command prompt and the PyCharm terminal.
By default pytest looks for files with the pattern "test_*.py", your file "test.py" doesn't match this pattern so is likely being ignored. Try renaming it to something else? "test_foo.py" for example
This works. I did not realise the naming convention related to files as well as functions.
When I use Java with JUnit in Eclipse, I can highlight a method name in a unit test file and run only that single test. Is something like this available in PyDev? I don't want to run all my tests in a file all the time, only a single one.
In PyDev you must do Ctrl+F9 and then select the test to run.
Also, it may be nice reading: http://pydev.org/manual_101_run.html as it gives some more hints on running modules within PyDev.
I'm using PyDev ( with Aptana ) to write and debug a Python Pylons app, and I'd like to step through the tests in the debugger.
Is it possible to launch nosetests through PyDev and stop at breakpoints?
Here is what i do to run nosetests using eclipse Pydev (Hope this will help you).
first of all i create a python script and i put it in the root of my package directory :
--Package
|
| -- runtest.py
|
| -- ... (others modules)
and in runtest.py i put:
import nose
nose.main()
now i go to in the menu Run -> Run configurations and i create a new configuration of Pydev Django i choose my package and put runtest.py in the main Module , next i go to arguments tab in the same widget and i put in Program arguments the path to my project and different arg to pass to the script example:
/home/me/projects/src --with-doctest # Run doctests too
now after clicking on Apply i can run this configuration .
For debugging you can run this configuration in debug mode and put your break point anywhere in your code and you can use the terrific debug widget to do several action : step into, to see vars ...
N.B : for doctests sadly i don't think you can put breakpoint in the line of doctest but what you can do is to put your breakpoint in the def of the function that is called by the doctest and like that you can use the debug mode .
Try import pydevd; pydevd.settrace() where would like a breakpoint.
I got this working, somewhat - that is, I don't have breakpoints and stepping working but I do get PyDev to run the tests and show the results in the PyUnit view.
When you run the unit test you'll have to override the test runner to use "nose" and command line arguments "--with-pylons=path/to/test.ini" in the arguments tab of the run configuration. For example I set it to "--with-pylons=../../test.ini". Unfortunately I have to set this up separately for each test I run, I haven't found a way to put a variable or project path in there.
Also, unfortunately, I haven't been able to get breakpoints working. I tried patching as recommended in http://pydev.blogspot.ca/2007/06/why-cant-pydev-debugger-work-with.html and its comments to no avail. YMMV.
In DecoratorTools-1.8-py2.7.egg/peak/util/decorators.py in decorate_assignment(), replace:
oldtrace = [frame.f_trace]
with
oldtrace = [sys.gettrace()]
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.
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)
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
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"])
For me it was this:
pytest.main(["-x", "path to test file", "args"])
For example:
import pytest
pytest.main(["-x", "/api/test", "-vv"])
Maybe you could give a try to pycharm it has direct integration with py.test (I use it at work) and debugger runs perfectly.
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.
You can just run py.test --pdb if you just want to a debugger and don't need the IDE