问题
I'd like to be able to enter an interactive session, preferably with IPython, if a unit test fails. Is there an easy way to do this?
edit: by "interactive session" I mean a full Python REPL rather than a pdb shell.
edit edit: As a further explanation: I'd like to be able to start an interactive session that has access to the context in which the test failure occurred. So for example, the test's self
variable would be available.
回答1:
In IPython, use %pdb before running the test
In [9]: %pdb
Automatic pdb calling has been turned ON
回答2:
Nosetests runner provides --pdb
option that will put you into the debugger session on errors or failures.
http://nose.readthedocs.org/en/latest/usage.html
回答3:
Are you really sure you want to do this? Your unit tests should do one thing, should be well-named, and should clearly print what failed. If you do all of that, the failure message will pinpoint what went wrong; no need to go look at it interactively. In fact, one of the big advantages of TDD is that it helps you avoid having to go into the debugger at all to diagnose problems.
来源:https://stackoverflow.com/questions/3517410/drop-into-an-interactive-session-to-examine-a-failed-unit-test