From 0fd094969cdb9d8b96ddeb85c0f589dac48c1e31 Mon Sep 17 00:00:00 2001 From: kkhandelwal Date: Wed, 27 Jun 2012 15:31:06 +0530 Subject: [PATCH 1/3] updated file --- FizzBuzz.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/FizzBuzz.py b/FizzBuzz.py index 00b6ebd..0aed186 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -1,18 +1,22 @@ """ Q1. Why is the report method untestable ? [2 pts] - +report_file takes pointer of file , available on local machine- external collaborators Q2. How will you change the api of the report method to make it more testable ? [2 pts] - +Passing a Filehandlewrapper to report function , so that function should not pick his collaborators. +We can use - +a. Filehandlewrapper - a wrapper class defined to have interface function for OPen +b. fileopener = using argument in report function, Implemented below """ class FizzBuzz(object): - def report(self, numbers): + def report(self, numbers,fileopener=open): - report_file = open('c:/temp/fizzbuzz_report.txt', 'w') + #report_file = Filehandlewrapper.open + report_file = fileopener('c:/temp/fizzbuzz_report.txt', 'w') for number in numbers: msg = str(number) + " " From 793b4c7fec3e530ae7c7426f288654bb5d540518 Mon Sep 17 00:00:00 2001 From: kkhandelwal Date: Wed, 27 Jun 2012 15:31:46 +0530 Subject: [PATCH 2/3] added --- TestFizzBuzzMocked.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/TestFizzBuzzMocked.py b/TestFizzBuzzMocked.py index 6f1d0d5..a7fb09a 100644 --- a/TestFizzBuzzMocked.py +++ b/TestFizzBuzzMocked.py @@ -17,7 +17,22 @@ def tearDown(self): self.fb = None def test_report(self): - pass + #mock the file handle + mock_opener_interface = self.mock() + mock_file = self.mock() + self.expectAndReturn(mock_opener_interface.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_interface.open) + + # verify + self.verify() + From ad6b4b5e7412e985a2f10e06b5a34b5ec371197d Mon Sep 17 00:00:00 2001 From: kkhandelwal Date: Wed, 27 Jun 2012 15:32:42 +0530 Subject: [PATCH 3/3] question 3 and 4 --- TestFizzBuzzStubbed.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/TestFizzBuzzStubbed.py b/TestFizzBuzzStubbed.py index 78ee454..78b048d 100644 --- a/TestFizzBuzzStubbed.py +++ b/TestFizzBuzzStubbed.py @@ -3,11 +3,19 @@ import FizzBuzz """ Q3. What will be printed when we execute 'python FizzBuzzStubbed.py' ? [3 pts] +#class setup - at start +setUpClass FizzBuzzStubbed - - - - +#test setup before each test +setup +test_report +teardown +#test setup before each test +.setup +test_report +teardown +#class teardown - at end +.tearDownClass @@ -16,8 +24,17 @@ report method of FizzBuzz object from a test case. [3 pts] """ + class MyStub(object): - pass + + def __init__(self): + self.values = [] + + def write(self, value): + self.values.append(value) + + def close(self): + self.closed = True