I use nosetests for unit testing so as team, developing IPython. There are tests for built-in magic functions https://github.com/ipython/ipython/blob/master/IPython/core/tests/test_magic.py
The problem is that writing tests as usual, when I try to initiate IPython with something like
from IPython import get_ipython
ip = get_ipython()
ip.register_magics(MyMagic)
nosetests fail with error that 'NoneType' object has no attribute 'register_magics'. This is because get_ipython() returns None.
In IPython tests some custom plugins solve the problem. Command iptest runs IPython tests, including magics. The problem is I don't understand how exactly should I run test, or where the plugin is located and how to modify it for custom magic testing. Any help will be appreciated
P.S.: I also tried to use InteractiveShellEmbed(), but it does not work as expected.
For now this may be done by calling from terminal:
PYTHONPATH=<path/to/folder/with/test> iptest <file_with_tests_without_py>
I saw this question: Can I debug with python debugger when using py.test somehow? but it doesn't really help, because I need to debug hooks, some of them not written by me, where modifying the code of the hook is really cumbersome.
Also, pytest runs through pipenv run. It's already difficult to make them both work together. I couldn't so far find a combination of pdb, pipenv and pytest that would launch each other.
Another way I could do it is by calling pytest.main() from my code, however, this means that other people who want to run my tests will have to use this "trampoline" to run other tests. I can live with this, but it still feels like it shouldn't be necessary.
I guess this is what you need, invoke pdb as early as possible:
`pipenv --py` -c 'import pdb, pytest; pdb.set_trace(); pytest.main()'
I created a Python library and a set of Python script around it. An example of this small script could be something like this rna_ex2x.py:
./rna_ec2x.py
usage: rna_ec2x.py [-h] [--sep SEP] [--chain CHAIN] [--ec-pairs]
[--ss-pairs SS_PAIRS] [--pairs-delta]
interaction_fn
rna_ec2x.py: error: too few arguments
I want to test these script with pytest. I know how to test my functions with pytest, but I can't find in the documentation what would be the best practice in testing standalone Python script. Any suggestions?
I don't know what would be the best practice but I simply call my programs using subprocess.call(), check the result code and verify that the program did what it intended to do. See my tests as examples.
nosetests --pdb let's me halt upon error or failure, but this is too late for my needs. Stepping through code during execution helps me debug where the problem is.
However, nosetests are helpful as they allow tests that rely on relative imports (i.e. tests in a package).
How can I set breakpoints before the tests are executed?
Currently I'm using:
python -m pdb /path/to/my/nosetests testfile.py
This solution isn't adequate. Nosetests interfere with pdb output, and my keyboard controls (e.g. arrow keys) are broken.
Using import pdb; pdb.set_trace() would seem like a good idea, however nosetests is blocking my access to the pdb console.
Even better than remembering to use -s is to use the set_trace variant that comes with Nose. Add
from nose.tools import set_trace; set_trace()
wherever you'd like to break in to the debugger. The stdin/out redirection will be taken care of for you. The only strange side effect I've run into is the inability to restart your code from within pdb (using run) while debugging during a nose run.
You can add
import pdb; pdb.set_trace()
anywhere in your source that you want to stop in the debugger.
Make sure you pass -s to nose so that it does not capture stdout.
If you have ipython, for unlimited awesomeness use:
import ipdb; ipdb.set_trace()
*unlimited awesomeness: just like ipython - auto-completion, coloring etc.
If you are using pytest, you can use
import pytest; pytest.set_trace()
See documentation.
Are there any ways to debug python scripts not leaving vim in *nix systems (executing the script, setting up breakpoints, showing variables in watch-list, etc)?
Use pdb:
import pdb
def main():
list = [1,2,3]
pdb.set_trace()
list = [2,3,4]
if __name__ == '__main__':
main()
Now run using :!python % and you'll hit your breakpoint and be able to debug interactively like in gdb.
As of Python 3.7, you can use breakpoint() builtin without importing anything.
Built-in breakpoint() calls sys.breakpointhook(). By default, the latter imports pdb and then calls pdb.set_trace()
Inheriting code from Pierre-Antoine's answer, the code would look like this:
def main():
list = [1,2,3]
breakpoint()
list = [2,3,4]
if __name__ == '__main__':
main()
Source: https://docs.python.org/3/whatsnew/3.7.html#pep-553-built-in-breakpoint
Try pyclewn. It allows to use vim as front end for pdb. You can create/delete break points, control flow of debugging process, look at values of your variables. All from vim!
Also try https://pypi.python.org/pypi/pudb - its like pdb but more advanced. Contains code highlighting, stack, showing avaliable values, etc. Not only-vim solution but for me works perfectly.
Three Steps:
Install:
pip install pudb
Paste set_trace in code
from pudb import set_trace; set_trace()
Run your code
As 2020 the Debugger Adapter Protocol is taken care by vimspector.
Supporting Cpp, Python, Java, Js, Go ...
See my other answer
The vimpdb plugin integrates the Python debugger pdb into the VIM editor.
I do recommend it.
Hope it helps.
Vim and pdb-clone is the combination I use. I use Home - pyclewn which provides a replacement for pdb called pdb-clone that is quite faster than vanilla pdb. It integrates well with vim via a plugin, and the thing I appreciate most is that it takes care of breakpoints outside the code, not setting traces within, thus not messing up my line numbers. It does not have a watch window for python yet. You might have a look at vim-debug too, which I could not get to work with my existing highlighting setup.
See the "Debugging" section in this blog post. It shows how to setup F7 to set breakpoints and Shift+F7 to remove breakpoints. It also uses pdb, as mentioned before. With a little modification, you can replace the use of pdb with ipdb (pdb using ipython), which is a lot nicer to use.
It sounds like you want to use VIM as a Python IDE.
A quick Google search found this and this example, with many more.
EDIT: Well, Ok, it seems likely you've searched more than I have.
I hope someone else has some ideas.
From what I know, there is one more option: You could use Eclipse + PyDev for project managing and Vim as an editor for Eclipse. That way You could use the best of both worlds.
Also, I haven't tried it, but You could try this script.