6 Replies - 1163 Views - Last Post: 24 June 2015 - 12:41 PM Rate Topic: -----

#1 CrazySynthax   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 81
  • Joined: 12-June 15

User Input Test

Posted 21 June 2015 - 03:39 PM

Hi,

I'm writing a Python code that gets many inputs from the user. I want to test the code without manually entering each input individually. Is there any way to write to a file all inputs in advance and let the python code reads the inputs from the file?
Is This A Good Question/Topic? 1
  • +

Replies To: User Input Test

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: User Input Test

Posted 21 June 2015 - 03:54 PM

You probably don't realize what a great idea this is, so I'm going to tell you: this is a great idea. Not a new idea, of course, but still a great one. You've come upon one of the basic concepts of good software engineering, which is scripted tests. So +1 for that.

Now, for your question: is it possible to set up your program to read input from a file? Well, yes. How you'd do it depends how your program handles input. It might be easy, or it might require a little bit of work. Probably if you show us some code it'll be easier to get at that question.

You might also consider looking into unit testing, which might be a little easier to work with. In unit testing, you take small isolated pieces of a program and test each of them to make sure they work as advertised. Then you can have a better sense of confidence that the program as a whole, composed of those small isolated units, works as advertised. However, this requires that you've composed your program of small isolated functions, which you might not have done. As I say, show us some code and I'm sure you'll get plenty of good suggestions.
Was This Post Helpful? 1
  • +
  • -

#3 Lemur   User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1453
  • View blog
  • Posts: 3,633
  • Joined: 28-November 09

Re: User Input Test

Posted 21 June 2015 - 03:55 PM

If you're looking for more advanced testing that'll be used professionally, take a look into Unittest, Doctest, or one of many others detailed here: http://docs.python-g.../writing/tests/

Post your code if you could, it'd help with next steps.

From there I would guess that you have your logic broken into functions and kept the IO separate. If you haven't, do that now. To make your code more testable, each function should do one and only one thing.

I'm going to demonstrate with pytest: http://pytest.org/la...html#getstarted

Say we have the function add2:
def add2(x):
  return x + 2



It has a clear purpose and does only one thing, making it very easy to test:
def test_add2():
  assert add2(3) == 5



...and really that's all there is to it, you're just making assertions about your code and validating them by running against your code. Of course there's a lot more detail to be had here such as stubbing, mocking, and other fundamentals, but for the purposes of a basic IO program test that should get you going.
Was This Post Helpful? 1
  • +
  • -

#4 CrazySynthax   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 81
  • Joined: 12-June 15

Re: User Input Test

Posted 21 June 2015 - 04:03 PM

I've read about UnitTest. I didn't figure out yet how to use these UnitTests in order to give inputs automatically (instead of human user).

So... Here is a piece of my code:

def DrawCard():

    global deck
    card=deck.deal()
    print "Card dealt: ", card 
    loc=-1
    while (True):
        loc = raw_input("Choose location (1-20) to place the new card: ")
        try:
            loc=int(loc)  
            if (loc<1 or loc>20):
                print "Error: input is out of range"
            elif type(table[loc-1])==str:
                print "a card was already placed in this spot"
            else: #this is a good choice
                table[loc-1]=str(card)
                break;
        except ValueError:
            print "input is not an integer"
                        
   

def CommunicateUser():

    global end
    while (end==0):
        PrintTableau()
        c = raw_input("Choose an option: d ­ draw a card, s ­ simple calculation, a ­ advanced calculation, q - quit ")
        if c=='d':
            DrawCard()
        if c=='s':
            SimpleCalc()
        if c=='a':
            AdvancedCalc()
        if c=='q':
            Quit()
        
    PrintTableau()
    



A short explaination: if the user presses 'd', then he gets a card and has to decide where to place it. I didn't copy the functions of SimpleCalc and AdvancedCalc... PrintTableau prints the table of the game.
Was This Post Helpful? 0
  • +
  • -

#5 Nallo   User is offline

  • D.I.C Regular
  • member icon

Reputation: 166
  • View blog
  • Posts: 258
  • Joined: 19-July 09

Re: User Input Test

Posted 23 June 2015 - 06:26 AM

Your program reads from stdin. So what you need is another program that writes to stdout and connect stdout with stdin through a pipe.

From the command line on a unix system (maybe python2 or python3 instead of python):
cat input.txt | python your_prog.py



Windows equivalent (if I remember correctly … been years since I last used windows):
type input.txt | python your_prog.py


Was This Post Helpful? 2
  • +
  • -

#6 witeboy724   User is offline

  • D.I.C Head
  • member icon

Reputation: 86
  • View blog
  • Posts: 209
  • Joined: 21-June 12

Re: User Input Test

Posted 23 June 2015 - 01:25 PM

View PostNallo, on 23 June 2015 - 06:26 AM, said:

Windows equivalent (if I remember correctly … been years since I last used windows):
type input.txt | python your_prog.py


I'm mostly using Win 7 right now and can confirm. If this is something you want to do just:
import sys

for line in sys.stdin:
    print line


I thought it would have something to do with sys.argv, but I just now learned more about stdin
Was This Post Helpful? 2
  • +
  • -

#7 CrazySynthax   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 81
  • Joined: 12-June 15

Re: User Input Test

Posted 24 June 2015 - 12:41 PM

thanks for your answers.
I understood that I can do it by Unix or Windows command window.

But, do you know any way to run this by Python Shell?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1