title: How to test while-loop (once) with nosetest (Python 2.7)
body: <p>I'm pretty new to this whole "programming thing" but at age 34 I thought that I'd like to learn the basics.
  I unfortunately don't know any python programmers. I'm learning programming due to personal interest (and more and more for the fun of it) but my "social habitat" is not "where the programmers roam" ;) .
  I'm almost finished with Zed Shaws "Learn Python the Hard Way" and for the first time I can't figure out a solution to a problem. The last two days I didn't even stumble upon useful hints where to look when I repeatedly rephrased (and searched for) my question.
  So stackoverflow seems to be the right place.
  Btw.: I lack also the correct vocabular quite often so please don't hesitate to correct me :) . This may be one reason why I can't find an answer.
  I use Python 2.7 and nosetests.</p>
  
  <p>How far I solved the problem (I think) in the steps I solved it:</p>
  
  <p>Function 1:</p>
  
  <pre><code>def inp_1():
      s = raw_input("&gt;&gt;&gt; ")
      return s
  </code></pre>
  
  <p>All tests import the following to be able to do the things below:</p>
  
  <pre><code>from nose.tools import *
  import sys
  from StringIO import StringIO
  from mock import *
  import __builtin__
  # and of course the module with the functions
  </code></pre>
  
  <p>Here is the test for inp_1:</p>
  
  <pre><code>import __builtin__
  from mock import *
  
  def test_inp_1():
      __builtin__.raw_input = Mock(return_value="foo")
      assert_equal(inp_1(), 'foo')
  </code></pre>
  
  <p>This function/test is ok. </p>
  
  <p>Quite similar is the following function 2:</p>
  
  <pre><code>def inp_2():
      s = raw_input("&gt;&gt;&gt; ")
      if s == '1':
          return s
      else:
          print "wrong"
  </code></pre>
  
  <p>Test:</p>
  
  <pre><code>def test_inp_2():
      __builtin__.raw_input = Mock(return_value="1")
      assert_equal(inp_1(), '1')
  
      __builtin__.raw_input = Mock(return_value="foo")
      out = StringIO()
      sys.stdout = out
      inp_1()
      output = out.getvalue().strip()
      assert_equal(output, 'wrong')
  </code></pre>
  
  <p>This function/test is also ok. </p>
  
  <p>Please don't assume that I really know what is happening "behind the scenes" when I use all the stuff above. I have some layman-explanations how this is all functioning and why I get the results I want but I also have the feeling that these explanations may not be entirely true. It wouldn't be the first time that how I think sth. works turns out to be different after I've learned more. Especially everything with "__" confuses me and I'm scared to use it since I don't really understand what's going on. Anyway, now I "just" want to add a while-loop to ask for input until it is correct:</p>
  
  <pre><code>def inp_3():
      while True:
          s = raw_input("&gt;&gt;&gt; ")
          if s == '1':
              return s
          else:
              print "wrong"
  </code></pre>
  
  <p>The test for inp_3 I thought would be the same as for inp_2 . At least I am not getting error messages. But the output is the following:</p>
  
  <pre><code>$ nosetests
  ......
  
       # &lt;- Here I press ENTER to provoke a reaction
       # Nothing is happening though.
  
  ^C   # &lt;- Keyboard interrupt (is this the correct word for it?)
  ----------------------------------------------------------------------
  Ran 7 tests in 5.464s
  
  OK
  $ 
  </code></pre>
  
  <p>The other 7 tests are sth. else (and ok).
  The test for inp_3 would be test nr. 8.
  The time is just the times passed until I press CTRL-C.
  I don't understand why I don't get error- or "test failed"-meassages but just an "ok".</p>
  
  <p>So beside the fact that you may be able to point out bad syntax and other things that can be improved (I really would appreciate it, if you would do this), my question is:</p>
  
  <p><strong>How can I test and abort while-loops with nosetest?</strong></p>