From c59683e36926c44ae6e81f28de9d356cea26e8ae Mon Sep 17 00:00:00 2001 From: Ashish-Yadav Date: Wed, 27 Jun 2012 16:22:06 +0530 Subject: [PATCH 1/6] Update master --- FizzBuzz.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/FizzBuzz.py b/FizzBuzz.py index 00b6ebd..9f5f84a 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -1,12 +1,14 @@ """ Q1. Why is the report method untestable ? [2 pts] - +Ans:- The report method has external depndencies. + It depends on the "open function" and the Txt file. Q2. How will you change the api of the report method to make it more testable ? [2 pts] - - +Ans:- The API of the report method can be changed by the the number of arguments it accepts as + "report(self,numbers,filehandler)" +where "filehandler" will be used for creating an impression of reading values from file. """ class FizzBuzz(object): From 5f541d2170735c1cbcc01f08b988a3941b4efc80 Mon Sep 17 00:00:00 2001 From: Ashish-Yadav Date: Wed, 27 Jun 2012 16:25:30 +0530 Subject: [PATCH 2/6] Update master --- TestFizzBuzzMocked.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/TestFizzBuzzMocked.py b/TestFizzBuzzMocked.py index 6f1d0d5..af4f6de 100644 --- a/TestFizzBuzzMocked.py +++ b/TestFizzBuzzMocked.py @@ -17,7 +17,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, fileOpener=mock_opener.open) + + # verify + self.verify() + + From dd8e5a7a88a904b74a6f97b8e694d6992f5b73fc Mon Sep 17 00:00:00 2001 From: Ashish-Yadav Date: Wed, 27 Jun 2012 16:26:20 +0530 Subject: [PATCH 3/6] Update master --- FizzBuzz.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FizzBuzz.py b/FizzBuzz.py index 9f5f84a..7c16116 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -12,9 +12,9 @@ """ class FizzBuzz(object): - def report(self, numbers): + def report(self, numbers,opener): - report_file = open('c:/temp/fizzbuzz_report.txt', 'w') + report_file = opener('c:/temp/fizzbuzz_report.txt', 'w') for number in numbers: msg = str(number) + " " From c2a9320335d77ea8642a17aca82d5b37f74ff785 Mon Sep 17 00:00:00 2001 From: Ashish-Yadav Date: Wed, 27 Jun 2012 16:26:45 +0530 Subject: [PATCH 4/6] Update master --- TestFizzBuzzMocked.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TestFizzBuzzMocked.py b/TestFizzBuzzMocked.py index af4f6de..ed5caa9 100644 --- a/TestFizzBuzzMocked.py +++ b/TestFizzBuzzMocked.py @@ -28,7 +28,7 @@ def test_report(self): self.replay() #call API - self.fb.report(numbers, fileOpener=mock_opener.open) + self.fb.report(numbers, opener=mock_opener.open) # verify self.verify() From b4cb2d5344b3ffd4b2c5db89ec666ffcc9a5e037 Mon Sep 17 00:00:00 2001 From: Ashish-Yadav Date: Wed, 27 Jun 2012 16:27:57 +0530 Subject: [PATCH 5/6] Update master --- TestFizzBuzzMocked.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/TestFizzBuzzMocked.py b/TestFizzBuzzMocked.py index ed5caa9..fb9009e 100644 --- a/TestFizzBuzzMocked.py +++ b/TestFizzBuzzMocked.py @@ -4,6 +4,30 @@ """ 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] + +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): From 155a3bfdc2a3af1e2fd3287b433df0168a0a1a56 Mon Sep 17 00:00:00 2001 From: Ashish-Yadav Date: Wed, 27 Jun 2012 16:32:26 +0530 Subject: [PATCH 6/6] Update master --- TestFizzBuzzStubbed.py | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/TestFizzBuzzStubbed.py b/TestFizzBuzzStubbed.py index 78ee454..96b8b6a 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 + @@ -14,6 +26,50 @@ 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] + +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):