Testing interactive code using raw_input
Ben Finney
ben+python at benfinney.id.au
Mon Mar 10 19:39:35 EDT 2014
Steven D'Aprano <steve+comp.lang.python at 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
More information about the Python-list
mailing list