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) def tearDown(self): '''Clear temporary testing variables.''' # Return sys.stdin to its normal state. sys.stdin = self.old_stdin When I run my tests I get the promising: c:\thinkpython2\ch3\ex_3-3>py -m unittest F... ====================================================================== FAIL: test_get_input (test_draw2x2grid.TestGetInput) Test that get_input() returns the expected result. ---------------------------------------------------------------------- Traceback (most recent call last): File "c:\thinkpython2\ch3\ex_3-3\test_draw2x2grid.py", line 119, in test_get_input self.assertEqual(sys.stdin.getvalue(), expected) AssertionError: '5' != 5 ---------------------------------------------------------------------- Ran 4 tests in 0.000s FAILED (failures=1) Currently the get_input() function is: def get_input(): '''Get string input from the user and convert it to an integer. This integer is returned to the caller. :num_sides: Number of sides for the displayed grid.''' pass I was bored with the actual exercise 3.3 in the text which is just to print an ASCII 2x2 grid and then a 3x3 grid, so I am using the TDD process to created a generalized grid printing program. So please excuse the file names "test_draw2x2grid.py" and "draw2x2grid.py". Let me know if there is a better way to test input() (The heart of get_input()), or if I have outright misconceptions, etc. TIA! -- boB _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor