diff --git a/FizzBuzz.py b/FizzBuzz.py index 00b6ebd..2b5b96a 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -1,7 +1,6 @@ """ Q1. Why is the report method untestable ? [2 pts] - - +--> report_file open and path is an external collaborative (platform and system environment dependent) which is handled in the function. hence, it is untestable. Q2. How will you change the api of the report method to make it more testable ? [2 pts] @@ -9,11 +8,11 @@ """ -class FizzBuzz(object): - def report(self, numbers): - report_file = open('c:/temp/fizzbuzz_report.txt', 'w') + +class FizzBuzz(object): + def report(self, numbers, file_handle): for number in numbers: msg = str(number) + " " fizzbuzz_found = False @@ -21,16 +20,18 @@ def report(self, numbers): msg += "fizz " fizzbuzz_found = True if number % 5 == 0: - msg += "buzz " - fizzbuzz_found = True - + msg += "buzz " + fizzbuzz_found = True if fizzbuzz_found: - report_file.write(msg + "\n") + file_handle.write(msg + "\n") + + - report_file.close() if "__main__" == __name__: - fb = FizzBuzz() - fb.report(range(100)) + fb = FizzBuzz() + file_handle = open('temp.txt', 'w') # can create open wrapper + fb.report(range(100), file_handle) + file_handle.close() diff --git a/TestFizzBuzzMocked.py b/TestFizzBuzzMocked.py index 6f1d0d5..e8e4b09 100644 --- a/TestFizzBuzzMocked.py +++ b/TestFizzBuzzMocked.py @@ -3,7 +3,30 @@ import FizzBuzz """ Q5. Write the psuedocode for the test_repport method, such that it uses PyMock - mock objects to test the report method of FizzBuzz. [5 pts] +mock objects to test the report method of FizzBuzz. [5 pts] +Ans:- + +def test_report(self): +#mock the file handle +mock_opener = self.mock() +mock_file = self.mock() +self.expectAndReturn(mock_opener.open('c:/temp/fizzbuzz_report.txt', 'w'), mock_file) +mock_file.write("3 fizz \n") +mock_file.close() + +#replay +self.replay() + +#call API +self.fb.report(numbers, opener=mock_opener.open) + +# verify +self.verify() + + + + + """ class TestFizzBuzzMocked(pymock.PyMockTestCase): @@ -17,7 +40,23 @@ def tearDown(self): self.fb = None def test_report(self): - pass + #mock the file handle + mock_opener = self.mock() + mock_file = self.mock() + self.expectAndReturn(mock_opener.open('c:/temp/fizzbuzz_report.txt', 'w'), mock_file) + mock_file.write("3 fizz \n") + mock_file.close() + + #replay + self.replay() + + #call API + self.fb.report(numbers, opener=mock_opener.open) + + # verify + self.verify() + + @@ -37,4 +76,4 @@ def test_report(self): if __name__ == "__main__": - unittest.main() + unittest.main() \ No newline at end of file diff --git a/TestFizzBuzzStubbed.py b/TestFizzBuzzStubbed.py index 78ee454..056f649 100644 --- a/TestFizzBuzzStubbed.py +++ b/TestFizzBuzzStubbed.py @@ -4,6 +4,18 @@ """ Q3. What will be printed when we execute 'python FizzBuzzStubbed.py' ? [3 pts] +Ans:- + +setUpClass FizzBuzzStubbed +setup +test_report +teardown +test_report +setup +test_report +teardown +tearDownClass + @@ -13,7 +25,46 @@ Q4. Implement MyStub class so that you can send it as a fake object to the - report method of FizzBuzz object from a test case. [3 pts] +report method of FizzBuzz object from a test case. [3 pts] +Ans:- + +###############myStub Class############################ +class MyStub(object): + +def gen_open_stub(output_stub): +def open(fpath, mode): +return output_stub +return open + +def __init__(self): +self.values = [] +self.cnt = 0 +def write(self, value): +self.values.append(value) +def close(self): +self.closed = True +def readline(self): +if self.cnt <= len(self.values): +ret_val = self.values[self.cnt] +self.cnt += 1 +return ret_val +else: +raise StopError() +############# And appropriate changes required for test_report method ########################## + + +def test_report(self): +print "test_report" +output_stub = MyStub() +my_open= MyStub.gen_open_stub(output_stub) +fb = FizzBuzz.FizzBuzz() +fb.report([33], my_open) +self.assertEqual(output_stub.readline(), '33 fizz \n' ) +fb.report([55], my_open) +self.assertEqual(output_stub.readline(), '55 buzz \n' ) +fb.report([15], my_open) +self.assertEqual(output_stub.readline(), '15 fizz buzz \n' ) +pass """ class MyStub(object): @@ -56,4 +107,4 @@ def test_report_for_empty_list(self): pass if __name__ == "__main__": - unittest.main() + unittest.main() \ No newline at end of file