简体   繁体   中英

Python Unittest to check input within while loop

I am new to python unit testing and I've has some success, but I am stuck on on this one unit test. I am trying to just try to check the input method named item and mock the input. I am not receiving any test results back. I am most likely doing it wrong, so any help would be awesome. Here is the code that is being tested

def get_input():

  myList = []
  popMax = 6


  while len(myList) < popMax:

    item = input("Enter a number: ")

    if item == "exit":
      break

 else:
    myList.append(item)
    print(myList)

print("This is your list!")
print(myList)

and here is the Test suite

import unittest
from unittest.mock import patch

from get_input import *

class GetInputTest(unittest.TestCase):
  @patch('builtins.input', return_value='yes')
  def test_answer_yes(self, input):
    self.assertEqual(get_input(), 'yes')




if __name__ == "__main__":
  unittest.main()

Thank you everyone in advanced

Your loop is an infinite loop (at least in the context of your test case). Indeed the only way to get out of the loop is to get an input being "exit", which does not happen in your test case.

Also, it's not clear how get_input() works, as you define popMax and myList but none of those is actually used in the function... Maybe you want to do myList.append(item) in the loop?

it should be the way your run the test. Here's my test code, and run the test manually. it run as expected since 'yes' will not fulfill the 'exit' test and you will get 6 items in the list.

def get_input():
    myList = []
    popMax = 6
    while len(myList) < popMax:
        item = input("Enter a number: ")
        if item == "exit":
            break
        else:
            myList.append(item)
            print(myList)
    print("This is your list!")
    print(myList)

class GetInputTest(unittest.TestCase):
    @patch('builtins.input', return_value='1\n2\n3\n4\n5\n6\n')
    def test_answer_yes(self, input):
        self.assertEqual(get_input(), 'yes')

>>> a = GetInputTest()    
>>> a.test_answer_yes()
['yes']
['yes', 'yes']
['yes', 'yes', 'yes']
['yes', 'yes', 'yes', 'yes']
['yes', 'yes', 'yes', 'yes', 'yes']
['yes', 'yes', 'yes', 'yes', 'yes', 'yes']
This is your list!
['yes', 'yes', 'yes', 'yes', 'yes', 'yes']
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    a.test_answer_yes()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/mock.py", line 1136, in patched
    return func(*args, **keywargs)
  File "<pyshell#9>", line 4, in test_answer_yes
    self.assertEqual(get_input(), 'yes')
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 797, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 790, in _baseAssertEqual
    raise self.failureException(msg)
AssertionError: None != 'yes'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM