1

I found some problem trying to create a py script that call another py script and input some int to it. I explain better:

my first script is simpleScript.py

def addition ( a , b ) :
    c = a + b 
    return c
x = int ( input ( 'input a int' ) )
y = int ( input ( 'input a int' ) )
summ = addition ( x , y )

on my second script tester.py I suppose to do something like this:

call simpleScript.py ;
input 4 as x ;
input 8 as y ;
run simpleScript.py using those two input ;

and some more stuff. The problem is that I can't really find ho to input it properly. Once I get it I'll be able to do all of the rest.

Thanks in advance.

lapolis
  • 13
  • 2
  • 1
    what about passing parameters instead? – Jean-François Fabre Dec 05 '17 at 14:09
  • Is there a reason you are using `input` if you do not actually want the user to input values? Just wanting to clarify, is the second script for test purposes, and you are wanting to simulate the inputs as a unit test of the first script? If so, this will help: https://stackoverflow.com/questions/46222661/how-to-mock-a-user-input-in-python - you want to look at mocking `builtins.input` to have a fake return value (the number a human would have entered). –  Dec 05 '17 at 14:11
  • @Bilkokuya yep, I'm coding a script in order to test my first script simulatin all the possible input that a human can input. – lapolis Dec 05 '17 at 14:27
  • Excellent, the solution I've linked should help you a long way. I'm going to flag this as duplicate - don't think of that as a bad thing though, it'll hopefully give you the answer from the previous time this was asked :) –  Dec 05 '17 at 14:30
  • 2
    Possible duplicate of [Mock standard input - multi line in python 3](https://stackoverflow.com/questions/39908390/mock-standard-input-multi-line-in-python-3) –  Dec 05 '17 at 14:31
  • I actually checked them already but I could not figure it out. So I ask it again with a simple example order to understand it properly. – lapolis Dec 05 '17 at 15:04

1 Answers1

0

Answering to help OP as there is a slightly difference between this and the linked duplicate (although I'd still suggest this is a duplicate that should be closed). The input here is being run at the module scope - so will execute when first loaded. i.e. simpleScript.py will ask for input when it is first called by import.

To do this in a test scenario, I'd firstly recommend to change this so that simpleScript.py provides a function that can be run. That way - the same solutions as the duplicates provide can be used.

But for this specific case where simpleScript.py does directly execute code, we can test the functionality with a test script below:

from unittest.mock import patch
from io import StringIO

@patch("sys.stdin", StringIO("1\n4"))
def test_simpleScript():
    import simpleScript

test_simpleScript()

This works by mocking the stdin (commandline input) to return 1, a new line then 4. Because of how input works with stdin this will return 1 on the first call to input and then 4 on the next call.

When we import the simpleScript module - all the code in it is executed. Because of the above mock patch we did, any calls to input during this will read the numbers we have given it. As the module only executes the first time it is loaded - you may have issues if you try multiple tests one after another. See here for how to force a module to reload if that is the case: How do I unload (reload) a Python module?