Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import builtins
- import unittest
- def main():
- ''' Main app '''
- range_start = int(input())
- range_stop = int(input())
- for i in range(range_start, range_stop):
- print(i)
- class TestMethods(unittest.TestCase):
- def mock_print(self, message, *args, **kwargs):
- '''
- Alters built-in input() function.
- Gathers output to list buffer.
- '''
- self.output_buffer.append(message)
- def mock_input(self, *args, **kwargs):
- '''
- Alters built-in print() function.
- Injects input data from list buffer.
- '''
- return self.input_data.pop()
- def set_input(self, input_data):
- '''Supplies data for mock_input().'''
- self.input_data = input_data
- self.input_data.reverse()
- def get_output(self):
- '''Returns data gathered by mock_print().'''
- output = self.output_buffer
- self.output_buffer = []
- return output
- def setUp(self):
- # let's store original functions somewhere
- self.original_input = builtins.input
- self.original_print = builtins.print
- # declate a buffer for output data
- self.output_buffer = []
- # we are replacing built-in function input() and output()
- builtins.input = self.mock_input
- builtins.print = self.mock_print
- def test_typical_case(self):
- # set test data
- input_data = ['2', '5']
- output_data = [2, 3, 4]
- # set desired input
- self.set_input(input_data)
- # run a function
- main()
- # assert output
- self.assertListEqual(self.get_output(), output_data)
- def test_empty(self):
- # set test data
- input_data = ['1000', '0']
- output_data = []
- # set desired input
- self.set_input(input_data)
- # run a function
- main()
- # assert output
- self.assertListEqual(self.get_output(), output_data)
- def tearDown(self):
- # let's restore original function
- builtins.input = self.original_input
- builtins.print = self.original_print
- if __name__ == '__main__':
- unittest.main()
Add Comment
Please, Sign In to add comment