Answers for "python test case input"

1

python mock input

from io import StringIO

def enter_name():
	name = input('Please enter name: ')
    return 'Hello ' + name

# Suppose we want to mock John as an input for a test
# All inputs end with a newline character
mock_input = StringIO('Johnn')

# monkeypatch is the parameter passed in test to mock certain values
def test_enter_name(monkeypatch):
    # Here 'sys.stdin' is used to specifically mock the standard input
    # The mock has to be set before the function being tested is run
  	monkeypatch.setattr('sys.stdin', mock_input)
    assert enter_name() == 'Hello John'
Posted by: Guest on September-04-2020

Python Answers by Framework

Browse Popular Code Answers by Language