Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #68162

Re: Testing interactive code using raw_input

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'url:pypi': 0.03; 'coverage.': 0.07; 'result,': 0.07; 'subject:code': 0.07; 'suppose': 0.07; 'test,': 0.07; 'arguments,': 0.09; 'part,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:using': 0.09; 'unittest': 0.09; 'python': 0.11; 'def': 0.12; 'function?': 0.16; 'inconvenient': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'supplying': 0.16; 'wrote:': 0.18; 'result.': 0.19; 'input': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'options': 0.25; 'this:': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'testing': 0.29; 'code': 0.31; 'assert': 0.31; "d'aprano": 0.31; 'factor': 0.31; 'steven': 0.31; 'anyone': 0.31; 'stuff': 0.32; 'url:python': 0.33; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'url:org': 0.36; 'e.g.': 0.38; 'to:addr :python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:* 10': 0.61; 'simple': 0.61; 'times': 0.62; 'myself': 0.63; 'provide': 0.64; 'more': 0.64; 'hints': 0.68; 'increase': 0.74; 'mock': 0.84; 'subject:Testing': 0.84; 'lot,': 0.93; 'technique': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Testing interactive code using raw_input
Date Mon, 10 Mar 2014 17:57:19 +0100
Organization None
References <531de147$0$29994$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bdb6ee.dip0.t-ipconnect.de
User-Agent KNode/4.11.5
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8012.1394470659.18130.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1394470660 news.xs4all.nl 2932 [2001:888:2000:d::a6]:37416
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:68162

Show key headers only | View raw


Steven D'Aprano wrote:

> Does anyone have any good hints for testing interactive code that uses
> raw_input, or input in Python 3?
> 
> A simple technique would be to factor out the interactive part, e.g. like
> this:
> 
> # Before
> def spam():
>     answer = raw_input(prompt)
>     return eggs(answer) + cheese(answer) + toast(answer)
> 
> # After
> def spam():
>     answer = raw_input(prompt)
>     return func(answer)
> 
> def func(s):
>     return eggs(s) + cheese(s) + toast(s)
> 
> 
> 
> and then test func. But how about times where it is inconvenient to
> factor out the raw_input stuff out of the function? E.g. suppose you have
> a function that takes some arguments, gathers some more values
> interactively, processes the lot, and then returns a result. With an
> automated test, I can provide the arguments, and check the result, but
> what are my options for *automatically* supplying input to raw_input?

https://pypi.python.org/pypi/mock

In Python 3 this is part of the standard library:

from unittest import mock

def sum_it():
    return "{} + {} = {}".format(input(), input(), input())

with mock.patch('builtins.input', side_effect="123"):
    assert sum_it() == "1 + 2 = 3"

I have not yet used it myself so far; instead I did something like

def sum_it(input=input):
   ...

and then passed a hand-crafted mock input function to increase coverage.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Testing interactive code using raw_input Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-10 15:59 +0000
  Re: Testing interactive code using raw_input Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-03-10 16:05 +0000
  Re:Testing interactive code using raw_input Dave Angel <davea@davea.name> - 2014-03-10 12:40 -0400
  Re: Testing interactive code using raw_input Peter Otten <__peter__@web.de> - 2014-03-10 17:57 +0100
    Re: Testing interactive code using raw_input Steven D'Aprano <steve@pearwood.info> - 2014-03-11 08:26 +0000
  Re: Testing interactive code using raw_input Ethan Furman <ethan@stoneleaf.us> - 2014-03-10 09:53 -0700
  Re: Testing interactive code using raw_input Ben Finney <ben+python@benfinney.id.au> - 2014-03-11 10:39 +1100

csiph-web