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


Groups > comp.lang.python > #68182

Re: Testing interactive code using raw_input

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2a.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.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'output': 0.05; 'subject:code': 0.07; 'test,': 0.07; 'dependency': 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; '\xe2\x80\x94': 0.09; 'python': 0.11; 'finney': 0.16; 'function?': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'strange,': 0.16; 'not,': 0.20; 'seems': 0.21; 'input': 0.22; 'tests': 0.22; 'header:User- Agent:1': 0.23; 'test.': 0.24; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'testing': 0.29; 'external': 0.29; 'code': 0.31; "d'aprano": 0.31; 'george': 0.31; 'steven': 0.31; 'writes:': 0.31; 'anyone': 0.31; 'but': 0.35; 'received:com.au': 0.36; 'should': 0.36; 'unit': 0.37; 'ben': 0.38; 'whatever': 0.38; 'to:addr :python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; "you're": 0.61; 'skip:\xe2 10': 0.65; 'hints': 0.68; '\xe2\x80\x93': 0.77; 'truth': 0.81; 'mock': 0.84; 'received:125': 0.84; 'subject:Testing': 0.84; 'want:': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: Testing interactive code using raw_input
Date Tue, 11 Mar 2014 10:39:35 +1100
References <531de147$0$29994$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host vmx15867.hosting24.com.au
X-Public-Key-ID 0xBD41714B
X-Public-Key-Fingerprint 9CFE 12B0 791A 4267 887F 520C B7AC 2E51 BD41 714B
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-gpg.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:xq4i1GkdkkvreSefKAVUJjiZ07g=
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.8027.1394494787.18130.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1394494787 news.xs4all.nl 2859 [2001:888:2000:d::a6]:41788
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:68182

Show key headers only | View raw


Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes:

> Does anyone have any good hints for testing interactive code that uses 
> raw_input, or input in Python 3?

Are you testing the behaviour of the ‘input’ function?

If not, then it is an external dependency; and, since you're not
interested in testing its behaviour, you can contrive its behaviour to
be exactly what you want: Mock the function during your unit test, and
get it to produce whatever output you like.

Mocking is a blunt instrument, but when the external dependency is
problematic – and it seems like “access to an interactive terminal” is a
problematic dependency – you should mock the dependency out for those
tests where the dependency is external to what you want to test.

-- 
 \           “'Tis strange, — but true; for truth is always strange; / |
  `\    Stranger than fiction.” —“Lord” George Gordon Noel Byron, _Don |
_o__)                                                            Juan_ |
Ben Finney

Back to comp.lang.python | Previous | NextPrevious 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