[Tutor] TDD: How to test input()?
boB Stepp
robertvstepp at gmail.com
Fri Oct 7 21:44:17 EDT 2016
Oops!
On Fri, Oct 7, 2016 at 8:26 PM, boB Stepp <robertvstepp at gmail.com> wrote:
> I think I have this figured out, but I want to be certain I am doing
> it both correctly and in the preferred way. I have a need for a
> get_input() function and have written my first test for this function
> as follows:
>
> class TestGetInput(unittest.TestCase):
> '''Tests for the function get_input().'''
>
> def setUp(self):
> '''Establish conditions for running these tests.'''
>
> # Redirect sys.stdin in order to test input functions.
> self.old_stdin = sys.stdin
> sys.stdin = StringIO('5')
>
> def test_get_input(self):
> '''Test that get_input() returns the expected result.'''
>
> expected = 5
> draw2x2grid.get_input()
> self.assertEqual(sys.stdin.getvalue(), expected)
My brain was scrambled. The test_get_input(self) should instead be:
def test_get_input(self):
'''Test that get_input() returns the expected result.'''
expected = 5
self.assertEqual(draw2x2grid.get_input(), expected)
boB
More information about the Tutor
mailing list