Can I debug with python debugger when using py.test somehow?

前端 未结 6 693
悲&欢浪女
悲&欢浪女 2020-12-08 09:22

I am using py.test for unit testing my python program. I wish to debug my test code with the python debugger the normal way (by which I mean pdb.set_trace() in the code) but

相关标签:
6条回答
  • 2020-12-08 09:27

    Add and remove breakpoints without editing source files

    Although you can add breakpoints by adding breakpoint() or set_trace() statements to your code, there are two issues with this approach:

    • Firstly, once you have started running your code, there is no way to remove your breakpoint. I often find that once I start running my code and reach an initial breakpoint, I want to place another one and remove the initial breakpoint. After breakpoint() drops me into the debugger I can add additional breakpoints, but I can't remove the initial one. Although this can be mitigated somewhat by putting the initial breakpoint statement higher up, if you have parametrised tests then even that is limited. I may find myself repeating cont very often.
    • Secondly, it requires changes to the source code. You need to remember to remove all breakpoint() commands before committing any code to version control, you have to remove them before switching branches, etc. I sometimes find I want to use the debugger to compare test runs between two branches, and having to edit the source code to add a breakpoint every time makes that a considerably slower and more error-prone exercise. I may even want to add a breakpoint in a library I'm calling, in which case the file I'm editing may not even me in my git repository but somewhere deep in my conda environment, increasing the risk of forgetting to remove it. Editing files to add break points is, in my humble opinion, ugly.

    To add and remove breakpoints interactively without editing any source files, you can evoke pytest as follows (in the bash shell):

    python -mipdb $(type -p pytest) -s test_fileset.py
    

    The -s flag is crucial here, because it stops pytest from messing with stdin and stdout, and when running inside the debugger, pytest will fail to mess with stdin and stdout and everything will go wrong. The exact calling syntax will be different for different shells.

    0 讨论(0)
  • 2020-12-08 09:28

    Similar to Peter Lyon's answer, but with the exact code you need for pytest, you can add the following to the bottom of your pytest module (my_test_module.py) :

    if __name__ == "__main__":
        pytest.main(["my_test_module.py", "-s"])
    

    Then you can invoke the debugger from the command line:

    pdb3 my_test_module.py
    

    Boom. You're in the debugger and able to enter debugger commands. This method leaves your test code un-littered with set_trace() calls and will run inside pytest 'normally'.

    0 讨论(0)
  • 2020-12-08 09:35

    I'm not familiar with py.test, put for unittest, you do the following. Maybe py.test is similar:

    In your test module (mytestmodule.py):

    if __name__ == "__main__":
        unittest.main(module="mytestmodule")
    

    Then run the test with

    python -m pdb mytestmodule.py
    

    You will get an interactive pdb shell.

    Looking at the docs, it looks like py.test has a --pdb command line option:

    http://codespeak.net/py/dist/test/features.html

    0 讨论(0)
  • 2020-12-08 09:39

    I found that I can run py.test with capture disabled, then use pdb.set_trace() as usual.

    > py.test --capture=no
    ============================= test session starts ==============================
    platform linux2 -- Python 2.5.2 -- pytest-1.3.3
    test path 1: project/lib/test/test_facet.py
    
    project/lib/test/test_facet.py ...> /home/jaraco/projects/project/lib/functions.py(158)do_something()
    -> code_about_to_run('')
    (Pdb)
    
    0 讨论(0)
  • 2020-12-08 09:40

    The easiest way is using the py.test mechanism to create breakpoint

    http://pytest.org/latest/usage.html#setting-a-breakpoint-aka-set-trace

    import pytest
    def test_function():
        ...
        pytest.set_trace()    # invoke PDB debugger and tracing
    

    Or if you want pytest's debugger as a one-liner, change your import pdb; pdb.set_trace() into import pytest; pytest.set_trace()

    0 讨论(0)
  • 2020-12-08 09:50

    it's real simple: put an assert 0 where you want to start debugging in your code and run your tests with:

    py.test --pdb 
    

    done :)

    Alternatively, if you are using pytest-2.0.1 or above, there also is the pytest.set_trace() helper which you can put anywhere in your test code. Here are the docs. It will take care to internally disable capturing before sending you to the pdb debugger command-line.

    0 讨论(0)
提交回复
热议问题