Rajkumar Srinivasan
Rajkumar Srinivasan

Reputation: 128

How to provide inputs runtime in python testcases

While writing test cases for python methods, how to provide inputs automatically to variables without prompting user to enter input ?

def function():
    speed = input('Enter speed:\n')
    if speed == 10:
        pass
    else:
        pass

For the above code if we call the method , it'll prompt user to enter input for speed.

>>> function()
Enter speed:
10

Is there any way to pass input using any functions or to skip a line and assign value for the variable?
Thanks in advance

Upvotes: 1

Views: 414

Answers (1)

Ofer Sadan
Ofer Sadan

Reputation: 11922

The function could be written to accept the test mode, simplest way is with a default None argument:

def function(speed=None):
    if speed is None:
        speed = input('Enter speed:\n')
    if speed == 10:
        pass
    else:
        pass

That way, it can still be called like before (it will run the input part):

function()

Or with different "inputs" during testing:

function(5)
function(10)
for i in range(33,50):
    function(i)

Upvotes: 2

Related Questions