By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

unit testing a method that asks two user inputs() in console

100+
P: 228
I have this small console app to add two numbers by asking the user to enter 2 numbers:

Expand|Select|Wrap|Line Numbers
  1.      def add():
  2.          a = int(input('Enter first number '))
  3.          b= int(input('Enter second number '))
  4.          return a + b
How do I unit test that? I tried the following but it returns false always even though it doesn't ask for user input which is great:

2
3
4
5
6
7
8
9

Expand|Select|Wrap|Line Numbers
  1. import unittest
  2.  
  3. from unittest.mock import patch
  4.  
  5.  
  6. @patch('builtins.input', return_value='2')
  7. @patch('builtins.input', return_value='3')
  8. def test_add(self,  a, b ):
  9.     self.assertEqual(result, 5)
Apr 3 '19 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.