From 0e1e05ea2d215393845a1288d31a262cc94dd82e Mon Sep 17 00:00:00 2001 From: niveditakumar Date: Tue, 26 Jun 2012 18:33:10 +0530 Subject: [PATCH 1/3] Update master --- FizzBuzz.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) 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() From b7a8e0ce3d065ac49996c8b07568711f834e93ae Mon Sep 17 00:00:00 2001 From: niveditakumar Date: Thu, 28 Jun 2012 10:09:54 +0530 Subject: [PATCH 2/3] Update master --- TestFizzBuzzMocked.py | 45 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) 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 From 26ee23d115db7de98ad5ee4bcd9b4ced29c69813 Mon Sep 17 00:00:00 2001 From: niveditakumar Date: Thu, 28 Jun 2012 10:10:57 +0530 Subject: [PATCH 3/3] Update master --- TestFizzBuzzStubbed.py | 55 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) 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