title: Passing different raw_input to a function being tested in Python
body: <p>I want to test with <code>unittest</code> a function in Python (2.7) that use different <code>raw_input</code>.</p>
  
  <p>How can I achieve this?</p>
  
  <p>Function (in module <code>fc</code>):</p>
  
  <pre><code>def main():
      name            = raw_input("name: ").lower()
      surname         = raw_input("surname: ").lower()
      birth_date    = raw_input("Birth date (dd/mm/yyyy): ").lower()
      city          = raw_input("city: ").lower()
      sex           = raw_input("sex (m/f): ").lower()
      #other tasks...
  </code></pre>
  
  <p>test function:</p>
  
  <pre><code>import fc
  import unittest
  
  class test_main_fc(unittest.TestCase):
  
      def test_main(self):
          #how can I give to main the parameters that will ask?
          self.assertEqual(fc.main(), 'rssmra80a01l781k')             
  
  if __name__ == '__main__':  
      unittest.main()
  </code></pre>
  
  <p>The solution I could find, <a href="https://stackoverflow.com/a/25275926/4949261" title="python mocking raw input in unittests">this</a>, works for 1 input passed at a time. I want to know how to pass different values to the <code>main</code> function.</p>
  
  <p>This works for only 1 value of <code>raw_input</code> requested, in this case, <code>name</code>.</p>
  
  <pre><code>class test_main_fc(unittest.TestCase):
  
      def test_fc_output(self):
  
          original_raw_input = __builtins__.raw_input
          __builtins__.raw_input = lambda _: 'mario'
  
          #capturing the output
          with captured_output() as (out, err):
              fc.main()
              output = out.getvalue().strip()
  
          self.assertEqual(output, 'rssmra80a01l781k')
  
          __builtins__.raw_input = original_raw_input
  </code></pre>