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

前端 未结 6 701
悲&欢浪女
悲&欢浪女 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: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'.

提交回复
热议问题