views:

61

answers:

3

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  A: 

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.

Ron Romero
+1: Please don't "automate" debugging. When the test fails, that's enough. An interactive session may not be the right thing to do. Entering into a text editor may be a bette choice. Or entering an an automated order for pizza because it will be a long night may be better than an interactive session.
S.Lott
I guess you're saying I should write more tests to automatically do the stuff that I would do from the interactive shell. Seems like generally good advice, but there may be cases where it's helpful to do some exploring in order to figure out what the test needs to be.
intuited
@intuited: Exploring is very good. Automating testing to "magically" switch between testing and interactive session is a bad idea. Too much automation is rarely useful.
S.Lott
@S.Lott: The idea is to be able to run a particular test in such a way that an interactive session gets started in place of the failed assertion. That way when there is a case where the test doesn't make it immediately obvious what's wrong, it's convenient to jump into an environment well suited to digging around looking for the problem.
intuited
@intuited: An interactive session may not be the right thing to do. Entering into a text editor may be a bette choice. Or entering an an automated order for pizza because it will be a long night may be better than an interactive session. Don't waste time trying to automate this. It's just debugging. You're writing code to save two mouse clicks.
S.Lott
@S.Lott: I don't understand what you mean. If there is a way to recreate the circumstances under which a test fails with two mouse clicks, instead of writing a bunch of code into an interactive session, well, that would pretty much be an answer to my question. I guess you're talking about using a debugger to catch test assertions, as Wai Yip Tung and Kozyarchuk have mentioned? ______ I don't really use mouse-driven software; my testing generally involves reading the output of running the test script into a buffer in `vim`. So far it's been plenty; am I missing out on something?
intuited
@intuited: Whatever you're talking about in this comment is not in your question. Please fix your question to include this entire use case. I can't see the point, since I don't use the debugger. If tests fail, I read the log and fix the code with an editor. I can't understand your use case without more details. Please **update** the question.
S.Lott
+1  A: 

In IPython, use %pdb before running the test

In [9]: %pdb
Automatic pdb calling has been turned ON
Wai Yip Tung
Hmm... that's about halfway there. Well, maybe. I actually want to be in an interactive shell, not so much in pdb.
intuited
I'm not sure why you want to do this. But once in pdb, just enter `q` will get you back to the interactive shell.
Wai Yip Tung
Hmmm... not exactly. Entering `q` will exit the debug context entirely. I want to run a shell from within the debug context. For example, `self` should be defined.
intuited
Nevermind, I didn't realize that `pdb` just layers shortcut commands on top of a full REPL. This is what I want.
intuited
A: 

Nosetests runner provides -pdb option that will put you into the debugger session on errors or failures.

http://somethingaboutorange.com/mrl/projects/nose/0.11.2/usage.html#extended-usage

Kozyarchuk